Skip to content

Commit 8149ddf

Browse files
evanthomasalexarchambault
authored andcommitted
Fix issue #13 (#16)
Fixes issue: "Cannot convert undefined or null to object #13"
1 parent a91e44b commit 8149ddf

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

render/js/src/main/scala/plotly/Plotly.scala

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,56 @@
11
package plotly
22

3-
import io.circe.Json
3+
import io.circe.{Json, Printer}
44
import io.circe.syntax._
5-
import io.circe.scalajs.convertJsonToJs
65

76
import scala.scalajs.js
87
import scala.scalajs.js.Dynamic.{global => g}
98
import scala.scalajs.js.JSON
10-
119
import plotly.Codecs._
1210
import plotly.element.Color
1311
import plotly.layout._
14-
15-
import java.lang.{ Integer => JInt, Double => JDouble, Boolean => JBoolean }
12+
import java.lang.{Boolean => JBoolean, Double => JDouble, Integer => JInt}
1613

1714
object Plotly {
1815

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+
}
2221

23-
def plot(div: String, data: Seq[Trace], layout: Layout): Unit =
22+
def plot(div: String, data: Seq[Trace], layout: Layout): Unit = {
2423
g.Plotly.plot(
2524
div,
26-
convertJsonToJs0(data.asJson),
27-
convertJsonToJs0(layout.asJson)
25+
stripNulls(data.asJson),
26+
stripNulls(layout.asJson)
2827
)
28+
}
2929

30-
def plot(div: String, data: Seq[Trace]): Unit =
30+
def plot(div: String, data: Seq[Trace]): Unit = {
3131
g.Plotly.plot(
3232
div,
33-
convertJsonToJs0(data.asJson)
33+
stripNulls(data.asJson)
3434
)
35+
}
3536

3637
def plot(div: String, data: Trace, layout: Layout): Unit =
3738
g.Plotly.plot(
3839
div,
39-
convertJsonToJs0(data.asJson),
40-
convertJsonToJs0(layout.asJson)
40+
stripNulls(data.asJson),
41+
stripNulls(layout.asJson)
4142
)
4243

4344
def plot(div: String, data: Trace): Unit =
4445
g.Plotly.plot(
4546
div,
46-
convertJsonToJs0(data.asJson)
47+
stripNulls(data.asJson)
4748
)
4849

4950
implicit class TraceOps(val trace: Trace) extends AnyVal {
5051
def plot(div: String, layout: Layout): Unit =
5152
Plotly.plot(div, trace, layout)
52-
53+
5354
def plot(
5455
div: String,
5556
title: String = null,
@@ -115,7 +116,7 @@ object Plotly {
115116
implicit class TraceSeqOps(val traces: Seq[Trace]) extends AnyVal {
116117
def plot(div: String, layout: Layout): Unit =
117118
Plotly.plot(div, traces, layout)
118-
119+
119120
def plot(
120121
div: String,
121122
title: String = null,

0 commit comments

Comments
 (0)