Skip to content

Commit 77cc443

Browse files
Added Voronoi and Stack charts
1 parent 22ad2b7 commit 77cc443

File tree

5 files changed

+112
-3
lines changed

5 files changed

+112
-3
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ scalacOptions ++= Seq(
1818
libraryDependencies ++= Seq(
1919
"org.scala-js" %%% "scalajs-dom" % "0.8.0",
2020
"com.github.japgolly.scalajs-react" %%% "core" % "0.8.1",
21-
"eu.unicredit" %%% "paths-scala-js" % "0.4.0"
21+
"eu.unicredit" %%% "paths-scala-js" % "0.4.4"
2222
)
2323

2424
jsDependencies ++= Seq(

src/main/scala/demo/colors.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ object colors {
2222

2323
def mix(c1: Color, c2: Color) = {
2424
val c3 = average(c1, c2)
25-
List(
25+
val colors = List(
2626
lighten(c1),
2727
c1,
2828
darken(c1),
@@ -33,6 +33,8 @@ object colors {
3333
c2,
3434
darken(c2)
3535
)
36+
37+
Stream.continually(colors).flatten
3638
}
3739

3840
def transparent(c: Color, alpha: Double = 0.7) = c.copy(alpha = alpha)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package demo.components
2+
3+
import scala.scalajs.js
4+
import japgolly.scalajs.react._
5+
import japgolly.scalajs.react.vdom.all.key
6+
import japgolly.scalajs.react.vdom.svg.all._
7+
import paths.high.Stack
8+
9+
import demo.colors._
10+
import bar.Stats
11+
12+
13+
object stack {
14+
private val palette = mix(Color(130, 140, 210), Color(180, 205, 150))
15+
private def below(p: js.Array[Double]) = s"translate(${ p(0) }, 320)"
16+
17+
val StackChart = ReactComponentB[Stats]("Stack chart")
18+
.render(stats => {
19+
val stack = Stack[Double](
20+
data = stats.values,
21+
accessor = identity,
22+
width = 380,
23+
height = 300,
24+
gutter = 10
25+
)
26+
val groups = stats.values.length
27+
val middle = groups / 2
28+
val count = stats.values.head.length
29+
30+
js.Dynamic.global.console.log(stack)
31+
32+
val rectangles = stack.curves.zipWithIndex map { case (curve, i) =>
33+
if (curve.group == 0) g(
34+
path(d := curve.line.path.print, stroke := "none", fill := string(palette(curve.group))),
35+
text(transform := below(curve.line.centroid), textAnchor := "middle", stats.labels(curve.index))
36+
)
37+
else path(d := curve.line.path.print, stroke := "none", fill := string(palette(curve.group)))
38+
}
39+
40+
svg(width := 460, height := 400,
41+
g(transform := "translate(80,50)", rectangles)
42+
)
43+
})
44+
.build
45+
}

src/main/scala/demo/components/toplevel.scala

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import sankey._
1515
import line._
1616
import smoothline._
1717
import waterfall._
18+
import voronoi._
19+
import stack._
1820

1921

2022
object toplevel {
@@ -97,6 +99,9 @@ object toplevel {
9799
SLink(start = "Minerals", end = "Chemicals", weight = 25)
98100
)
99101
)
102+
val voronoiPoints = (1 to 30) map { _ =>
103+
(js.Math.random(), js.Math.random())
104+
}
100105
val tickers = List(
101106
List(
102107
Event("Jan 2000", 39.81),
@@ -506,7 +511,19 @@ object toplevel {
506511
id = Some("waterfall"),
507512
title = "Waterfall Chart",
508513
text = "A breakdown of incomes and costs."
509-
), WaterfallChart(balance))
514+
), WaterfallChart(balance)),
515+
Panel(PanelContent(
516+
id = Some("voronoi"),
517+
title = "Voronoi Diagram",
518+
text = "A convex tiling of the plane."
519+
), VoronoiChart(voronoiPoints))
520+
),
521+
div(className := "row",
522+
Panel(PanelContent(
523+
id = Some("stack"),
524+
title = "Stack Chart",
525+
text = "Here is a stacked bar chart example."
526+
), StackChart(stats))
510527
)
511528
)
512529
)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package demo.components
2+
3+
import scala.scalajs.js
4+
import japgolly.scalajs.react._
5+
import japgolly.scalajs.react.vdom.all.key
6+
import japgolly.scalajs.react.vdom.svg.all._
7+
import paths.high.Voronoi
8+
9+
import demo.colors._
10+
11+
12+
object voronoi {
13+
private val palette = List("LightCoral", "NavajoWhite", "LemonChiffon",
14+
"PaleGreen", "CornflowerBlue", "Thistle", "Lavender", "#FFB347", "#A05FAB",
15+
"#E7D6B6", "#DE9AA4", "#AFCFAA", "#B3AF9C", "#C1C5C0","#1b85b8",
16+
"#ae5a41", "#c3cb71", "#FFD1DC", "#AEC6CF", "#E7D943", "#A3E743",
17+
"#FDFD96", "#836953", "#779ECB", "#D5B13B", "#66A0A5", "#E4DE12",
18+
"#B94BE4", "#F2DE9C", "#3BBDD1", "#A3E8F3", "#ffff00")
19+
20+
val VoronoiChart = ReactComponentB[Seq[(Double, Double)]]("Voronoi chart")
21+
.render(points => {
22+
val diagram = Voronoi[(Double, Double)](
23+
data = points,
24+
accessor = identity,
25+
width = 460,
26+
height = 400,
27+
xrange = (0, 1),
28+
yrange = (0, 1)
29+
)
30+
31+
val areas = diagram.curves map { curve =>
32+
path(d := curve.line.path.print, stroke := "grey", fill := palette(curve.index))
33+
}
34+
val circles = diagram.nodes map { node =>
35+
val p = node.point
36+
circle(cx := p(0), cy := p(1), r := 3, stroke := "none", fill := "red")
37+
}
38+
39+
svg(width := 460, height := 400,
40+
areas,
41+
circles
42+
)
43+
})
44+
.build
45+
}

0 commit comments

Comments
 (0)