Update graph with JFreeChart and slider

后端 未结 1 1693
温柔的废话
温柔的废话 2020-12-06 20:02

I have a time history for arrays describing the pressure along a pipe. So I have an array of pressure values along the length of the pipe for each delta t. I want to plot th

相关标签:
1条回答
  • 2020-12-06 20:25

    Absent a complete example, you may be able to use the approach shown here; it uses plot.setDataset() to replace the dataset on receiving each event.

    Addendum: This example that shows temperature versus length over time may get you started.

    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();
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题