0

I have two XYSeries that belong to a same dataset. First series has to show shapes only, while second series has to show lines only. Both must have the same color. Is there a way to do it ?

Providing the piece of code to better understand:

XYSeries series = new XYSeries("S1", false); // autosort disabled
    for(int i = 0; i < xValues.length; ++i)
        series.add(xValues[i], yValues[i]);

    XYSeries series1 = new XYSeries("S2", false);
    for(int i = 0; i < xValues.length; ++i)
        series1.add(yValues[i], xValues[i]);

    XYSeriesCollection dataset = new XYSeriesCollection();

    dataset.addSeries(series);

    dataset.addSeries(series1);

    // create a chart with title, axis labels, tooltips and maybe a legend
    JFreeChart chart = ChartFactory.createScatterPlot(title, xLabel, yLabel, dataset, 
            PlotOrientation.VERTICAL, legend, true, false);

    XYPlot plot = (XYPlot) chart.getPlot();
    XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();

    renderer.setSeriesLinesVisible(0, true);
    renderer.setSeriesShapesVisible(1, false);
    renderer.setSeriesLinesVisible(1, true);
Jacopo
  • 63
  • 7

1 Answers1

3

Based on this example, I edited the following lines to get the image below.

XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) xyPlot.getRenderer();
renderer.setSeriesLinesVisible(1, true);
renderer.setSeriesShapesVisible(1, false);
renderer.setSeriesPaint(1, Color.blue);

Addendum: To get pairs of matching colors, you can override getItemPaint() as shown here and here.

enter image description here

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • [Updated](http://stackoverflow.com/posts/9763985/revisions) to use same color for both series. – trashgod Mar 19 '12 at 01:49
  • Thanks. I omitted something, though. In my chart there can be lots of series, each of them with two series. So each dataset has a single color, provided automatically by the library. – Jacopo Mar 19 '12 at 12:02
  • You can override `getItemPaint()`; I've linked to examples above. – trashgod Mar 19 '12 at 14:42