我对 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 并且应用程序崩溃。
我想知道的是提高性能的建议。这是我的想法(对此的任何评论/建议/反馈将不胜感激):
将用户限制在要绘制的特定范围内。问题是我不知道 JFreeChart 可以处理多少数据。对此有何建议?我最好不想使用幻数或试错法。
使用可滚动的 XYDataSet。我对此很陌生,对实施没有太多了解。任何示例代码和有关使用此技术的功效的评论都将受到高度赞赏。
我愿意探索新的想法。请让我知道你对这个问题的看法。非常感谢提前!