Skip to content

Commit a335773

Browse files
Added line charts
1 parent e1290b9 commit a335773

File tree

4 files changed

+499
-4
lines changed

4 files changed

+499
-4
lines changed

src/main/scala/demo/colors.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package demo
22

33
object colors {
4-
case class Color(r: Double, g: Double, b: Double)
4+
case class Color(r: Double, g: Double, b: Double, alpha: Double = 1)
55

66
def cut(x: Double) = x.floor min 255
77

88
def multiply(factor: Double) = { c: Color =>
9-
Color(cut(factor * c.r), cut(factor * c.g), cut(factor * c.b))
9+
Color(cut(factor * c.r), cut(factor * c.g), cut(factor * c.b), c.alpha)
1010
}
1111

1212
def average(c1: Color, c2: Color) =
1313
Color(
1414
cut((c1.r + c2.r) / 2),
1515
cut((c1.g + c2.g) / 2),
16-
cut((c1.b + c2.b) / 2)
16+
cut((c1.b + c2.b) / 2),
17+
(c1.alpha + c2.alpha / 2)
1718
)
1819

1920
val lighten = multiply(1.2)
@@ -34,5 +35,9 @@ object colors {
3435
)
3536
}
3637

37-
def string(c: Color) = s"rgb(${ c.r.floor },${ c.g.floor },${ c.b.floor })"
38+
def transparent(c: Color, alpha: Double = 0.7) = c.copy(alpha = alpha)
39+
40+
def string(c: Color) =
41+
if (c.alpha == 1) s"rgb(${ c.r.floor },${ c.g.floor },${ c.b.floor })"
42+
else s"rgba(${ c.r.floor },${ c.g.floor },${ c.b.floor },${ c.alpha })"
3843
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.Stock
8+
9+
import demo.colors._
10+
11+
12+
object line {
13+
case class Event(date: String, value: Double)
14+
15+
private val palette = mix(Color(130, 140, 210), Color(180, 205, 150))
16+
private val months = List("Jan", "Feb", "Mar", "Apr", "May",
17+
"Jun", "Jul","Aug", "Sep", "Oct", "Nov", "Dec")
18+
19+
private def parseDate(event: Event) = {
20+
val date = new js.Date
21+
val Array(month, year) = event.date split ' '
22+
date.setFullYear(year.toInt)
23+
date.setMonth(months.indexOf(month))
24+
25+
date.getTime
26+
}
27+
28+
val LineChart = ReactComponentB[Seq[Seq[Event]]]("Stock chart")
29+
.render(events => {
30+
val stock = Stock[Event](
31+
data = events,
32+
xaccessor = parseDate,
33+
yaccessor = _.value,
34+
width = 420,
35+
height = 360,
36+
closed = true
37+
)
38+
val lines = stock.curves map { curve =>
39+
g(transform := "translate(50,0)",
40+
path(d := curve.area.path.print, fill := string(transparent(palette(curve.index))), stroke := "none"),
41+
path(d := curve.line.path.print, fill := "none", stroke := string(palette(curve.index)))
42+
)
43+
}
44+
45+
svg(width := 480, height := 400,
46+
lines
47+
)
48+
})
49+
.build
50+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.SmoothLine
8+
9+
import demo.colors._
10+
import line.Event
11+
12+
13+
object smoothline {
14+
private val palette = mix(Color(130, 140, 210), Color(180, 205, 150))
15+
private val months = List("Jan", "Feb", "Mar", "Apr", "May",
16+
"Jun", "Jul","Aug", "Sep", "Oct", "Nov", "Dec")
17+
18+
private def parseDate(event: Event) = {
19+
val date = new js.Date
20+
val Array(month, year) = event.date split ' '
21+
date.setFullYear(year.toInt)
22+
date.setMonth(months.indexOf(month))
23+
24+
date.getTime
25+
}
26+
27+
val SmoothLineChart = ReactComponentB[Seq[Seq[Event]]]("Stock chart")
28+
.render(events => {
29+
val chart = SmoothLine[Event](
30+
data = events,
31+
xaccessor = parseDate,
32+
yaccessor = _.value,
33+
width = 420,
34+
height = 360,
35+
closed = true
36+
)
37+
val lines = chart.curves map { curve =>
38+
g(transform := "translate(50,0)",
39+
path(d := curve.area.path.print, fill := string(transparent(palette(curve.index))), stroke := "none"),
40+
path(d := curve.line.path.print, fill := "none", stroke := string(palette(curve.index)))
41+
)
42+
}
43+
44+
svg(width := 480, height := 400,
45+
lines
46+
)
47+
})
48+
.build
49+
}

0 commit comments

Comments
 (0)