I am new to jfreechart and i am having trouble plotting the values in sequence. i have series of numbers with each one having more than 6 decimal values example:(5.981412023,2636.51824) and varying from +6.00000000 to -6.00000000. The values are taken from excel sheet and it has a graph(xy scatter type), which doesn't look similar to the one created by jfreechart. I found out that the values are not plotted in the sequence which i gave but its arranged and plotted. example: if i gave the sequence like (8,6,2,9,4,7,5), the plot is arranged and then the line looks like its plotted to this arranged sequence like (2,4,5,6,7,8,9).
Is there any way i can plot it in a sequence of the plots inserted ? sorry about my English.
Example:
package test;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
public class PlotTest {
private XYSeriesCollection dataset;
public static void main (String[] args) {
new PlotTest();
}
public PlotTest () {
dataset = new XYSeriesCollection();
XYSeries data = new XYSeries("data");
data.add(3, 2); //Point 1
data.add(1, 1); //Point 2
data.add(4, 1); //Point 3
data.add(2, 2); //Point 4
dataset.addSeries(data);
showGraph();
}
private void showGraph() {
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
final ApplicationFrame frame = new ApplicationFrame("Title");
frame.setContentPane(chartPanel);
frame.pack();
frame.setVisible(true);
}
private JFreeChart createChart(final XYDataset dataset) {
final JFreeChart chart = ChartFactory.createScatterPlot(
"Title", // chart title
"X", // x axis label
"Y", // y axis label
dataset, // data
PlotOrientation.VERTICAL,
true, // include legend
true, // tooltips
false // urls
);
XYPlot plot = (XYPlot) chart.getPlot();
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesLinesVisible(0, true);
plot.setRenderer(renderer);
return chart;
}
}
Based on the above example, I want the program to connect the dots in order 1-2-3-4, which is the order I added them to my dataset. But I do get them connected in order 2-4-1-3, sorted by x-value.
XYSeries
constructor withautoSort
set totrue
. – trashgod