Oh yeh, I do have a bug in that code
When I fix the bug however i get a slightly different problem.
plot.setRenderer(1, new XYStepRenderer()); works correctly now, but plot.setRenderer(0, new XYStepRenderer()); sets the renderer for both datasets, when I only one it to set the renderer for the first dataset
Code: Select all
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYStepRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class StepChartExample extends JFrame
{
/** Creates a new instance of StepChartExample */
public StepChartExample()
{
//Create Data Sets, each with one series of random data.
XYSeriesCollection dataSetOne = new XYSeriesCollection();
dataSetOne.addSeries(createRandomData("DS1, S1", 100));
XYSeriesCollection dataSetTwo = new XYSeriesCollection();
dataSetTwo.addSeries(createRandomData("DS2, S1", 100));
//Create the chart
JFreeChart chart = ChartFactory.createXYLineChart( "Title",
"X Axis",
"Y Axis",
new XYSeriesCollection(),
PlotOrientation.VERTICAL,
true,
true,
true);
//Get the charts plot
XYPlot plot = chart.getXYPlot();
//Add a second range axis
plot.setRangeAxis(1, new NumberAxis("Y2 Axis"));
//Set the data sets
plot.setDataset(0, dataSetOne);
plot.setDataset(1, dataSetTwo);
//Map the data sets to the axes
plot.mapDatasetToRangeAxis(0,0);
plot.mapDatasetToRangeAxis(1,1);
//THE PROBLEM:
//Either of these two lines will set the
//renderer for both data sets and makes both lines
//red
//plot.setRenderer(new XYStepRenderer());
plot.setRenderer(0, new XYStepRenderer());
//This line works
//plot.setRenderer(1, new XYStepRenderer());
//Create the chart panel
ChartPanel chartPanel = new ChartPanel(chart);
//Do the Swing stuff
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.add(chartPanel);
this.pack();
this.setVisible(true);
}
//Creates a series of random data
public XYSeries createRandomData(String name, int amount)
{
XYSeries series = new XYSeries(name);
for(int i=0; i<amount; i++)
series.add(i, Math.random());
return series;
}
public static void main(String[] args)
{
new StepChartExample();
}
}