Reputation: 97
I made a method to generate graphic with JfreeChart. I have more than 2500 value to plot in my scatter graphic but it takes a lot of time for that. So how can we resolve that ?
this a piece of code :
public NuagePointsFact(){
LectureFichierExcel lfe = new LectureFichierExcel("data/requete1Test.xls");
this.series = new TimeSeries[lfe.findDate().size()];
this.dataset = new TimeSeriesCollection();
for(int i=0; i<lfe.findDate().size(); i++){
this.series [i] = new TimeSeries(i, Day.class);
series[i].add(new Day(new Date(lfe.findDate().get(i))), lfe.findValues().get(i));
dataset.addSeries(series[i]);
}
.....
Thank you.
Upvotes: 2
Views: 928
Reputation: 11
To made JFreechart to works good. Before set series of data set: axisR.setAutoRange(false); axisH.setAutoRange(false); It as som thet:
JFreeChart chart1 = ChartFactory.createXYLineChart(null, null, "Level", dataset1);
XYPlot plot1 = chart1.getXYPlot();
axisR = (NumberAxis)plot.getRangeAxis();
axisH = (NumberAxis)plot.getDomainAxis();
axisR.setAutoRange(false);
axisH.setAutoRange(false);
after data setting
axisR.setAutoRange(true);
axisH.setAutoRange(true);
Upvotes: 1
Reputation: 5923
Are you creating the chart before are after adding the data to the TimeSeries
? If you are adding data after the chart has been created/shown then the plot
will redraw after each point has been added.
Consider this example:
import java.text.SimpleDateFormat;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeriesDataItem;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class TimeSeriesDemo1 extends ApplicationFrame {
private static TimeSeries s1;
private static boolean loadFirst;
public TimeSeriesDemo1(String title) {
super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
if (!loadFirst){
Runnable task = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {}
int count = 0;
for ( int year = 2000 ; year < 2010 ; year++){
for (int month = 1 ; month < 13 ; month++){
for (int day = 1 ; day < 29 ; day++){
final TimeSeriesDataItem di = new TimeSeriesDataItem(new Day(day, month, year), Math.random() * 20);
count++;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
s1.add(di);
}
});
}
}
}
System.out.println("After the chart is created " + count);
}
};
Thread th = new Thread(task,"Load Data"){
};
th.start();
}
}
private static JFreeChart createChart(XYDataset dataset) {
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Large Dataset Demo", // title
"Date", // x-axis label
"Value", // y-axis label
dataset, // data
true, // create legend?
true, // generate tooltips?
false // generate URLs?
);
XYPlot plot = (XYPlot) chart.getPlot();
DateAxis domainAaxis = (DateAxis) plot.getDomainAxis();
domainAaxis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
return chart;
}
private static XYDataset createDataset() {
s1 = new TimeSeries("Random Data");
int count = 0;
if (loadFirst){
for ( int year = 2000 ; year < 2010 ; year++){
for (int month = 1 ; month < 13 ; month++){
for (int day = 1 ; day < 29 ; day++){
final TimeSeriesDataItem di = new TimeSeriesDataItem(new Day(day, month, year), Math.random() * 20);
count++;
s1.add(di);
}
}
}
System.out.println("Before the chart is created " + count);
}
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(s1);
return dataset;
}
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
return new ChartPanel(chart);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
loadFirst = true;
TimeSeriesDemo1 demo = new TimeSeriesDemo1("Large Time Series Demo ");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
});
}
}
If you set loadFirst = true
the chart adds about 3k data points before it's drawn on the screen.
Set loadFirst = false
and it will add them afterwards taking much longer.
You shold be able to see the differance in performance. It's not clear from you example which method you are using.
If you are showing the chart before loading the data then try swithcing the order if possible.
Upvotes: 2
Reputation: 32391
I have just a few suggestions, you will have to try them: you could add data to your series in bunches of 100 values or so. You will have to use a separate thread to add to the model and then repaint the chart, wait for a while and add another bunch and so on.
However, I have used JFreeChart in the past and I think it was working decently with more than just 2500 values.
Upvotes: 1