|
1 | 1 | package plotly
|
2 | 2 |
|
3 |
| -import io.circe.Json |
| 3 | +import io.circe.{Json, Printer} |
4 | 4 | import io.circe.syntax._
|
5 |
| -import io.circe.scalajs.convertJsonToJs |
6 | 5 |
|
7 | 6 | import scala.scalajs.js
|
8 | 7 | import scala.scalajs.js.Dynamic.{global => g}
|
9 | 8 | import scala.scalajs.js.JSON
|
10 |
| - |
11 | 9 | import plotly.Codecs._
|
12 | 10 | import plotly.element.Color
|
13 | 11 | import plotly.layout._
|
14 |
| - |
15 |
| -import java.lang.{ Integer => JInt, Double => JDouble, Boolean => JBoolean } |
| 12 | +import java.lang.{Boolean => JBoolean, Double => JDouble, Integer => JInt} |
16 | 13 |
|
17 | 14 | object Plotly {
|
18 | 15 |
|
19 |
| - private def convertJsonToJs0(json: Json): js.Any = |
20 |
| - // getting weird errors if not doing the conversion-to-string then parsing |
21 |
| - JSON.parse(JSON.stringify(convertJsonToJs(json))) |
| 16 | + private val printer = Printer.noSpaces.copy(dropNullKeys = true) |
| 17 | + private def stripNulls(json: Json): js.Any = { |
| 18 | + // Remove empty objects |
| 19 | + JSON.parse(printer.pretty(json)) |
| 20 | + } |
22 | 21 |
|
23 |
| - def plot(div: String, data: Seq[Trace], layout: Layout): Unit = |
| 22 | + def plot(div: String, data: Seq[Trace], layout: Layout): Unit = { |
24 | 23 | g.Plotly.plot(
|
25 | 24 | div,
|
26 |
| - convertJsonToJs0(data.asJson), |
27 |
| - convertJsonToJs0(layout.asJson) |
| 25 | + stripNulls(data.asJson), |
| 26 | + stripNulls(layout.asJson) |
28 | 27 | )
|
| 28 | + } |
29 | 29 |
|
30 |
| - def plot(div: String, data: Seq[Trace]): Unit = |
| 30 | + def plot(div: String, data: Seq[Trace]): Unit = { |
31 | 31 | g.Plotly.plot(
|
32 | 32 | div,
|
33 |
| - convertJsonToJs0(data.asJson) |
| 33 | + stripNulls(data.asJson) |
34 | 34 | )
|
| 35 | + } |
35 | 36 |
|
36 | 37 | def plot(div: String, data: Trace, layout: Layout): Unit =
|
37 | 38 | g.Plotly.plot(
|
38 | 39 | div,
|
39 |
| - convertJsonToJs0(data.asJson), |
40 |
| - convertJsonToJs0(layout.asJson) |
| 40 | + stripNulls(data.asJson), |
| 41 | + stripNulls(layout.asJson) |
41 | 42 | )
|
42 | 43 |
|
43 | 44 | def plot(div: String, data: Trace): Unit =
|
44 | 45 | g.Plotly.plot(
|
45 | 46 | div,
|
46 |
| - convertJsonToJs0(data.asJson) |
| 47 | + stripNulls(data.asJson) |
47 | 48 | )
|
48 | 49 |
|
49 | 50 | implicit class TraceOps(val trace: Trace) extends AnyVal {
|
50 | 51 | def plot(div: String, layout: Layout): Unit =
|
51 | 52 | Plotly.plot(div, trace, layout)
|
52 |
| - |
| 53 | + |
53 | 54 | def plot(
|
54 | 55 | div: String,
|
55 | 56 | title: String = null,
|
@@ -115,7 +116,7 @@ object Plotly {
|
115 | 116 | implicit class TraceSeqOps(val traces: Seq[Trace]) extends AnyVal {
|
116 | 117 | def plot(div: String, layout: Layout): Unit =
|
117 | 118 | Plotly.plot(div, traces, layout)
|
118 |
| - |
| 119 | + |
119 | 120 | def plot(
|
120 | 121 | div: String,
|
121 | 122 | title: String = null,
|
|
0 commit comments