Java – different series of xyline charts in different colors JfreeChart

I have created an XY line graph using JfreeChart. There are two data sets. I want both rows to be in different colors I try to use the following code –

XYPlot plot = chart.getXYPlot();
  XYItemRenderer xyir = plot.getRenderer();
  xyir.setSeriesPaint(0,Color.GREEN);
  plot.setDataset(0,xyDataset1);

  xyir.setSeriesPaint(1,Color.blue);
  plot.setDataset(1,xyDataset2);

I also try to use the following code. I use different renderers (I don't know if this is the correct method)

XYPlot plot1 = chart.getXYPlot();
  XYPlot plot2 = chart.getXYPlot();

  XYItemRenderer xyir1 = plot1.getRenderer();
  xyir1.setSeriesPaint(0,Color.GREEN);
  plot1.setDataset(0,xyDataset1);

  XYItemRenderer xyir2 = plot2.getRenderer();
  xyir2.setSeriesPaint(1,Color.blue);
  plot2.setDataset(1,xyDataset2);

In both cases, they are printed in blue What's up? Any suggestions??

Solution

Find the solution, it works for me, using two different renderers, which I didn't do well in the early days –

XYPlot plot = chart.getXYPlot();
  plot.setDataset(0,xyDataset1);
  plot.setDataset(1,xyDataset2);
  XYLineAndShapeRenderer renderer0 = new XYLineAndShapeRenderer(); 
  XYLineAndShapeRenderer renderer1 = new XYLineAndShapeRenderer(); 
  plot.setRenderer(0,renderer0); 
  plot.setRenderer(1,renderer1); 
  plot.getRendererForDataset(plot.getDataset(0)).setSeriesPaint(0,Color.red); 
  plot.getRendererForDataset(plot.getDataset(1)).setSeriesPaint(0,Color.blue);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>