how to create real time line chart using jfreechart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
akki_tg
Posts: 6
Joined: Fri May 24, 2013 8:27 pm
antibot: No, of course not.

how to create real time line chart using jfreechart

Post by akki_tg » Sat May 25, 2013 9:40 am

i have made line chart in jfree.to accomplish that i have created a simple java class that retrieves data from database in the form of arraylist.and i used a jsp page to display chart.that code works fine and display line chart on web browser but now i want to create a real time line chart or dynamic chart.but i do not know how to get that.
can anybody sort out my problem.

akki_tg
Posts: 6
Joined: Fri May 24, 2013 8:27 pm
antibot: No, of course not.

how to create chart using only one value

Post by akki_tg » Wed May 29, 2013 11:45 am

hi all,i would like to createa dynamic chart using only one value.but dont know how to do that.
suppose i have values like x=11,23,34......
is it possible to create chart using only x values(dynamic chart)?
thanx

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

Re: how to create real time line chart using jfreechart

Post by John Matthews » Wed May 29, 2013 12:02 pm

This example creates an arbitrary number of line charts that update in approximately real time. Because accessing a database implies unpredictable latency, update your chart's data model in the background thread of a SwingWorker, illustrated here.

akki_tg
Posts: 6
Joined: Fri May 24, 2013 8:27 pm
antibot: No, of course not.

Re: how to create real time line chart using jfreechart

Post by akki_tg » Wed May 29, 2013 9:45 pm

John Matthews wrote:This example creates an arbitrary number of line charts that update in approximately real time. Because accessing a database implies unpredictable latency, update your chart's data model in the background thread of a SwingWorker, illustrated here.
hi john, thanks for reply
your examle works great.but i would like to show it on web browser.can you post me any full example regarding dynamic line chart.
thanks again

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

Re: how to create real time line chart using jfreechart

Post by John Matthews » Fri May 31, 2013 12:20 pm

You could use the meta refresh tag in your servlet/JSP, instead of a Swing timer.

akki_tg
Posts: 6
Joined: Fri May 24, 2013 8:27 pm
antibot: No, of course not.

Re: how to create real time line chart using jfreechart

Post by akki_tg » Sat Jun 01, 2013 2:38 pm

John Matthews wrote:You could use the meta refresh tag in your servlet/JSP, instead of a Swing timer.
hi john,u told me use meta refresh tag for jsp/servlet.but i did not get the point.actually i dont have so much experience in jsp/servlet.could u guide me how this code i implement in servlet/jsp.i'll be very thankfull to you.



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.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;


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), 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 = 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();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
demo.start();
}
});
}
}

akki_tg
Posts: 6
Joined: Fri May 24, 2013 8:27 pm
antibot: No, of course not.

Re: how to create real time line chart using jfreechart

Post by akki_tg » Sun Jun 02, 2013 12:50 pm

hi john,u told me use meta refresh tag for jsp/servlet.but i did not get the point.actually i dont have so much experience in jsp/servlet.could u guide me how this code i implement in servlet/jsp.i'll be very thankfull to you.



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.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;


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), 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 = 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();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
demo.start();
}
});
}
}

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

Re: how to create real time line chart using jfreechart

Post by John Matthews » Sun Jun 02, 2013 6:36 pm

Add the meta refresh tag to the JSP that references your chart servlet. A complete example of a chart servlet is beyond the scope of this forum, but there's a good example in the guide.

JeromeJames
Posts: 1
Joined: Thu Nov 28, 2013 10:21 am
antibot: No, of course not.

Re: how to create real time line chart using jfreechart

Post by JeromeJames » Thu Nov 28, 2013 10:27 am

I have created real time charts using Jscharts with the help of a charting software.I found it really useful for creating various types of javascript charts.

Locked