gpt4 book ai didi

java - 使用 JFreeChart 和 slider 更新图表

转载 作者:搜寻专家 更新时间:2023-10-31 19:56:26 25 4
gpt4 key购买 nike

我有一个描述管道压力的数组的时间历史。所以对于每个 delta t,我沿着管道的长度有一个压力值数组。我想用 JFreeChart 绘制沿管道长度的压力,并选择使用 slider 绘制哪个 delta t,这样每当用户移动 slider 时,图形就会更新为来自不同 delta t 的值。我还将磁贴重置为管道最后部分的压力。发生的事情是标题是更新,这意味着数据正在正确更新,但曲线保持不变。我已经阅读了论坛上所有可能的主题并尝试了我能想到的所有方法,但它不起作用!这是我的扩展 JPanel 的类的代码,其中方法 jSlider1StateChanged 听到 slider 位置的变化,createChart 在程序启动时创建一个新图表,dataSetGen(int ndt) 根据 slider 位置生成图形的新数据集:

public class MyMainPanel extends JPanel {

private JFreeChart jc;
private OutputPipe op;
private DefaultXYDataset ds;
private javax.swing.JFrame jFrame1;
private javax.swing.JSlider jSlider1;
private pipevisualizer.MyChartPanel pnlChartPanel;

private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {
int ndt = ((JSlider) evt.
getSource()).
getValue();
System.out.println("Slider1: " + ((JSlider) evt.
getSource()).
getValue());

dataSetGen(ndt);
int a = 0;
jc.fireChartChanged();
}

private void dataSetGen(int ndt) {
ArrayList<OutputPipeDt> opDtArray = op.getOpLit();
OutputPipeDt opDt = opDtArray.get(ndt);

double[] H = opDt.getH();
double[] l = new double[H.length];
double[] p = new double[H.length];
double dX = op.getPipeLength() / H.length;
double slope = op.getPipeSlope();
double el = op.getPipeUSElev();

for (int i = 0; i < H.length; i++) {
l[i] = dX * i;
p[i] = el - dX * slope * i;
}

double[][] dataH = new double[2][H.length];
dataH[0] = l;
dataH[1] = H;

double[][] dataP = new double[2][H.length];
dataP[0] = l;
dataP[1] = p;

ds = new DefaultXYDataset();
ds.addSeries("pipe head", dataH);
ds.addSeries("pipe profile", dataP);

jc.setTitle("H[end] = " + Double.toString(dataH[1][l.length - 1]));
jc.fireChartChanged();
}

private JFreeChart createChart(OutputPipe op, int ndt) {

ArrayList<OutputPipeDt> opDtArray = op.getOpLit();
OutputPipeDt opDt = opDtArray.get(ndt);

double[] H = opDt.getH();
double[] l = new double[H.length];
double[] p = new double[H.length];
double dX = op.getPipeLength() / H.length;
double slope = op.getPipeSlope();
double el = op.getPipeUSElev();

for (int i = 0; i < H.length; i++) {
l[i] = dX * i;
p[i] = el - dX * slope * i;
}

double[][] dataH = new double[2][H.length];
dataH[0] = l;
dataH[1] = H;

double[][] dataP = new double[2][H.length];
dataP[0] = l;
dataP[1] = p;

DefaultXYDataset ds = new DefaultXYDataset();
ds.addSeries("pipe head", dataH);
ds.addSeries("pipe profile", dataP);


JFreeChart chart = ChartFactory.createXYLineChart(
"t = " + Double.toString(op.getOpLit().get(ndt).getT()),
// chart title
"X",
// x axis label
"Y",
// y axis label
ds,
// data
PlotOrientation.VERTICAL,
true,
// include legend
true,
// tooltips
false // urls
);

return chart;
}
}

我认为对数据集的任何更改都会使图形重新绘制。抱歉,如果代码可能太大而无法在帖子中显示,但我不知道应该粘贴哪些部分才能或多或少地清楚。

最佳答案

如果没有完整的示例,您可以使用显示的方法 here ;它使用 plot.setDataset() 在接收到每个事件时替换数据集。

附录:这个显示温度随时间变化的长度的示例可能会让您入门。

ChartSliderTest

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
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.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
* @see https://stackoverflow.com/a/15207445/230513
*/
public class ChartSliderTest {

private static final int N = 25;
private static final double K = 273.15;
private static final Random random = new Random();

private static XYDataset getDataset(int n) {

final XYSeries series = new XYSeries("Temp (K°)");
double temperature;
for (int length = 0; length < N; length++) {
temperature = K + n * random.nextGaussian();
series.add(length + 1, temperature);
}
return new XYSeriesCollection(series);
}

private static JFreeChart createChart(final XYDataset dataset) {
JFreeChart chart = ChartFactory.createXYLineChart(
"ChartSliderTest", "Length (m)", "Temp (K°)", dataset,
PlotOrientation.VERTICAL, false, false, false);
return chart;
}

private static void display() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final List<XYDataset> list = new ArrayList<XYDataset>();
for (int i = 0; i <= 10; i++) {
list.add(getDataset(i));
}
JFreeChart chart = createChart(list.get(5));
final XYPlot plot = (XYPlot) chart.getPlot();
plot.getRangeAxis().setRangeAboutValue(K, K / 5);
ChartPanel chartPanel = new ChartPanel(chart) {
@Override
public Dimension getPreferredSize() {
return new Dimension(800, 400);
}
};
f.add(chartPanel);
final JSlider slider = new JSlider(0, 10);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
plot.setDataset(list.get(slider.getValue()));
}
});
Box p = new Box(BoxLayout.X_AXIS);
p.add(new JLabel("Time:"));
p.add(slider);
f.add(p, BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

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

关于java - 使用 JFreeChart 和 slider 更新图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15190048/

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