gpt4 book ai didi

java - jfreechart xylinechart 排序时间未更新

转载 作者:太空宇宙 更新时间:2023-11-04 15:13:23 25 4
gpt4 key购买 nike

我正在 Java Swing 中制作一个简单的排序基准,我希望它显示随时间变化的图表。但算法运行时图表不会更新。我真的很困惑。有人可以解释一下我做错了什么吗?

当 GUI 运行时,数组会填充随机数据

int A[]=null;int [] copyOfA=null; 

图表

   final XYSeriesCollection dataset = new XYSeriesCollection();
private ChartPanel createDemoPanel() {
JFreeChart jfreechart = ChartFactory.createXYLineChart(title, "array", "time",dataset ,
PlotOrientation.VERTICAL, true, true, false);
jfreechart.setBackgroundPaint(Color.white);
XYPlot plot = (XYPlot) jfreechart.getPlot();
plot.getDomainAxis().setRange(0, 1000); //array
plot.getRangeAxis().setRange(-1,1);//time

//plot.getDomainAxis().setFixedAutoRange(20);
//plot.getRangeAxis().setFixedAutoRange(100);
// render shapes and lines
XYLineAndShapeRenderer renderer =
new XYLineAndShapeRenderer(true, true);
plot.setRenderer(renderer);
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);

return new ChartPanel(jfreechart);
}

ActionPerformed 内部

   private void insSortActionPerformed(java.awt.event.ActionEvent evt) {       
XYSeries series = new XYSeries("ins sort");

if( insSort.isEnabled()) {

long startTime = System.currentTimeMillis();
insSort(A);
long endTime = System.currentTimeMillis();
totalRuntime += (endTime - startTime);
}

series.add(totalRuntime,A.length);
dataset.addSeries(series);
}

最佳答案

在这一行,Y 轴范围设置为 (-1,1) 间隔:

plot.getDomainAxis().setRange(0, 1000); //array
plot.getRangeAxis().setRange(-1,1);//time

但是 Y 轴旨在从 totalRuntime 中获取值,该值是以毫秒为单位给出的时间差:

if( insSort.isEnabled()) {
long startTime = System.currentTimeMillis();
insSort(A);
long endTime = System.currentTimeMillis();
totalRuntime += (endTime - startTime); // this is certainly > 1
}
series.add(totalRuntime,A.length);
dataset.addSeries(series);

这个值肯定> 1(除非你拥有有史以来最强大的计算机:)。因此,您可以查看哪个值正在获取 totalRuntime 并相应地调整 Y 轴范围。

您还可以检查:

  • 域轴范围:如果数组的元素超过 1000 个,那么您也不会看到图形。
  • insSort.isEnabled()实际上是true吗?如果不是这样,那么您将向数据集添加垃圾。

附录

I want to graph sorting time versus the number of elements

你可以看一下下面的例子。由于我没有实现排序方法,因此示例使用 Swing timer 来模拟时间。 。关键是将值正确添加到系列中并设置正确的范围。

注意:由于时间是随机生成的,因此显示的信息是垃圾。该示例试图说明使图形正常工作应遵循的过程。特别注意 fillChart() 方法:它发挥了作用。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class Demo {

private final XYSeries _timeSeries = new XYSeries("Time Series");
boolean _shouldPaint;

private void createAndShowGUI() {

JToggleButton fillChart = new JToggleButton("Fill chart") ;
fillChart.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JToggleButton toggleButton = (JToggleButton)e.getSource();
if(toggleButton.isSelected()) {
_timeSeries.clear();
_shouldPaint = true;
fillChart();
} else {
_shouldPaint = false;
}
}
});

JPanel content = new JPanel(new BorderLayout());
content.add(getFreeChartPanel(), BorderLayout.CENTER);
content.add(fillChart, BorderLayout.SOUTH);

JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(content);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

private JPanel getFreeChartPanel() {
String title = "Time series example";

XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(_timeSeries); // series is added only once!

JFreeChart chart = ChartFactory.createXYLineChart(title, "Array elements", "Time (in millis)", dataset,
PlotOrientation.VERTICAL, true, true, false);
XYPlot plot = chart.getXYPlot();
plot.setRenderer(new XYLineAndShapeRenderer(true, true));
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
plot.getDomainAxis().setRange(0, 1000); // Number of elements - top visible: 1000
plot.getRangeAxis().setRange(0, 4000); // Time employed to do the sort - top visible: 4 seconds (4k millis)

return new ChartPanel(chart);
}

/**
* You should do your own implementation of this method.
*/
private void fillChart() {
Timer timer = new Timer(500, new ActionListener() {

long lastTimeMillis = System.currentTimeMillis();
int domainAxis = 0;

@Override
public void actionPerformed(ActionEvent e) {
if(_shouldPaint) {
long currentTimeMillis = System.currentTimeMillis();
long rangeAxisValue = (long)((currentTimeMillis - lastTimeMillis) * Math.random());
int domainAxisValue = domainAxis + 100;

_timeSeries.add(domainAxisValue, rangeAxisValue);
// Note this is the unique line that has an effect on the graphic

lastTimeMillis = currentTimeMillis;
domainAxis = domainAxisValue;

} else {
Timer timer = (Timer)e.getSource();
timer.stop();
}
}
});

timer.setDelay(1500);
timer.start();
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Demo().createAndShowGUI();
}
});
}
}

图片

enter image description here

关于java - jfreechart xylinechart 排序时间未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21103990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com