1

I am creating an XYPlot with initially null dataset, and then add multiple (e.g. 2) XYSeriesCollection instances to it. Like

val chart = ChartFactory.createXYLineChart(...)
val plot = chart.getXYPlot
plot.setDataset(0, dataset0)
plot.setDataset(1, dataset1)

Now I want them to have them in different colors and strokes:

import BasicStroke._
val renderer = plot.getRenderer
renderer.setSeriesPaint(0, Color.black)
renderer.setSeriesPaint(1, Color.red)
renderer.setSeriesStroke(0, new BasicStroke(2.0f))
renderer.setSeriesStroke(1, 
  new BasicStroke(2.0f, CAP_ROUND, JOIN_ROUND, 1.0f, Array(6f, 6f), 0f))
)

But both appear in black and non-dashed. So I must be making a mistake in terms of understanding the correspondence between datasets and series?


I also tried with plot.getRendererForDataset(dataset), but again, both datasets are controlled by the settings for series 0, while the renderer settings for series 1 seem to be irrelevant.

0__
  • 66,707
  • 21
  • 171
  • 266

1 Answers1

1

I don't know what the purpose of plot.setDataset(idx, _) is, but the XYSeriesCollection itself needs to have the different series included.

val series: Seq[XYSeries] = ...
val dataset = new XYSeriesCollection
series.foreach(dataset.addSeries _)
val chart = ChartFactory.createXYLineChart("title", "x", "y", dataset, 
  PlotOrientation.VERTICAL, true, false, false)

val plot      = chart.getXYPlot
val renderer  = plot.getRenderer

renderer.setSeriesPaint (0, paint0 )
renderer.setSeriesStroke(0, stroke0)
renderer.setSeriesPaint (1, paint1 )
renderer.setSeriesStroke(1, stroke1)
...   
0__
  • 66,707
  • 21
  • 171
  • 266