How to update Scatter Plot as the new value comes

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
kalu
Posts: 1
Joined: Mon Jan 15, 2018 4:39 pm
antibot: No, of course not.

How to update Scatter Plot as the new value comes

Post by kalu » Mon Jan 15, 2018 4:49 pm

Hi, I want to update the value of scatter plot of below examples. But i am not able to do it. Please see the code and suggest me how to do it. Thanks in advance for the help.

public class EnergyTest extends JFrame{

private static final long serialVersionUID = 6294689542092367723L;

public EnergyTest(String title) {
super(title);

// Create dataset
XYDataset dataset = createDataset();

// Create chart
JFreeChart chart = ChartFactory.createScatterPlot(
"Boys VS Girls weight comparison chart",
"X-Axis", "Y-Axis", dataset);


//Changes background color
XYPlot plot = (XYPlot)chart.getPlot();
plot.setBackgroundPaint(new Color(255,228,196));


// Create Panel
ChartPanel panel = new ChartPanel(chart);
setContentPane(panel);
}
double time = 0;
private XYDataset createDataset(){
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series1 = new XYSeries("Boys");
Components components = JSensors.get.components();
List<Cpu> cpus = components.cpus;
if (cpus != null) {
for (final Cpu cpu : cpus) {
if (cpu.sensors != null) {
List<Temperature> temps = cpu.sensors.temperatures;
restart:
for (final Temperature temp : temps) {
switch(temp.name) {
case("Temp CPU Core #1") :
series1.add(++time, temp.value);
case("Temp CPU Core #2") :
continue restart;
case("Temp CPU Package") :
continue restart;
}
}
}
}
}
dataset.addSeries(series1);
return dataset;

}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
EnergyTest example = new EnergyTest("Scatter Chart Example | BORAJI.COM");
example.setSize(800, 400);
example.setLocationRelativeTo(null);
example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
example.setVisible(true);
});
}
}

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: How to update Scatter Plot as the new value comes

Post by John Matthews » Tue Jan 16, 2018 12:22 am

In this complete example, a button's ActionListener updates an XYSeries named added. The listening plot updates itself in response.

Locked