Jfreechart make a dynamic scrollable chart [duplicate] - java

How can I use JFreeChart to display just the most recent data in a continually updated time series?
Addenda: A complete, working example that incorporates the accepted answer is shown here. See also this variation having two series. See also this Q&A regarding setTimeBase().

The JFreeChart class DynamicTimeSeriesCollection is a good choice.
Addendum: As noted by #Bahadır, the last point of the series was persistently zero. #Don helpfully suggests advancing the time and then appending the data.
dataset.advanceTime();
dataset.appendData(newData);
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
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;
import org.jfree.chart.ui.UIUtils;
/**
* #see http://stackoverflow.com/a/15521956/230513
* #see http://stackoverflow.com/questions/5048852
*/
public class DTSCTest extends ApplicationFrame {
private static final String TITLE = "Dynamic Series";
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;
public DTSCTest(final String title) {
super(title);
final DynamicTimeSeriesCollection dataset =
new DynamicTimeSeriesCollection(1, COUNT, new Second());
dataset.setTimeBase(new Second(0, 0, 0, 1, 1, 2011));
dataset.addSeries(gaussianData(), 0, "Gaussian data");
JFreeChart chart = createChart(dataset);
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 combo = new JComboBox();
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) {
#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, BorderLayout.SOUTH);
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);
}
});
}
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(
TITLE, "hh:mm:ss", "milliVolts", dataset, true, true, false);
final XYPlot plot = result.getXYPlot();
ValueAxis domain = plot.getDomainAxis();
domain.setAutoRange(true);
ValueAxis range = plot.getRangeAxis();
range.setRange(-MINMAX, MINMAX);
return result;
}
public void start() {
timer.start();
}
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
DTSCTest demo = new DTSCTest(TITLE);
demo.pack();
UIUtils.centerFrameOnScreen(demo);
demo.setVisible(true);
demo.start();
}
});
}
}

You can also eliminate the zero by first advanceTime(), then appendData. (swap the way they are doing it in the example).

One alternative approach to #thrashgod's answer would be to use TimeSeriesCollection and setting item age on the TimeSeries. Below code can setup a graph to show last 1 hour of data with 1 minute intervals.
private TimeSeriesCollection dataset;
private TimeSeries sensorSeries;
sensorSeries = new TimeSeries("name", Minute.class);
sensorSeries.setMaximumItemAge(60);
dataset = new TimeSeriesCollection();
dataset.addSeries(sensorSeries);
..and you will add the data as it comes with:
sensorSeries.add(new Minute(new Date()), newData);

Related

JFreeChart AutoRange Doesn't Work on Multiple Series On The Same Plot

In this code I created 2 TimeSeries and added them to the same plot but the axis.setAutoRange(true) works only for the second series.
Is there a way to make the AutoRange work on both of the TimeSeries?
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.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import javax.swing.*;
import java.awt.*;
public class Graph extends ApplicationFrame {
private TimeSeries seriesA;
private TimeSeries seriesB;
public Graph(final String windowTitle, int width, int height, String xTitle, String yTitle, String headerTitle, String graphTitle) {
super(windowTitle);
final TimeSeriesCollection dataset = new TimeSeriesCollection();
this.seriesA = new TimeSeries(graphTitle);
this.seriesB = new TimeSeries(graphTitle);
dataset.addSeries(this.seriesA);
dataset.addSeries(this.seriesB);
final JFreeChart chart = ChartFactory.createTimeSeriesChart(
headerTitle,//set title
xTitle,//set x title
yTitle,//set y title
dataset,
false,
false,
false
);
final XYPlot plot = chart.getXYPlot();
ValueAxis axis = plot.getDomainAxis();
axis.setFixedAutoRange(60000.0);
axis = plot.getRangeAxis();
axis.setAutoRange(true);
final ChartPanel chartPanel = new ChartPanel(chart);
final JPanel content = new JPanel(new BorderLayout());
content.add(chartPanel);
chartPanel.setPreferredSize(new java.awt.Dimension(width, height));
setContentPane(content);
}
public void addPointA(double y) {
this.seriesA.add(new Millisecond(), y);
}
public void addPointB(double y) {
this.seriesB.add(new Millisecond(), y);
}
public static void main(final String[] args) throws InterruptedException {
final Graph demo = new Graph("Demo",500,500,"Time","Value",
"Header1","graph1");//window title
demo.pack();//doesnt matter
RefineryUtilities.positionFrameOnScreen(demo,0.2,0.7);//manually choose window position %
demo.setVisible(true);//show window
double lastValue=80;//randomize input
while (true){
demo.addPointA(lastValue);
demo.addPointB(lastValue-100);
//randomize input
lastValue*=Math.random()*0.2-0.1+1.001;
lastValue+=Math.random()*2-1;
//limit input rate
Thread.sleep(100);
}
}
}
In this picture the axis.setAutoRange(true) works only for the red Graph (seriesB)
Several problems merit attention:
The name of each TimeSeries comprising a TimeSeriesCollection serves as a Comparable index; the names should be unique for reliable auto-ranging; enable the chart factory's legend to see the effect.
As shown in org.jfree.chart.demo.TimeSeriesChartDemo1, included in the distribution, auto-range typically requires no special settings.
Construct and manipulate Swing GUI objects only on the event dispatch thread.
Don't sleep() on the event dispatch thread; use javax.swing.Timer to pace updates.
Don't extend the top-level container needlessly.
Don't nest containers needlessly.
import java.awt.EventQueue;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import javax.swing.*;
import java.awt.event.*;
public class Graph extends ApplicationFrame {
private final TimeSeries seriesA = new TimeSeries("A");
private final TimeSeries seriesB = new TimeSeries("B");
public Graph(final String windowTitle, int width, int height,
String xTitle, String yTitle, String headerTitle, String graphTitle) {
super(windowTitle);
final TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(this.seriesA);
dataset.addSeries(this.seriesB);
final JFreeChart chart = ChartFactory.createTimeSeriesChart(
headerTitle, xTitle, yTitle, dataset, true, true, false
);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(width, height));
add(chartPanel);
}
public void addPointA(double y) {
this.seriesA.add(new Millisecond(), y);
}
public void addPointB(double y) {
this.seriesB.add(new Millisecond(), y);
}
public static void main(final String[] args) {
EventQueue.invokeLater(() -> {
Graph demo = new Graph("Demo", 640, 480,
"Time", "Value", "Header", "Graph");
demo.pack();//matters a great deal
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
new Timer(100, (new ActionListener() {
double lastValue = 80;
#Override
public void actionPerformed(ActionEvent e) {
demo.addPointA(lastValue);
demo.addPointB(lastValue - 100);
lastValue *= Math.random() * 0.2 - 0.1 + 1.001;
lastValue += Math.random() * 2 - 1;
}
})).start();
});
}
}

Changing the colors of Gantt renderer

I want to change the colors of the renderers in my chart. I'm using JFreeChart
in the same line. I want to have different collors according to the task description.
The problem is I keep having the same color in a line.
package testJFreeChart;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.geom.Rectangle2D;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.CategoryItemEntity;
import org.jfree.chart.entity.EntityCollection;
import org.jfree.chart.labels.CategoryItemLabelGenerator;
import org.jfree.chart.labels.IntervalCategoryItemLabelGenerator;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.CategoryItemRendererState;
import org.jfree.chart.renderer.category.GanttRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.IntervalCategoryDataset;
import org.jfree.data.gantt.GanttCategoryDataset;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;
import org.jfree.data.time.SimpleTimePeriod;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;
public class GanttDemo2 extends ApplicationFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public static final TaskSeriesCollection model = new TaskSeriesCollection();
public GanttDemo2(final String title) {
super(title);
final IntervalCategoryDataset dataset = createSampleDataset();
// create the chart...
final JFreeChart chart = ChartFactory.createGanttChart(
"Diagramme de Gantt", // chart title
"Processus", // domain axis label
"temps(ms)", // range axis label
dataset, // data
true, // include legend
true, // tooltips
false // urls
);
final CategoryPlot plot = (CategoryPlot) chart.getPlot();
DateAxis range = (DateAxis) plot.getRangeAxis();
range.setDateFormatOverride(new SimpleDateFormat("SSS"));
range.setMaximumDate(new Date(300));
// add the chart to a panel...
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
//GanttRenderer personnalisé..
MyRenderer renderer = new MyRenderer(model);
renderer.setBaseItemLabelGenerator(new CategoryItemLabelGenerator() {
public String generateLabel(CategoryDataset dataSet, int series, int categories) {
String label = "task";
return label;
}
public String generateColumnLabel(CategoryDataset dataset, int categories) {
//System.out.println(dataset.getColumnKey(categories).toString());
return dataset.getColumnKey(categories).toString();
}
public String generateRowLabel(CategoryDataset dataset, int series) {
return dataset.getRowKey(series).toString();
}
});
renderer.setBaseItemLabelsVisible(true);
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE9, TextAnchor.CENTER_LEFT));
plot.setRenderer(renderer);
plot.setBackgroundPaint(Color.WHITE);
}
/** #see http://stackoverflow.com/questions/8938690 */
private static class MyRenderer extends GanttRenderer {
private static final int PASS = 2; // assumes two passes
private final List<Color> clut = new ArrayList<Color>();
private final TaskSeriesCollection model;
private int row;
private int col;
private int index;
public MyRenderer(TaskSeriesCollection model) {
this.model = model;
}
#Override
public Paint getItemPaint(int row, int col) {
if (clut.isEmpty() || this.row != row || this.col != col) {
initClut(row, col);
this.row = row;
this.col = col;
index = 0;
}
int clutIndex = index++ / PASS;
return clut.get(clutIndex);
}
private void initClut(int row, int col) {
clut.clear();
Color result = Color.cyan;
TaskSeries series = (TaskSeries) model.getRowKeys().get(row);
List<Task> tasks = series.getTasks(); // unchecked
int taskCount = tasks.get(col).getSubtaskCount();
taskCount = Math.max(1, taskCount);
System.out.println("---->" + taskCount);
String description;
for (int i = 0; i < taskCount; i++) {
// if(taskCount!=1){
//clut.remove(result);
description = tasks.get(col).getSubtask(i).getDescription();
System.out.println(description + " " + i);
if (description.equals("bloque")) {
result = Color.green;
clut.add(result);
System.out.println("yessssss");
}
if (description.equals("ES")) {
result = Color.yellow;
clut.add(result);
}
if (description.equals("Exec")) {
result = Color.blue;
clut.add(result);
System.out.println("NOOOOO");
}
// }
clut.add(result);
}
}
}
private IntervalCategoryDataset createSampleDataset() {
final TaskSeries s1 = new TaskSeries("");
final Task t = new Task("P0", new SimpleTimePeriod(30, 50));
final Task st0 = new Task("Exec", new SimpleTimePeriod(10, 20));
// Task st01 = new Task( "ES",new SimpleTimePeriod(30,60));
t.addSubtask(st0);
// t.addSubtask(st01);
s1.add(t);
final Task t1 = new Task("P1", new SimpleTimePeriod(5, 10));
final Task st11 = new Task("ES", new SimpleTimePeriod(10, 20));
Task st12 = new Task("Exec", new SimpleTimePeriod(30, 60));
t1.addSubtask(st11);
t1.addSubtask(st12);
s1.add(t1);
final Task t3 = new Task("P2", new SimpleTimePeriod(0, 40));
final Task st31 = new Task("bloque", new SimpleTimePeriod(5, 30));
final Task st32 = new Task("Exec", new SimpleTimePeriod(50, 60));
t3.addSubtask(st31);
t3.addSubtask(st32);
s1.add(t3);
model.add(s1);
return model;
}
public static void main(final String[] args) {
final GanttDemo2 demo = new GanttDemo2("Gantt Chart Demo 2");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
It should be row 1 color blue; row 2 color yellow then blue; row 3 color green then blue.
The variation below seems to produce the desired result. Note that legend will not use the new colors unless you use the approach suggested here.
Console:
----> 1
exec: 0
blue
----> 2
ES: 0
yellow
exec: 1
blue
----> 2
bloque: 0
green
exec: 1
blue
Code:
package chart;
import java.awt.Color;
import java.awt.Paint;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.GanttRenderer;
import org.jfree.data.category.IntervalCategoryDataset;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;
import org.jfree.data.time.SimpleTimePeriod;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class GanttDemo2 extends ApplicationFrame {
private static final long serialVersionUID = 1L;
public static final TaskSeriesCollection model = new TaskSeriesCollection();
public GanttDemo2(final String title) {
super(title);
final IntervalCategoryDataset dataset = createSampleDataset();
// create the chart...
final JFreeChart chart = ChartFactory.createGanttChart(
"Diagramme de Gantt", // chart title
"Processus", // domain axis label
"temps(ms)", // range axis label
dataset, // data
false, // include legend
true, // tooltips
false // urls
);
final CategoryPlot plot = (CategoryPlot) chart.getPlot();
DateAxis range = (DateAxis) plot.getRangeAxis();
range.setDateFormatOverride(new SimpleDateFormat("SSS"));
range.setMaximumDate(new Date(100));
// add the chart to a panel...
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
//GanttRenderer personnalisé..
MyRenderer renderer = new MyRenderer(model);
plot.setRenderer(renderer);
plot.setBackgroundPaint(Color.WHITE);
}
/**
* #see https://stackoverflow.com/questions/8938690
*/
private static class MyRenderer extends GanttRenderer {
private static final int PASS = 2; // assumes two passes
private final List<Color> clut = new ArrayList<>();
private final TaskSeriesCollection model;
private int row;
private int col;
private int index;
public MyRenderer(TaskSeriesCollection model) {
this.model = model;
}
#Override
public Paint getItemPaint(int row, int col) {
if (clut.isEmpty() || this.row != row || this.col != col) {
initClut(row, col);
this.row = row;
this.col = col;
index = 0;
}
int clutIndex = index++ / PASS;
return clut.get(clutIndex);
}
private void initClut(int row, int col) {
clut.clear();
TaskSeries series = (TaskSeries) model.getRowKeys().get(row);
List<Task> tasks = series.getTasks(); // unchecked
int taskCount = tasks.get(col).getSubtaskCount();
taskCount = Math.max(1, taskCount);
System.out.println("----> " + taskCount);
String description;
for (int i = 0; i < taskCount; i++) {
description = tasks.get(col).getSubtask(i).getDescription();
System.out.println(description + ": " + i);
if (description.equals("bloque")) {
clut.add(Color.green);
System.out.println("green");
}
if (description.equals("ES")) {
clut.add(Color.yellow);
System.out.println("yellow");
}
if (description.equals("exec")) {
clut.add(Color.blue);
System.out.println("blue");
}
}
}
}
private IntervalCategoryDataset createSampleDataset() {
final TaskSeries s1 = new TaskSeries("");
final Task t0 = new Task("P0", new SimpleTimePeriod(30, 50));
final Task st0 = new Task("exec", new SimpleTimePeriod(10, 20));
// Task st01 = new Task( "ES",new SimpleTimePeriod(30,60));
t0.addSubtask(st0);
// t.addSubtask(st01);
s1.add(t0);
final Task t1 = new Task("P1", new SimpleTimePeriod(5, 10));
final Task st11 = new Task("ES", new SimpleTimePeriod(10, 20));
Task st12 = new Task("exec", new SimpleTimePeriod(30, 60));
t1.addSubtask(st11);
t1.addSubtask(st12);
s1.add(t1);
final Task t2 = new Task("P2", new SimpleTimePeriod(0, 40));
final Task st31 = new Task("bloque", new SimpleTimePeriod(5, 30));
final Task st32 = new Task("exec", new SimpleTimePeriod(50, 60));
t2.addSubtask(st31);
t2.addSubtask(st32);
s1.add(t2);
model.add(s1);
return model;
}
public static void main(final String[] args) {
final GanttDemo2 demo = new GanttDemo2("Gantt Chart Demo 2");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}

How to draw moving and Running sine wave chart using JFree chart in java?

I m using JFreeChart to draw Running or Moving Sine Wave in java with netbeans. I write code for it , but it does not like moving or running sine wave graph. So if you have any idea regarding that then suggest me.
My code is below
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
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;
import org.jfree.ui.ApplicationFrame;
public class DrawChart extends ApplicationFrame implements ActionListener {
public XYSeries series;
public DrawChart(final String title) {
super(title);
series = new XYSeries("Sine", true, true);
XYSeriesCollection dataset = new XYSeriesCollection(series);
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
final JButton button = new JButton("Add New Data Item");
button.setActionCommand("ADD_DATA");
button.addActionListener(this);
final JPanel content = new JPanel(new BorderLayout());
content.add(chartPanel);
content.add(button, BorderLayout.SOUTH);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(content);
}
private JFreeChart createChart(final XYDataset dataset) {
JFreeChart jfreechart = ChartFactory.createXYLineChart("Sin Curve", "Angle (Deg)", "Value", dataset, PlotOrientation.VERTICAL, true, true, true);
jfreechart.setBackgroundPaint(Color.white);
XYPlot xyplot = (XYPlot) jfreechart.getPlot();
xyplot.setBackgroundPaint(Color.lightGray);
xyplot.setDomainGridlinePaint(Color.white);
xyplot.setRangeGridlinePaint(Color.white);
return jfreechart;
}
public void actionPerformed(final ActionEvent e) {
if (e.getActionCommand().equals("ADD_DATA")) {
for (int i = 0; i < 100; i++) {
final double x = (i)/10.0 ;
final double y = Math.sin(x);
this.series.addOrUpdate(x, y);
}
}
}
public static void main(final String[] args) {
final DrawChart demo = new DrawChart("Dynamic Data view");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
Starting from this example, substituting Math.sin() for nextGaussian() produced the illustration below. A javax.swing.Timer paces the animation at 1 Hz.
private ChartPanel createPane() {
final XYSeries series = new XYSeries("Data");
for (int i = 0; i < random.nextInt(N) + N / 2; i++) {
series.add(i, Math.sin(i));
}
XYSeriesCollection dataset = new XYSeriesCollection(series);
new Timer(1000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
series.add(series.getItemCount(), Math.sin(series.getItemCount()));
}
}).start();
…
}
Try this JavaFX. It's more flexible.
This is what I gathered and came up with using JavaFX
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javafx.animation.AnimationTimer;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart.Series;
import javafx.stage.Stage;
public class ACS extends Application
{
private static final int MAX_DATA_POINTS = 200;
private Series series;
private float xSeriesData = 0;
private final ConcurrentLinkedQueue<Number> dataQ = new ConcurrentLinkedQueue<Number>();
private ExecutorService executor;
private AddToQueue addToQueue;
private Timeline timeline2;
private NumberAxis xAxis;
private void init(final Stage primaryStage)
{
xAxis = new NumberAxis(0, MAX_DATA_POINTS, 1);
xAxis.setForceZeroInRange(false);
xAxis.setAutoRanging(false);
final NumberAxis yAxis = new NumberAxis(0, 200, 1);
yAxis.setAutoRanging(false);
// -- Chart
final LineChart<Number, Number> sc = new LineChart<Number, Number>(xAxis, yAxis) {
// Override to remove symbols on each data point
#Override
protected void dataItemAdded(final Series<Number, Number> series, final int itemIndex, final Data<Number, Number> item)
{}
};
sc.setAnimated(false);
sc.setId("liveAreaChart");
sc.setTitle("Animated Area Chart");
// -- Chart Series
series = new LineChart.Series<Number, Number>();
series.setName("Area Chart Series");
sc.getData().add(series);
final Scene scene = new Scene(sc, 800, 800);
scene.getStylesheets().add("site.css");
sc.getStyleClass().add("thick-chart");
primaryStage.setScene(scene);
}
#Override
public void start(final Stage primaryStage) throws Exception
{
init(primaryStage);
primaryStage.show();
// -- Prepare Executor Services
executor = Executors.newCachedThreadPool();
addToQueue = new AddToQueue();
executor.execute(addToQueue);
// -- Prepare Timeline
prepareTimeline();
}
public static void main(final String[] args)
{
launch(args);
}
private class AddToQueue implements Runnable
{
double PERIOD = 20;
double SCALE = 20;
int pos = 0;
#Override
public void run()
{
try {
final double Min = 20;
final double Max = 55;
// add a item of random data to queue
//uncomment the line below to generate a normal graph
// dataQ.add(Min + (Math.random() * ((Max - Min))));
dataQ.add(((Math.sin((++pos * 2 * Math.PI) / PERIOD) * (SCALE / 2)) + (SCALE / 2)));
Thread.sleep(1000);
executor.execute(this);
}
catch (final InterruptedException ex) {
// Logger.getLogger(ACS.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
// -- Timeline gets called in the JavaFX Main thread
private void prepareTimeline()
{
// Every frame to take any data from queue and add to chart
new AnimationTimer() {
#Override
public void handle(final long now)
{
addDataToSeries();
}
}.start();
}
private void addDataToSeries()
{
for (int i = 0; i < 20; i++) { // -- add 20 numbers to the plot+
if (dataQ.isEmpty()) {
break;
}
// series.getData().add(new LineChart.Data(xSeriesData++, dataQ.remove()));
final Number datapoint = dataQ.remove();
xSeriesData = xSeriesData + 1;
System.out.println(xSeriesData + " " + datapoint);
series.getData().add(new LineChart.Data(xSeriesData, datapoint));
}
// remove points to keep us at no more than MAX_DATA_POINTS
if (series.getData().size() > (MAX_DATA_POINTS * 10)) {
series.getData().remove(0, series.getData().size() - (MAX_DATA_POINTS * 10));
}
// update
xAxis.setLowerBound(xSeriesData - (MAX_DATA_POINTS));
xAxis.setUpperBound(xSeriesData - 1);
}
}

Dynamically repaint ScatterPlot in jfreechart

I have a scatterplot which plots positions of agents. These positions change. I was wondering how can I repaint/redraw the scatterplot with the new positions
my drawing method. I need to redraw in the updatePositions function. Is there any way to implement any listener for ScatterPlot?
private ChartPanel createPanel() {
JFreeChart jfreechart = ChartFactory.createScatterPlot(
title, "", "", initPositions(),PlotOrientation.VERTICAL, true, true, false);
XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
xyPlot.setDomainCrosshairVisible(true);
xyPlot.setRangeCrosshairVisible(true);
XYItemRenderer renderer = xyPlot.getRenderer();
renderer.setSeriesPaint(0, Color.blue);
adjustAxis((NumberAxis) xyPlot.getDomainAxis(), true);
adjustAxis((NumberAxis) xyPlot.getRangeAxis(), false);
xyPlot.setBackgroundPaint(Color.white);
return new ChartPanel(jfreechart);
}
private void adjustAxis(NumberAxis axis, boolean vertical) {
axis.setRange(-1, lattice+1);
axis.setTickUnit(new NumberTickUnit(1));
axis.setVerticalTickLabels(vertical);
}
private XYDataset initPositions() {
XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
for (int i = 0; i < populationSize; i++) {
if(population.get(i).status==1){
healthy.add(population.get(i).position[0], population.get(i).position[1]);
}else if(population.get(i).status==2){
infected.add(population.get(i).position[0], population.get(i).position[1]);
}else if(population.get(i).status==3){
recovered.add(population.get(i).position[0], population.get(i).position[1]);
}
}
xySeriesCollection.addSeries(healthy);
xySeriesCollection.addSeries(infected);
xySeriesCollection.addSeries(recovered);
return xySeriesCollection;
}
public void clear(){
healthy.clear();
infected.clear();
recovered.clear();
}
public void updatePositions(ArrayList<Person> pop ){
population = pop;
for (int i = 0; i < populationSize; i++) {
if(population.get(i).status==1){
healthy.addOrUpdate(population.get(i).position[0], population.get(i).position[1]);
}else if(population.get(i).status==2){
infected.addOrUpdate(population.get(i).position[0], population.get(i).position[1]);
}else if(population.get(i).status==3){
recovered.addOrUpdate(population.get(i).position[0], population.get(i).position[1]);
}
}
}
this is the method in the main class. The update of the positions is done at the "move" function
public static void main(String [] args){
createPopulation(populationSize);
initInfection(infectRatio);
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
DrawArea demo = new DrawArea("Demo", lattice, populationSize,population);
demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
demo.pack();
demo.setLocationRelativeTo(null);
demo.setVisible(true);
for(int i =0;i<1000;i++){
for(int j=0; j<populationSize; j++){
population.get(j).move(0.8);
}
demo.clear();
demo.updatePositions(population);
}
}
});
}
As shown below, the chart (view) listens to its dataset (model) and updates itself accordingly. When the Move button is pressed, each XYDataItem in the series in modified in update() to reflect its new position.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.*;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataItem;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
/**
* #see http://stackoverflow.com/a/19749344/230513
* #see http://stackoverflow.com/a/7208723/230513
*/
public class ScatterMove extends JFrame {
private static final int N = 16;
private static final String title = "Scatter Move Demo";
private static final Random rand = new Random();
private XYSeries moved = new XYSeries("Population");
public ScatterMove(String s) {
super(s);
update();
final ChartPanel chartPanel = createDemoPanel();
this.add(chartPanel, BorderLayout.CENTER);
JPanel control = new JPanel();
control.add(new JButton(new AbstractAction("Move") {
#Override
public void actionPerformed(ActionEvent e) {
moved.clear();
update();
}
}));
this.add(control, BorderLayout.SOUTH);
}
private void update() {
for (int i = 0; i < N; i++) {
moved.add(new XYDataItem(rand.nextGaussian(), rand.nextGaussian()));
}
}
private ChartPanel createDemoPanel() {
JFreeChart jfreechart = ChartFactory.createScatterPlot(
title, "X", "Y", createSampleData(),
PlotOrientation.VERTICAL, true, true, false);
XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
XYItemRenderer renderer = xyPlot.getRenderer();
NumberAxis domain = (NumberAxis) xyPlot.getDomainAxis();
domain.setRange(-3.0, 3.0);
domain.setTickUnit(new NumberTickUnit(1));
NumberAxis range = (NumberAxis) xyPlot.getRangeAxis();
range.setRange(-3.0, 3.0);
range.setTickUnit(new NumberTickUnit(1));
return new ChartPanel(jfreechart){
#Override
public Dimension getPreferredSize() {
return new Dimension(640, 480);
}
};
}
private XYDataset createSampleData() {
XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
xySeriesCollection.addSeries(moved);
return xySeriesCollection;
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
ScatterMove demo = new ScatterMove(title);
demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
demo.pack();
demo.setLocationRelativeTo(null);
demo.setVisible(true);
}
});
}
}

How to get the coordinates of a point on mouse click with JFreeChart?

I am trying to get the coordinates of the point clicked with the mouse on a scatter plot graph.
When you click on a point, "Click event!" and the coordinates are printed on the console.
When you click on the "Test" button, "Test" and the coordinates are printed on the console.
Problem: The coordinates printed after clicking the button are up-to-date. The coordinates printed after clicking on a point are the one of the previously selected point.
How can I fix that (so when I click on a point, the coordinates of the new selected point are displayed) ?
package graph;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
/**
* #see http://stackoverflow.com/questions/7231824
* #see http://stackoverflow.com/questions/7205742
* #see http://stackoverflow.com/questions/7208657
* #see http://stackoverflow.com/questions/7071057
*/
public class GraphFrameOld extends JFrame {
private static final int N = 32;
private static final String title = "Scatter Plot Pannel";
private static final Random rand = new Random();
private final XYSeries added = new XYSeries("Added");
private static XYPlot xyPlot;
public GraphFrameOld(String s) {
super(s);
final ChartPanel chartPanel = createGraphPanel();
this.add(chartPanel, BorderLayout.CENTER);
JPanel control = new JPanel();
control.add(new JButton(new AbstractAction("Add") {
#Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < N; i++) {
added.add(rand.nextDouble(), rand.nextDouble());
}
}
}));
control.add(new JButton(new AbstractAction("Test") {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Test");
System.out.println(xyPlot.getDomainCrosshairValue() + " "
+ xyPlot.getRangeCrosshairValue());
}
}));
// add click event
chartPanel.addChartMouseListener(new ChartMouseListener() {
#Override
public void chartMouseClicked(ChartMouseEvent e) {
System.out.println("Click event!");
XYPlot xyPlot2 = chartPanel.getChart().getXYPlot();
// Problem: the coordinates displayed are the one of the previously selected point !
System.out.println(xyPlot2.getDomainCrosshairValue() + " "
+ xyPlot2.getRangeCrosshairValue());
}
#Override
public void chartMouseMoved(ChartMouseEvent arg0) {
}
});
this.add(control, BorderLayout.SOUTH);
}
private ChartPanel createGraphPanel() {
JFreeChart jfreechart = ChartFactory
.createScatterPlot(title, "X", "Y", createSampleData(),
PlotOrientation.VERTICAL, true, true, false);
xyPlot = (XYPlot) jfreechart.getPlot();
xyPlot.setDomainCrosshairVisible(true);
xyPlot.setRangeCrosshairVisible(true);
XYItemRenderer renderer = xyPlot.getRenderer();
renderer.setSeriesPaint(0, Color.blue);
NumberAxis domain = (NumberAxis) xyPlot.getDomainAxis();
domain.setRange(0.00, 1.00);
domain.setTickUnit(new NumberTickUnit(0.1));
domain.setVerticalTickLabels(true);
NumberAxis range = (NumberAxis) xyPlot.getRangeAxis();
range.setRange(0.0, 1.0);
range.setTickUnit(new NumberTickUnit(0.1));
return new ChartPanel(jfreechart);
}
private XYDataset createSampleData() {
XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
XYSeries series = new XYSeries("Random");
for (int i = 0; i < N * N; i++) {
double x = rand.nextDouble();
double y = rand.nextDouble();
series.add(x, y);
}
xySeriesCollection.addSeries(series);
xySeriesCollection.addSeries(added);
return xySeriesCollection;
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
GraphFrameOld demo = new GraphFrameOld(title);
demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
demo.pack();
demo.setLocationRelativeTo(null);
demo.setVisible(true);
}
});
}
}
My guess is that your mouse listener gets called before the (internal) JFreeCharts listener is, so the cross hair point is not yet updated when your code executes (point to the previous selection still). Putting your chartMouseClicked code in an invokeLater should fix that.
The problem is that the redrawing of the chart is also triggered by the mouse event and so it is not assured that this has finished at the time your event listener is triggered.
You should listen to another point which guarantees the redraw is finished. You can use the chartProgressListener and filter out when the redrawing has completed. The adapted code isn't very elegant and you might need some more checks, but it seems to do the job:
package graph;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.event.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
/**
* #see http://stackoverflow.com/questions/7231824
* #see http://stackoverflow.com/questions/7205742
* #see http://stackoverflow.com/questions/7208657
* #see http://stackoverflow.com/questions/7071057
*/
public class GraphFrameOld extends JFrame {
private static final int N = 32;
private static final String title = "Scatter Plot Pannel";
private static final Random rand = new Random();
private final XYSeries added = new XYSeries("Added");
private static XYPlot xyPlot;
public GraphFrameOld(String s) {
super(s);
final ChartPanel chartPanel = createGraphPanel();
this.add(chartPanel, BorderLayout.CENTER);
JPanel control = new JPanel();
control.add(new JButton(new AbstractAction("Add") {
#Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < N; i++) {
added.add(rand.nextDouble(), rand.nextDouble());
}
}
}));
control.add(new JButton(new AbstractAction("Test") {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Test");
System.out.println(xyPlot.getDomainCrosshairValue() + " "
+ xyPlot.getRangeCrosshairValue());
}
}));
this.add(control, BorderLayout.SOUTH);
}
private ChartPanel createGraphPanel() {
JFreeChart jfreechart = ChartFactory
.createScatterPlot(title, "X", "Y", createSampleData(),
PlotOrientation.VERTICAL, true, true, false);
xyPlot = (XYPlot) jfreechart.getPlot();
xyPlot.setDomainCrosshairVisible(true);
xyPlot.setRangeCrosshairVisible(true);
XYItemRenderer renderer = xyPlot.getRenderer();
renderer.setSeriesPaint(0, Color.red);
NumberAxis domain = (NumberAxis) xyPlot.getDomainAxis();
domain.setRange(0.00, 1.00);
domain.setTickUnit(new NumberTickUnit(0.1));
domain.setVerticalTickLabels(true);
NumberAxis range = (NumberAxis) xyPlot.getRangeAxis();
range.setRange(0.0, 1.0);
range.setTickUnit(new NumberTickUnit(0.1));
final ChartPanel result = new ChartPanel(jfreechart);
jfreechart.addProgressListener(new ChartProgressListener() {
#Override
public void chartProgress(ChartProgressEvent cpe) {
if(cpe.getType()==ChartProgressEvent.DRAWING_FINISHED){
System.out.println("Click event!");
XYPlot xyPlot2 = result.getChart().getXYPlot();
System.out.println(xyPlot2.getDomainCrosshairValue() + " "
+ xyPlot2.getRangeCrosshairValue());
}
}
});
return result;
}
private XYDataset createSampleData() {
XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
XYSeries series = new XYSeries("Random");
for (int i = 0; i < N * N; i++) {
double x = rand.nextDouble();
double y = rand.nextDouble();
series.add(x, y);
}
xySeriesCollection.addSeries(series);
xySeriesCollection.addSeries(added);
return xySeriesCollection;
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
GraphFrameOld demo = new GraphFrameOld(title);
demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
demo.pack();
demo.setLocationRelativeTo(null);
demo.setVisible(true);
}
});
}
}
Try extracting from ChartMouseEvent e:
e.x
e.y
e.getTrigger().getX()
worked for me!
System.out.println(e.getTrigger().getX() + " " + e.getTrigger().getY() );

Categories

Resources