问题描述
我创建了JFreeChart程序,该程序可以:
I create JFreeChart program that can:
- 移动样条线点
- 不允许越过黑色样条线(边界样条线)
- 实时创建新的样条线(作为Grapher)
- 鼠标滚轮缩放
要向数据集中添加新系列,请使用以下功能:
For adding new series to dataset I use this function:
public static XYSeriesCollection createSplineDataset(File[] polFiles) {
dataset = new XYSeriesCollection();
for (File polFile : polFiles) {
XYSeries series = new XYSeries(polFile.getName());
Scanner s = null;
try {
s = new Scanner(new File(polFile.getAbsolutePath()));
}catch (FileNotFoundException ex) {
System.out.println("Scanner error!");
}
s.useLocale(Locale.US);
while (s.hasNext()) {
float x = s.nextFloat();
float y = s.nextFloat();
series.add(x, y);
}
dataset.addSeries(series);
}
return dataset;
}
主程序(有500多个代码字符串,所以这是其中的一部分):
Main program (there 500+ strings of code, so this is part of it):
public class SplineDemo {
// declaration of variables
private static void display(){
final File[] polFiles = new File("FORPLOT").listFiles();
polFiles[0] = new File("FORPLOT/InitPolin1");
polFiles[1] = new File("FORPLOT/InitPolin0");
for (int i = 2; i <= 36; i++)
polFiles[i] = new File("FORPLOT/P"+(i-2));
dataset = JFunc.createSplineDataset(polFiles); // create dataset
// --------some code-----------
NumberAxis domain = new NumberAxis("\u03C1");
NumberAxis range = new NumberAxis("g(\u03C1)");
SplineRenderer r = new SplineRenderer(20);
xyplot = new XYPlot(dataset, domain, range, r);
final XYLineAndShapeRenderer render = (XYLineAndShapeRenderer) xyplot.getRenderer();
render.setBaseShapesVisible(true);
final JFreeChart chart = new JFreeChart(xyplot);
// --------some code-----------
chartPanel = new ChartPanel(chart){
@Override
public Dimension getPreferredSize() {
return new Dimension(640, 480);
}
};
chart.removeLegend();
chartPanel.addMouseListener(new MouseListener() {
//------ for creating new splines and to move points of splines ---------
});
chartPanel.addMouseWheelListener(new MouseWheelListener() {
//--------- zoom ------------
});
chartPanel.addMouseMotionListener(new MotionListener());
chartPanel.addChartMouseListener(new ChartMouseListener() {
//------ for creating new splines and to move points of splines ---------
});
chartPanel.setDomainZoomable(false);
chartPanel.setRangeZoomable(false);
chartPanel.setPopupMenu(null);
frame = new JFrame(Title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(chartPanel);
//------ buttons -------
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.addComponentListener(new ComponentListener() {
@Override
public void componentResized(ComponentEvent ce) {
// ---- to move points when window was resized
}
});
}
public static class MotionListener implements MouseMotionListener {
//------ to move points -----------
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
display();
}
});
}
}
因此, @trashgod 建议在此处修改useBuffer
,但这并没有帮助我.所以,我的问题是,当同时绘制1-5个样条曲线时,理想情况下一切都会快速运行.当它们变得超过30个样条线时(如屏幕截图所示),工作会减速(例如,在移动的情况下,点不在鼠标后面的时间,缩放效果变慢等).问题可以包括什么?这里是YourKit的报告,但我不理解.慢慢地,所有图表的新绘制还是有效?
So, @trashgod adviced here to modify useBuffer
but it didn't help me.So, my problem is that when at there are 1-5 splines at the same time plotted, everything works ideally quickly. When them becomes more than 30 splines as on a screenshot, working is decelerated (for example, points aren't in time behind a mouse in case of moving, zoom works slower, etc.). In what the problem can consist? Here the report from YourKit, but I don't understand it. Slowly the new draw of all diagrams or what works?
我不明白30幅图已经可以如此制动了.如果超过100,会怎样?如果有必要,我可以在zip归档文件中丢弃完整的代码并进行项目
I don't understand how 30 diagrams can already brake so. What will be in case of 100+? If it is necessary, I can throw off a full code and project in zip archive
推荐答案
XYSplineRenderer
"将数据点与自然三次样条连接起来.毫不意外,它的性能可扩展数千点.如果目标是呈现平滑的数据,则进行插值在背景,如此处所建议,并还原为父级XYLineAndShapeRenderer
仅用于呈现
The XYSplineRenderer
"connects data points with natural cubic splines." Not unexpectedly, its performance scales poorly for thousands points. If the goal is to render smoothed data, it may be advantageous to do the interpolation in the background, as suggested here, and revert to the parent XYLineAndShapeRenderer
for rendering only.
此外,每条都有数百个点的数十条曲线可能很难在视觉上区分开.考虑控制相关系列的可见性,此示例中的 中使用JCheckBox
切换单个系列的显示.
In addition, scores of curves, each having hundreds of points, may be difficult to distinguish visually. Consider controlling the visibility of related series, a shown in this example that uses JCheckBox
to toggle the display of individual series.
这篇关于Java Swing-JFreeChart应用程序运行缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!