Time Series Chart question

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Adrian

Time Series Chart question

Post by Adrian » Tue Feb 17, 2004 2:14 am

Hi,

I am trying to create a time series chart, but instead of the months as the sample chart shows, I'd like to display hours on the time axle. I tried using the Hour class, but when adding to the TimeSeries object, it gives me a runtime error (TimeSeries.add(): you are trying to add data where the time period class is org.jfree.data.time.Hour, but the TimeSeries is expecting an instance of org.jfree.data.time.Day). Do I have to convert myself the days to hours, or there is another way JFreeChart offers?Anybody did this before?

Thanks,

Adrian

Guest

Post by Guest » Tue Feb 17, 2004 6:08 am

While creating a TimeSeries class specify the Hour.class in the constructor

TimeSeries s1 = new TimeSeries("L&G European Index Trust", Hour.class);
s1.add(new Hour(15, new Day(new Date(2001, 02, 15))), 181.8);
s1.add(new Hour(16, new Day(new Date(2001, 02, 16))), 123);

Ameet

akanguri
Posts: 4
Joined: Mon Feb 16, 2004 2:01 pm

Post by akanguri » Tue Feb 17, 2004 6:09 am

While creating a TimeSeries class specify the Hour.class in the constructor

TimeSeries s1 = new TimeSeries("L&G European Index Trust", Hour.class);
s1.add(new Hour(15, new Day(new Date(2001, 02, 15))), 181.8);
s1.add(new Hour(16, new Day(new Date(2001, 02, 16))), 123);

Ameet

luis_esru
Posts: 42
Joined: Tue Jan 20, 2004 6:40 pm
Location: Glasgow
Contact:

Post by luis_esru » Tue Feb 17, 2004 9:45 am

Have a look at the sample below:
Luis

---
package org.jfree.chart.demo;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.TimeSeriesToolTipGenerator;

import org.jfree.chart.Marker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.StandardXYItemRenderer;
import org.jfree.chart.renderer.XYItemRenderer;
import org.jfree.data.XYDataset;
import org.jfree.chart.TextTitle;
import org.jfree.data.DefaultTableXYDataset;
import org.jfree.data.XYSeries;

import java.text.SimpleDateFormat;
import java.util.Locale;
import java.text.ParseException;

import java.awt.*;
import javax.swing.*;

/**
* A time series chart.
*
* @author David Gilbert
*/
public class TimeSeriesDemo8 extends JInternalFrame {


String what = "Resultant Temperature";
String when = "day 1";
String where = "group of zones 1";
String[] units = {"Time","degC"};
int[] catSerNbs = {24,4};
String[] series = {"reception","office","X1","X2"};


String[] time = {
"01-Jul-1999 01:30:00","01-Jul-1999 02:30:00","01-Jul-1999 03:30:00","01-Jul-1999 04:30:00",
"01-Jul-1999 05:30:00","01-Jul-1999 06:30:00","01-Jul-1999 07:30:00","01-Jul-1999 08:30:00","01-Jul-1999 09:30:00",
"01-Jul-1999 10:30:00","01-Jul-1999 11:30:00","01-Jul-1999 12:30:00","01-Jul-1999 13:30:00","01-Jul-1999 14:30:00",
"01-Jul-1999 15:30:00","01-Jul-1999 16:30:00","01-Jul-1999 17:30:00","01-Jul-1999 18:30:00","01-Jul-1999 19:30:00",
"01-Jul-1999 20:30:00","01-Jul-1999 21:30:00","01-Jul-1999 22:30:00","01-Jul-1999 23:30:00","01-Jul-1999 24:30:00"};

String[] time1 = {
"01-Jun-1999 01:30:00","01-Jun-1999 02:30:00","01-Jun-1999 03:30:00","01-Jun-1999 04:30:00",
"01-Jun-1999 05:30:00","01-Jun-1999 06:30:00","01-Jun-1999 07:30:00","01-Jun-1999 08:30:00","01-Jun-1999 09:30:00",
"01-Jun-1999 10:30:00","01-Jun-1999 11:30:00","01-Jun-1999 12:30:00","01-Jun-1999 13:30:00","01-Jun-1999 14:30:00",
"01-Jun-1999 15:30:00","01-Jun-1999 16:30:00","01-Jun-1999 17:30:00","01-Jun-1999 18:30:00","01-Jun-1999 19:30:00",
"01-Jun-1999 20:30:00","01-Jun-1999 21:30:00","01-Jun-1999 22:30:00","01-Jun-1999 23:30:00","01-Jun-1999 24:30:00"};

double[] data1 = {16.61,16.54,16.43,16.33,16.22,16.13,16.04,16.65,17.76,
18.40,18.99,19.48,19.74,20.01,20.24,20.37,20.33,19.67,18.65,18.13,17.63,17.15,16.95,16.69};
double[] data2 = {12.33,12.22,12.06,11.91,11.78,11.65,11.53,13.64,16.22,16.80,17.10,17.28,
17.39,17.48,17.57,17.62,17.65,17.64,16.83,14.64,13.58,13.32,12.79,12.59};
double[] data3 = {23.61,22.54,23.43,24.33,25.22,27.13,24.04,23.65,22.76,
21.40,22.99,23.48,24.74,23.01,22.24,22.37,22.33,23.67,23.65,24.13,25.63,26.15,27.95,28.69};
double[] data4 = {17.33,17.22,18.06,19.91,19.78,19.65,18.53,17.64,16.22,15.80,14.10,14.28,
14.39,14.48,15.57,16.62,17.65,18.64,19.83,19.64,19.58,18.32,17.79,16.59};

double criticalValue = 27;

/**
* A demonstration application showing how to create a simple time series chart.
*
* @param title the frame title.
*/
public TimeSeriesDemo8(String title) {

super(title, true, true, true, true);

setLocation(0,0);
setSize(250,200);


XYDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
chartPanel.setMouseZoomable(true, false);
setContentPane(chartPanel);

}


private JFreeChart createChart(XYDataset dataset) {


JFreeChart chart = ChartFactory.createTimeSeriesChart(
what,
units[0],
units[1],
dataset,
true,
true,
false
);
chart.setBackgroundPaint(Color.white);

chart.addSubtitle(new TextTitle(when));


XYPlot plot = chart.getXYPlot();
plot.setInsets(new Insets(0, 0, 0, 20));
plot.addRangeMarker(new Marker(criticalValue, Color.BLACK,
new BasicStroke(1.0f), Color.BLACK, 0.8f));

XYItemRenderer renderer = plot.getRenderer();
if (renderer instanceof StandardXYItemRenderer) {
StandardXYItemRenderer r = (StandardXYItemRenderer) renderer;
r.setPlotShapes(true);
r.setShapesFilled(true);
TimeSeriesToolTipGenerator g = new TimeSeriesToolTipGenerator("hh:mm:ss", "0.00");
r.setToolTipGenerator(g);
}


//this colors must be decided!!!
Paint[] paint = {Color.green,Color.cyan,Color.orange,Color.pink};

for (int i=0; i<catSerNbs[1]; i++){
renderer.setSeriesPaint(i, paint);
}

return chart;
}


public XYDataset createDataset() {

SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss", Locale.UK);
XYSeries series1 = new XYSeries(series[0], false);
XYSeries series2 = new XYSeries(series[1], false);
XYSeries series3 = new XYSeries(series[2], false);
XYSeries series4 = new XYSeries(series[3], false);

try {
for (int i=0; i<catSerNbs[0]; i++){
series1.add(sdf.parse(time).getTime(),data1);
series2.add(sdf.parse(time).getTime(),data2);
series3.add(sdf.parse(time1).getTime(),data3);
series4.add(sdf.parse(time1).getTime(),data4);
}
}
catch (ParseException e) {
e.printStackTrace();
}
DefaultTableXYDataset dataset = new DefaultTableXYDataset(series1);
dataset.addSeries(series2);
dataset.addSeries(series3);
dataset.addSeries(series4);

return dataset;
}

}
University of Strathclyde in Glasgow
ESRU
luis@esru.strath.ac.uk

Adrin

Post by Adrin » Tue Feb 17, 2004 7:05 pm

Thanks for your answers. It works now.

Adrian

Adrian

One more question related to the Time Series chart

Post by Adrian » Wed Feb 18, 2004 12:47 am

First a few words about what I need to do: I need to display the number of widgets created every hour for the past 12 hours. I add 12 entries into my TimeSeries object, and I expect to see 12 entries on the time axles, one for each hour. However, for some reason my chart only displays every other hour (I expect to see on the time axle 13:00, 14:00, 16:00, 17:00.... but it displays 14:00, 16:00, 18:00). There is any way to override this default? I tried with to set auto default to false, and played with ranges, and ticks, but with no success. Anyone has any idea how to solve this?
Thanks,

Adrian

Locked