How to create a multi-series (line) chart with scala-chart -
i came across scala-chart, nice wrapper using jfreechart in scala. has several utility classes generating charts minimal effort.
what best way generate line chart multiple series using scala-chart?
in scala-chart
there several different ways create multi-series line chart. way use depends on how create dataset (including ways work legacy jfreechart code):
(for comparison) create single-series line chart:
val series = (x <- 1 5) yield (x,x*x) val chart = xylinechart(series)
build multi-series line chart entirely scala collections (this way recommend because idiomatic):
val names: list[string] = "series a" :: "series b" :: nil val data = { name <- names series = (x <- 1 5) yield (x,util.random.nextint(5)) } yield name -> series val chart = xylinechart(data)
from collection of
xyseries
objects:val names: list[string] = "series a" :: "series b" :: nil def randomseries(name: string): xyseries = list.tabulate(5)(x => (x,util.random.nextint(5))).toxyseries(name) val data = (name <- names) yield randomseries(name) val chart = xylinechart(data)
explicitly create
xyseriescollection
object:def data: xyseriescollection = ??? val chart = xylinechart(data)
these simple snippets should illustrate how data creation possible , of time boils down 1 of these ways.
the current implementation (as of 0.4.0) work in way that:
- all chart factories accept arbitrary
data
object of typea
and - in addition accept implicit -- type class
toxydataset[a]
-- converts arbitrarydata
of typea
respective dataset factory based on - in case of xy charts there default converters
xyseries
,xyseriescollection
,coll[xyseries]
(where coll scala standard library collection),coll[(a,b)]
,coll[(a,coll[(b,c)])]
this way chart factories extendable , can used data
instances of custom types -- have write own conversion type class instances custom types.
Comments
Post a Comment