Reputation: 63
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);
Upvotes: 0
Views: 745
Reputation: 205875
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.
Upvotes: 3