I am using JFreeChart to plot a line graph. I would like to load arrays as the data set... one array for the x-axis and another array for the y-axis. I am having a problem when trying to pass the array as the data to use in the dataset. The following is what I've tried;
public DefaultCategoryDataset createDataset(int[] epochNo, int[] BFacts)
{
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i<epochNo.length(); i++)
{
dataset.addValue(epochNo[i], BFacts[i]);
}
return dataset;
}
Thanks in advance!
There are two solutions; with a normal line chart, you use a DefaultCategoryDataset
. The addValue
method has three arguments, the second being the name of the dataset. And the third is the column label:
dataset.addValue(BFacts[i], "myline", String.valueOf(epochNo[i]));
There other solution is to see this as an XY line chart (using ChartFactory.createXYLineChart
). In that case your dataset is an XYDataset
instead of a CategoryDataset
:
private XYDataset createDataset(int[] epochNo, int[] bFacts) {
final XYSeries myline = new XYSeries( "myline" );
for (int i = 0; i < epochNo.length; i++) {
myline.add(epochNo[i], bFacts[i]);
}
final XYSeriesCollection dataset = new XYSeriesCollection( );
dataset.addSeries(myline);
return dataset;
}