Chart update using JFreeChart and slider

I have a timeline for arrays describing pressure along a pipe. Therefore, I have an array of pressure values ​​along the length of the pipe for each delta t. I want to build pressure along the length of the pipe using JFreeChart and choose which delta t will be built using the slider, so that whenever the user moves the slider, the image file is updated with values ​​from another delta t. I also return the plate as pressure on the last part of the pipe. What happens is that the header is an update, that is, the data is updated correctly, but the curve remains unchanged. I read all the possible topics on the forums and tried everything I could, but that doesn't work! Here is the code of my class that extends JPanel, in which the jSlider1StateChanged method hears the slider changing position,createChart creates a new chart when the program starts, and dataSetGen (int ndt) generates a graph of a new data set based on the position of the 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;
  }
}

, . , , , , , .

+5
1

, ; 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();
            }
        });
    }
}
+6

All Articles