Skip to content

Commit

Permalink
add Image support
Browse files Browse the repository at this point in the history
  • Loading branch information
novakov-alexey authored and alexarchambault committed Mar 23, 2021
1 parent 29c09f7 commit 288a318
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 1 deletion.
23 changes: 23 additions & 0 deletions core/shared/src/main/scala/plotly/Trace.scala
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,29 @@ object Box {
)
}

@data(optionSetters = true) class Image(
z: Seq[Seq[Seq[Double]]],
x0: Option[Element] = None,
y0: Option[Element] = None,
name: Option[String] = None,
text: Option[Seq[String]] = None,
opacity: Option[Double] = None,
ids: Option[Seq[String]] = None,
dx: Option[Double] = None,
dy: Option[Double] = None,
source: Option[String] = None,
hoverinfo: Option[HoverInfo] = None,
hovertemplate: Option[Seq[String]] = None,
meta: Option[String] = None,
customdata: Option[Seq[String]] = None,
xaxis: Option[AxisReference] = None,
yaxis: Option[AxisReference] = None,
colormodel: Option[ColorModel] = None,
zmax: Option[Seq[Double]] = None,
zmin: Option[Seq[Double]] = None,
hoverlabel: Option[HoverLabel] = None
) extends Trace

@data(optionSetters = true) class Bar(
x: Sequence,
y: Sequence,
Expand Down
9 changes: 9 additions & 0 deletions core/shared/src/main/scala/plotly/element/Alignment.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package plotly.element

sealed abstract class Alignment(val label: String) extends Product with Serializable

object Alignment {
case object Left extends Alignment("left")
case object Right extends Alignment("right")
case object Auto extends Alignment("auto")
}
11 changes: 11 additions & 0 deletions core/shared/src/main/scala/plotly/element/ColorModel.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package plotly.element

sealed abstract class ColorModel(val label: String) extends Product with Serializable

object ColorModel {
case object RGB extends ColorModel("rgb")
case object RGBA extends ColorModel("rgba")
case object RGBA256 extends ColorModel("rgba256")
case object HSL extends ColorModel("hsl")
case object HSLA extends ColorModel("hsla")
}
1 change: 1 addition & 0 deletions core/shared/src/main/scala/plotly/element/HoverInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ object HoverInfo {
case object Z extends Element("z")
case object Text extends Element("text")
case object Name extends Element("name")
case object Color extends Element("color")


case object All extends HoverInfo {
Expand Down
12 changes: 12 additions & 0 deletions core/shared/src/main/scala/plotly/element/HoverLabel.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package plotly.element

import dataclass.data

@data(optionSetters = true) class HoverLabel(
bgcolor: Option[OneOrSeq[Color]] = None,
bordercolor: Option[OneOrSeq[Color]] = None,
font: Option[HoverLabelFont] = None,
align: Option[OneOrSeq[Alignment]] = None,
namelength: Option[OneOrSeq[Int]] = None,
uirevision: Option[Element] = None
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package plotly.element

import dataclass.data

@data(optionSetters = true) class HoverLabelFont(
family: Option[OneOrSeq[String]] = None,
size: Option[OneOrSeq[Double]] = None,
color: Option[OneOrSeq[Color]] = None
)
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ object ArgonautCodecsInternals extends ArgonautCodecsExtra {
implicit val tickModeIsEnum = IsEnum.instance[TickMode](_.mode)
implicit val patternIsEnum = IsEnum.instance[Pattern](_.label)
implicit val rowOrderIsEnum = IsEnum.instance[RowOrder](_.label)
implicit val alignmentIsEnum = IsEnum.instance[Alignment](_.label)
implicit val colorModelIsEnum = IsEnum.instance[ColorModel](_.label)

def jsonSumDirectCodecFor(name: String): JsonSumCodec = new JsonSumCodec {
def encodeEmpty: Nothing =
Expand Down Expand Up @@ -225,6 +227,7 @@ object ArgonautCodecsInternals extends ArgonautCodecsExtra {
case "x" => Right(HoverInfo.X)
case "y" => Right(HoverInfo.Y)
case "z" => Right(HoverInfo.Z)
case "color" => Right(HoverInfo.Color)
case "text" => Right(HoverInfo.Text)
case "name" => Right(HoverInfo.Name)
case other => Left(s"Unrecognized hover info element: $other")
Expand Down
53 changes: 52 additions & 1 deletion tests/src/test/scala/plotly/doc/DocumentationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ package doc
import java.io.{ByteArrayOutputStream, File, InputStream}
import java.lang.{Double => JDouble}
import java.nio.file.Files

import argonaut.Argonaut._
import argonaut.{Json, Parse}
import plotly.layout.Layout
import org.mozilla.javascript._
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import plotly.element.HoverInfo
import plotly.element.HoverInfo.{X,Y,Z}
import plotly.element.ColorModel._

import scala.util.matching.Regex

Expand Down Expand Up @@ -308,4 +310,53 @@ class DocumentationTests extends AnyFlatSpec with Matchers {
}
}

it should "demo Image Trace" in {
val js =
"""
|var data = [
| {
| type: "image",
| opacity: 0.1,
| x0: 0.05,
| y0: 0.05,
| colormodel: "rgb",
| hoverinfo: "x+y+z+color",
| z: [[[255, 0, 0], [0, 255, 0], [0, 0, 255]]]
| }
|];
|
|var layout = {
| width: 400,
| height: 400,
| title: "image with opacity 0.1"
|};
|
|Plotly.newPlot('myDiv', data, layout);
|""".stripMargin
val (data, maybeLayout) = plotlyDemoElements(js)
maybeLayout should ===(Some(
Layout()
.withWidth(400)
.withHeight(400)
.withTitle("image with opacity 0.1")
))

data.headOption match {
case Some(image) =>
val colors = Seq(
Seq(Seq(255d, 0d, 0d), Seq(0d, 255, 0), Seq(0d, 0, 255)),
)
val expected = Image(z = colors)
.withOpacity(0.1)
.withX0(0.05)
.withY0(0.05)
.withHoverinfo(HoverInfo(X, Y, Z, HoverInfo.Color))
.withColormodel(RGB)

image should ===(expected)
case None =>
fail("data must contain an image trace")
}
}

}

0 comments on commit 288a318

Please sign in to comment.