Sujay
Sujay

Reputation: 6783

Performance Issue - JFreeChart (XYDataSet)

I'm pretty much new to JFreeChart and am facing a performance issue while trying to render a huge dataset. The dataset in my scenario comes from a csv file which has well over 46700 rows (lines) and around 2666900+ data points to plot.

My current code for plotting is as follows:

public  void doPlot(String plotTitle, HashMap<Integer, ArrayList<PlotCheckBox>> plotDataList, boolean dotOnly) {

    plotDialog.dispose();

    DefaultXYDataset xyDataSet;
    try {
        if (plotDataList.isEmpty()) {
            return;
        }

        xyDataSet = new DefaultXYDataset();
        /**
         * Populate Data Set
         */
        boolean[] optionList;
        int countPlotPoints = 0;

        for (Integer tabIndex : plotDataList.keySet()) {
            ArrayList<PlotCheckBox> checkBoxList = plotDataList.get(tabIndex);
            try {
                if (checkBoxList == null) {
                    continue;
                } else if (checkBoxList.isEmpty()) {
                    continue;
                }

                optionList = new boolean[checkBoxList.size()];
                for (int index = 0; index < checkBoxList.size(); index++) {
                    optionList[index] = checkBoxList.get(index).isSelectedForPlot();
                }

                /**
                * The getTabXYData() method builds up the xyDataSet by loading values from a given csv file and the 
                * attributes chosen by the user to plot
                */
                countPlotPoints += getTabXYData(xyDataSet, tabIndex, optionList, jTabbedPane_results.getTitleAt(tabIndex));
            } finally {
                checkBoxList = null;
                optionList = null;
            }
        }

        System.out.println("Plot Points In This Graph: "+countPlotPoints);

        if (countPlotPoints == 0) {
            print("No options selected.\n");
            JOptionPane.showMessageDialog(this, "No Plot Points Were Selected!", "Warning", JOptionPane.WARNING_MESSAGE);
            return;
        }

        JFreeChart chart = ChartFactory.createXYLineChart(plotTitle, "time(ms)", "---", xyDataSet, PlotOrientation.VERTICAL, true, true, false);
        XYPlot plot = (XYPlot) chart.getPlot();
        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();

        for (int i = 0; i < countPlotPoints; i++) {
            if (dotOnly) {
                renderer.setSeriesLinesVisible(i, false);
            } else {
                renderer.setSeriesLinesVisible(i, true);
            }
            renderer.setSeriesShapesVisible(i, true);
        }
        plot.setRenderer(renderer);


        ChartPanel chartpanel = new ChartPanel(chart);
        chartpanel.setDefaultDirectoryForSaveAs(new File(lastAnalyzedPath));

        JFrame frame = new JFrame();
        frame.setTitle(plotTitle);
        frame.add(new JScrollPane(chartpanel));
        frame.pack();
        frame.setVisible(true);

        frame.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public  void windowClosing(java.awt.event.WindowEvent evt) {
                try {
                    System.out.println(":: Clearning Memory ::");
                    System.out.println("\tFree Memory (Before cleanup): "+Runtime.getRuntime().freeMemory());
                    Component component = getComponent(0);
                    if(component instanceof ChartPanel){
                        JFreeChart chart = ((ChartPanel) component).getChart();
                        XYPlot plot = (XYPlot) chart.getPlot();
                        plot        = null;
                        chart       = null;
                        component   = null;
                    }
                } finally {
                    System.runFinalization();
                    System.gc();
                    System.out.println("\tFree Memory (Post cleanup): "+Runtime.getRuntime().freeMemory());
                }
            }
        });

    } finally {
        xyDataSet = null;
        System.runFinalization();
        System.gc();
    }
}

Because of this huge dataset, the plot takes a lot of time to load and trying to change the size of the plot window throws an OutOfMemoryError and the application crashes.

What I would like to know are suggestions for improving performance. Here's what I've thought of (any comments/suggestions/feedback on this would really be appreciated):

  1. Restricting users to a specific range to plot. Problem is I don't know how much of data can JFreeChart handle. Any suggestion on this? I would preferably won't want to use a magic number or just a trial and error method.

  2. Use a scrollable XYDataSet. I'm pretty much new to this and don't have much idea about the implementation. Any sample code and a comment on efficacy of using this technique would be highly appreciated.

I'm open to exploring new ideas. Please do let me know what you think about this issue. Many a thanks in advance!

Upvotes: 1

Views: 1708

Answers (1)

trashgod
trashgod

Reputation: 205805

For ~106 data points, FastScatterPlot is a good choice. For larger numbers, you'll have to test. For speed, it uses an internal render() method, instead of a plug-in renderer. Also consider using a SwingWorker to update the chart incrementally as data is read.

Upvotes: 1

Related Questions