Please let me know how I can make a program for JFreeChart
in a JFrame
. There are two JFreeCharts
and both are visible, but only one is running; the other is still, why it is happening? Is there any sequential mistake in my code or what?
If you explain or give any hint, I shall be thankful to you.
Please read carefully and you can run on your laptop/PC, it is running code.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.Timer;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.DynamicTimeSeriesCollection;
import org.jfree.data.time.Second;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
/**
* @see http://stackoverflow.com/a/15521956/230513
* @see http://stackoverflow.com/questions/5048852
*/
public class Grid_Layout extends ApplicationFrame {
private static final long serialVersionUID = 1L;
private static final String TITLE1 = "X-Axis";
private static final String TITLE2 = "Y-Axis";
private static final String TITLE3 = "Z-Axis";
private static final String START = "Start";
private static final String STOP = "Stop";
private static final float MINMAX = 100;
private static final int COUNT = 2 * 60;
private static final int FAST = 100;
private static final int SLOW = FAST * 5;
private static final Random random = new Random();
private static final Random random2 = new Random();
private Timer timer;
private Timer timer2;
public Grid_Layout(final String title) {
super(title);
final XYDataset dataset = createDataset1();
JFreeChart chart = createChart(dataset);
final XYDataset dataset2 = createDataset2();
JFreeChart chart2 = createChart2(dataset2);
final JButton run = new JButton(STOP);
run.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (STOP.equals(cmd)) {
timer.stop();
run.setText(START);
} else {
timer.start();
run.setText(STOP);
}
}
});
final JComboBox<String> combo = new JComboBox<String>();
combo.addItem("Fast");
combo.addItem("Slow");
combo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("Fast".equals(combo.getSelectedItem())) {
timer.setDelay(FAST);
} else {
timer.setDelay(SLOW);
}
}
});
this.add(new ChartPanel(chart) {
private static final long serialVersionUID = 1L;
@Override
public Dimension getPreferredSize() {
return new Dimension(640, 480);
}
}, BorderLayout.CENTER);
this.add(new ChartPanel(chart2) {
private static final long serialVersionUID = 1L;
@Override
public Dimension getPreferredSize() {
return new Dimension(640, 480);
}
}, BorderLayout.CENTER);
JPanel btnPanel = new JPanel(new FlowLayout());
btnPanel.add(run);
btnPanel.add(combo);
this.add(btnPanel);
}
private float randomValue() {
return (float) (random.nextGaussian() * MINMAX / 3);
}
private float[] gaussianData() {
float[] a = new float[COUNT];
for (int i = 0; i < a.length; i ) {
a[i] = randomValue();
}
return a;
}
private JFreeChart createChart(final XYDataset dataset) {
final JFreeChart result = ChartFactory.createTimeSeriesChart(TITLE1, "Time (seconds)", "Intensity (cm/s)",
dataset, true, true, false);
final XYPlot plot = result.getXYPlot();
ValueAxis domain = plot.getDomainAxis();
domain.setAutoRange(true);
ValueAxis range = plot.getRangeAxis();
range.setRange(-MINMAX, MINMAX);
range.setAutoRange(true);
return result;
}
private JFreeChart createChart2(final XYDataset dataset) {
final JFreeChart result = ChartFactory.createTimeSeriesChart(TITLE2, "Time (seconds)", "Intensity (cm/s)",
dataset, true, true, false);
final XYPlot plot = result.getXYPlot();
ValueAxis domain = plot.getDomainAxis();
domain.setAutoRange(true);
ValueAxis range = plot.getRangeAxis();
range.setRange(-MINMAX, MINMAX);
range.setAutoRange(true);
return result;
}
public void start() {
timer.start();
}
private XYDataset createDataset1() {
final DynamicTimeSeriesCollection dataset = new DynamicTimeSeriesCollection(1, COUNT, new Second());
dataset.setTimeBase(new Second(0, 0, 0, 24, 5, 2022));
dataset.addSeries(gaussianData(), 0, "X");
timer = new Timer(FAST, new ActionListener() {
float[] newData = new float[1];
@Override
public void actionPerformed(ActionEvent e) {
newData[0] = randomValue();
dataset.advanceTime();
dataset.appendData(newData);
}
});
return dataset;
}
private XYDataset createDataset2() {
final DynamicTimeSeriesCollection dataset = new DynamicTimeSeriesCollection(1, COUNT, new Second());
dataset.setTimeBase(new Second(0, 0, 0, 26, 5, 2022));
dataset.addSeries(gaussianData(), 0, "Y");
timer2 = new Timer(FAST, new ActionListener() {
float[] newData2 = new float[1];
@Override
public void actionPerformed(ActionEvent e) {
newData2[0] = randomValue();
dataset.advanceTime();
dataset.appendData(newData2);
}
});
return dataset;
}
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
Grid_Layout demo = new Grid_Layout("Vibration Analyser");
demo.setLayout(new GridLayout(3, 1));
demo.pack();
demo.setLocationRelativeTo(null);
demo.setVisible(true);
demo.start();
}
});
}
}
CodePudding user response:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.Timer;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.DynamicTimeSeriesCollection;
import org.jfree.data.time.Second;
import org.jfree.data.xy.XYDataset;
import org.jfree.chart.ui.ApplicationFrame;
/**
* @see https://stackoverflow.com/a/72411813/230513
* @see http://stackoverflow.com/a/15521956/230513
* @see http://stackoverflow.com/questions/5048852
*/
public final class Grid_Layout extends ApplicationFrame {
private static final int CHART_WIDTH = 600;
private static final int CHART_HEIGHT = 300;
private static final String TITLE1 = "X-Axis";
private static final String TITLE2 = "Y-Axis";
private static final String TITLE3 = "Z-Axis";
private static final String START = "Start";
private static final String STOP = "Stop";
private static final float MINMAX = 100;
private static final int COUNT = 2 * 60;
private static final int FAST = 100;
private static final int SLOW = FAST * 5;
private static final Random random = new Random();
private Timer timer;
private Timer timer2;
private static class GridChart extends ChartPanel {
public GridChart(JFreeChart chart) {
super(chart);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(CHART_WIDTH, CHART_HEIGHT);
}
}
public Grid_Layout(final String title) {
super(title);
final XYDataset dataset = createDataset1();
JFreeChart chart = createChart(dataset);
final XYDataset dataset2 = createDataset2();
JFreeChart chart2 = createChart2(dataset2);
final JButton run = new JButton(STOP);
run.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (STOP.equals(cmd)) {
timer.stop();
timer2.stop();
run.setText(START);
} else {
timer.start();
timer2.start();
run.setText(STOP);
}
}
});
final JComboBox<String> combo = new JComboBox<String>();
combo.addItem("Fast");
combo.addItem("Slow");
combo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("Fast".equals(combo.getSelectedItem())) {
timer.setDelay(FAST);
timer2.setDelay(FAST);
} else {
timer.setDelay(SLOW);
timer2.setDelay(SLOW);
}
}
});
JPanel gridPanel = new JPanel(new GridLayout(0, 1));
gridPanel.add(new GridChart(chart));
gridPanel.add(new GridChart(chart2));
JPanel btnPanel = new JPanel(new FlowLayout());
btnPanel.add(run);
btnPanel.add(combo);
this.add(gridPanel);
this.add(btnPanel, BorderLayout.SOUTH);
}
private float randomValue() {
return (float) (random.nextGaussian() * MINMAX / 3);
}
private float[] gaussianData() {
float[] a = new float[COUNT];
for (int i = 0; i < a.length; i ) {
a[i] = randomValue();
}
return a;
}
private JFreeChart createChart(final XYDataset dataset) {
final JFreeChart result = ChartFactory.createTimeSeriesChart(TITLE1, "Time (seconds)", "Intensity (cm/s)",
dataset, true, true, false);
final XYPlot plot = result.getXYPlot();
ValueAxis domain = plot.getDomainAxis();
domain.setAutoRange(true);
ValueAxis range = plot.getRangeAxis();
range.setRange(-MINMAX, MINMAX);
range.setAutoRange(true);
return result;
}
private JFreeChart createChart2(final XYDataset dataset) {
final JFreeChart result = ChartFactory.createTimeSeriesChart(TITLE2, "Time (seconds)", "Intensity (cm/s)",
dataset, true, true, false);
final XYPlot plot = result.getXYPlot();
ValueAxis domain = plot.getDomainAxis();
domain.setAutoRange(true);
ValueAxis range = plot.getRangeAxis();
range.setRange(-MINMAX, MINMAX);
range.setAutoRange(true);
return result;
}
public void start() {
timer.start();
timer2.start();
}
private XYDataset createDataset1() {
final DynamicTimeSeriesCollection dataset = new DynamicTimeSeriesCollection(1, COUNT, new Second());
dataset.setTimeBase(new Second(0, 0, 0, 24, 5, 2022));
dataset.addSeries(gaussianData(), 0, "X");
timer = new Timer(FAST, new ActionListener() {
float[] newData = new float[1];
@Override
public void actionPerformed(ActionEvent e) {
newData[0] = randomValue();
dataset.advanceTime();
dataset.appendData(newData);
}
});
return dataset;
}
private XYDataset createDataset2() {
final DynamicTimeSeriesCollection dataset = new DynamicTimeSeriesCollection(1, COUNT, new Second());
dataset.setTimeBase(new Second(0, 0, 0, 26, 5, 2022));
dataset.addSeries(gaussianData(), 0, "Y");
timer2 = new Timer(FAST, new ActionListener() {
float[] newData2 = new float[1];
@Override
public void actionPerformed(ActionEvent e) {
newData2[0] = randomValue();
dataset.advanceTime();
dataset.appendData(newData2);
}
});
return dataset;
}
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
Grid_Layout demo = new Grid_Layout("Vibration Analyser");
demo.pack();
demo.setLocationRelativeTo(null);
demo.setVisible(true);
demo.start();
}
});
}
}