Improving JFreeChart Performance

时间:2016-03-04 18:09:58

标签: java performance jfreechart

I am new to JFreeChart and I am plotting millions of Data of XYDataset but it is plotting in slow motion when it exceeds 50,000 data points. Here i am writing a simple example where an array contains 100 000 values and it takes more than 2 minutes to plot these values. How to improve the performance?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.TimeUnit;

import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
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.XYLineAndShapeRenderer;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.xy.XYDataItem;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;


public class RandomNumberPanel extends JPanel {
    DataConsumer dataConsumer;
    XYDataset dataset;
    XYSeries series1;
    JFreeChart chart;

    public RandomNumberPanel(){//NonblockingCounter nonBlockingCounter,NonblockingCounter tracerForCompareButton,SharedBoolean sharedBoolean, String strategyName, BooleanChecker sleepTime) {

        setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
        dataset = createDataset();
        chart = createChart(dataset);
        chart.removeLegend();
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(500,205));//width height
        this.add(chartPanel,BorderLayout.CENTER);
        this.dataConsumer=new DataConsumer();

    }
    public void startResponsePanel(){
        this.dataConsumer.start();
    }
    private  XYDataset createDataset() {
         series1 = new XYSeries("Test");

        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series1);

        return dataset;
        }
      private JFreeChart createChart(final XYDataset dataset) {
            final JFreeChart result = ChartFactory.createXYLineChart(
                null,
                "Jobs",
                "Random Numbers",
                dataset,
                PlotOrientation.VERTICAL,
                true,
                true,
                false
            );

            result.setTitle("Speed Test");
            final XYPlot plot = result.getXYPlot();
            NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
            rangeAxis.setAutoRange(true);
            ValueAxis xaxis = plot.getDomainAxis();
            xaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
            xaxis.setAutoRange(true);          
            return result;
        }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

         JFrame jframe = new JFrame("Speed Test");
         RandomNumberPanel responsePanel = new RandomNumberPanel();
         jframe.getContentPane().add(responsePanel);

         jframe.setBounds(200, 120, 600, 280);
         jframe.setVisible(true);
         responsePanel.startResponsePanel();
         jframe.addWindowListener(new WindowAdapter() {

                 public void windowClosing(WindowEvent windowevent)
                 {
                         System.exit(0);
                 }

         });


    }

     class DataConsumer extends Thread{
         public DataConsumer () {
            }

            @Override
            public void run() {
                int counter=0;
                final int range=100000;
                //chart.setNotify(false);
            //  chart.setAntiAlias(false);
                int array[]=new int[range];
                for(int i=0;i<range;i++) array[i]= (int)(Math.random()*4+1);
                long time1=System.nanoTime();
                while(counter<range){

                        series1.add(counter,array[counter]);
                        counter++;
                    }
            //  chart.setNotify(true);
                long time2=System.nanoTime();
                JOptionPane.showMessageDialog(null,"Time="+TimeUnit.NANOSECONDS.toSeconds(time2-time1)+" seconds");


                }
            }
     }

0 个答案:

没有答案