性能问题 - JFreeChart(XYDataSet)

时间:2012-07-02 14:19:37

标签: java performance jfreechart scatter-plot

我对JFreeChart很陌生,在尝试渲染庞大的数据集时遇到了性能问题。我的场景中的数据集来自一个csv文件,该文件具有超过46700行(行)和大约2666900+个数据点。

我目前的绘图代码如下:

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();
    }
}

由于这个巨大的数据集,绘图需要花费大量时间来加载并尝试更改绘图窗口的大小会引发OutOfMemoryError并且应用程序崩溃。

我想知道的是提高绩效的建议。这就是我的想法(任何评论/建议/反馈都会非常感激):

  1. 将用户限制在特定范围内进行绘图。问题是我不知道JFreeChart可以处理多少数据。有什么建议吗?我最好不要使用幻数或只是试错法。

  2. 使用可滚动的XYDataSet。我对此非常陌生,对实现并不太了解。任何示例代码和对使用此技术的功效的评论都将受到高度赞赏。

  3. 我愿意探索新的想法。请告诉我您对此问题的看法。非常感谢提前!

1 个答案:

答案 0 :(得分:1)

对于~10 6 数据点,FastScatterPlot是一个不错的选择。对于更大的数字,您将不得不进行测试。对于速度,它使用内部render()方法,而不是插件渲染器。另外,请考虑使用SwingWorker在读取数据时逐步更新图表。