Reputation: 2658
I came across Scala-chart, a really nice wrapper for using jFreeChart in Scala. It has several utility classes for generating charts with minimal effort.
What is the best way to generate a line chart with multiple series using scala-chart?
Upvotes: 2
Views: 1377
Reputation: 508
In scala-chart
there are several different ways to create a multi-series line chart. Which way to use depends on how you create your dataset (including ways to work with legacy JFreeChart code):
(for comparison) create a single-series line chart:
val series = for (x <- 1 to 5) yield (x,x*x)
val chart = XYLineChart(series)
build up a multi-series line chart entirely from scala collections (this way I recommend because it is the most idiomatic):
val names: List[String] = "Series A" :: "Series B" :: Nil
val data = for {
name <- names
series = for (x <- 1 to 5) yield (x,util.Random.nextInt(5))
} yield name -> series
val chart = XYLineChart(data)
from a 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 = for (name <- names) yield randomSeries(name)
val chart = XYLineChart(data)
explicitly create an XYSeriesCollection
object:
def data: XYSeriesCollection = ???
val chart = XYLineChart(data)
These are some simple snippets but they should illustrate how data creation is possible and most of the time it boils down to one of these ways.
The current implementation (as of 0.4.0) work in the way that:
data
object of type A
andToXYDataset[A]
-- which converts this arbitrary data
of type A
to the respective dataset the factory is based onXYSeries
, XYSeriesCollection
, Coll[XYSeries]
(where coll is a scala standard library collection), Coll[(A,B)]
and Coll[(A,Coll[(B,C)])]
This way the chart factories are fully extendable and can be used with data
instances of your custom types -- you just have to write your own conversion type class instances for your custom types.
Upvotes: 4