Dynamic jfreechat in 30seconds intervals

A free public discussion forum for the JFreeChart class library.

Dynamic jfreechat in 30seconds intervals

Postby gmakinana » Tue Jul 02, 2013 12:28 pm

I am new in JFreeChats and new to Java also...

I have this data from our claims table

SEQ IP_CLAIMS_RECEIVED HB_CLAIMS_RECEIVED IP_AVERAGE_RESPONSE HB_AVERAGE_RESPONSE 30 Seconds 29 19 4 4

This data every 30 seconds will change giving the amount of claims processed and with the avarage seconds it took to process this...

So my graph I want it on the Y-Axis to display the round seconds like (0,,5,10,15,20,25,30,35,40) and then on the X-Axis to display the time - the 30 seconds intervals showing the refresh times... And then the spike-lines would show the average-response times... And if it will be possible have two combined graphs one for IP Claims and another for HB Claims... I have a perfect example of the graph but its confusing me a bit here is the code of it below:

Code: Select all

package timeseriesdemo;
  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 TimeSeriesDemo 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 TimeSeriesDemo(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();
            System.out.println("dataset1 : "+dataset.advanceTime());
            dataset.advanceTime();
            dataset.appendData(newData);
        }
    });
}

private float randomValue() {
    System.out.println("randomvalue : "+(float) (random.nextGaussian() * MINMAX / 3));
    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", "Claims Received", 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() {
            TimeSeriesDemo demo = new TimeSeriesDemo(TITLE);
            demo.pack();
            RefineryUtilities.centerFrameOnScreen(demo);
            demo.setVisible(true);
            demo.start();
        }
    });
}[img][/img]
gmakinana
 
Posts: 1
Joined: Tue Jul 02, 2013 12:26 pm

Re: Dynamic jfreechat in 30seconds intervals

Postby John Matthews » Thu Jul 04, 2013 7:57 pm

Cross-posted here. Code copied from here without the required attribution.
John Matthews
 
Posts: 190
Joined: Wed Sep 12, 2007 3:18 pm

Re: Dynamic jfreechat in 30seconds intervals

Postby david.gilbert » Mon Jul 08, 2013 7:04 am

Thanks John, you've done an amazing job to make StackOverflow an excellent resource for JFreeChart information.

To all developers: when you use someone else's code, no matter what the source, please check the terms and conditions for the use of that code...and respect them. It is very important, even (or especially) for code you don't pay money for.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
david.gilbert
JFreeChart Project Leader
 
Posts: 11195
Joined: Fri Mar 14, 2003 10:29 am


Return to JFreeChart - General

Who is online

Users browsing this forum: Bing [Bot] and 3 guests