TimeSeries Chart
Time series charts are created using data from the XYDataset, it returns x-values which are of type double, these values are converted by a class called DateAxis which converts the huge double returned from XYDataset into dates and back to the double as necessary. How the double values are converted into dates isn't important for us to know, the DateAxis just does it for us which we can use to create the chart. If you are really interested to find out, then type "number of milliseconds since midnight, 1 January 1970, XYDataset, JFreeChart" in to the Holy Grail of Searches(google ;)), you might find some convincing results.
Charts usually have a domain, a time series as the name of the interface says itself(TimeSerieschart), has a domain which will have a series of times, as a matter of fact it can have values ranging from days to months.
TimeSeries can let you make objects of different times and it lets you add up all of the them and gives a series of times back as one object.
A dataset, TimeSeriesCollection, can be created out of the TimeSeries object by adding the TimeSeries object to the TimeSeriesCollection object. Since, TimeSeriesCollection implements XYDataset, the TimeSeriesCollection becomes a dataset which can be used to create the chart.
A chart is created with default settings but it is highly customizable, for example, a renderer object can be obtained from the plot which can in-turn be obtained from the chart. Now this renderer can be changed to display series shapes at each datas point, in addition to the lines between data points.And secondly, a date format override can be set for the domain axis.
To modify the renderer, first a reference to the renderer is needed and secondly cast of the rendrer into a XYLineAndShapeRenderer is required.
eg. to set the default shape visible and to set default shape filled the following magic will suffice:
XYItemRenderer r = plot.getRenderer();
if(r instanceof XYLineAndShapeRenderer)
{
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
renderer.setDeafultShapeVisible(true);
renderer.setDefaultShapeFilled(true);
}
In other words, the above snippet of code puts a legend, which is picked for the series, at each data point.
To override the date format of the domain axis the follwing will do:
DateAxis axis =(DateAxis)plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("MM=yyyy"));
Once the above date format override is set,the axis will auto-select a DateTickUnit and will ignore the formatting from the tick unit and use the override format instead.
I am including the following program which also features in the JFreeCharts Developer's Guide. If some of you have gone through the guide, and if you happened to try out
the following code, you will notice that I have excluded the "public static JPanel createDemoPanel()" method from this code snippet because for this example the method doesn't
serve any purpose.
Other points not explained in the guide are:
In the code section where the renderer is being customized this line "renderer.setDefaultShapesFilled(true);" is there but it is not required because the shape that we have picked for
displaying a mark is not hollow type.
The exact code I worked on can't be displayed here because of my company policy but I would refine on somethings which I did different from what appears below:
As you can see below, the timeseries objects are being hand carved one after another, in my case, this done in a for loop based on matching criterion of some of the
persistent properties of POJOs. In other words, I filled up my time series objects with dates from database based on the business requirement. You could do the same, no matter whether you use
Hibernate with Spring and POJOs or some other framework, JFreeChart's TimeSeries isn't meant for one set of implementation and environment.
package name.your.own;
import java.awt.Color;
import java.text.SimpleDateFormat;
import javax.swing.JPanel;
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.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;
public class TimeSeriesDemo extends ApplicationFrame
{
public TimeSeriesDemo(String title)
{
super(title);
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 static JFreeChart createChart(XYDataset dataset)
{
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Legal & General Unit Trust Prices", // title
"Date", // x-axis label
"Price Per Unit", // y-axis label
dataset, // data
true, // create legend?
true, // generate tooltips?
false // generate URLs?
);
chart.setBackgroundPaint(Color.white);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
XYItemRenderer r = plot.getRenderer();
if (r instanceof XYLineAndShapeRenderer)
{
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
renderer.setDefaultShapesVisible(true);
renderer.setDefaultShapesFilled(true);
}
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
return chart;
}
private static XYDataset createDataset()
{
TimeSeries s1 = new TimeSeries("L&G European Index Trust", Month.class);
s1.add(new Month(2, 2001), 181.8);s1.add(new Month(3, 2001), 167.3);s1.add(new Month(4, 2001), 153.8);s1.add(new Month(5, 2001), 167.6);
s1.add(new Month(6, 2001), 158.8);s1.add(new Month(7, 2001), 148.3);s1.add(new Month(8, 2001), 153.9);s1.add(new Month(9, 2001), 142.7);
s1.add(new Month(10, 2001), 123.2);s1.add(new Month(11, 2001), 131.8);s1.add(new Month(12, 2001), 139.6);s1.add(new Month(1, 2002), 142.9);
s1.add(new Month(2, 2002), 138.7);s1.add(new Month(3, 2002), 137.3);s1.add(new Month(4, 2002), 143.9);s1.add(new Month(5, 2002), 139.8);
s1.add(new Month(6, 2002), 137.0);s1.add(new Month(7, 2002), 132.8);
TimeSeries s2 = new TimeSeries("L&G UK Index Trust", Month.class);
s2.add(new Month(2, 2001), 129.6);s2.add(new Month(3, 2001), 123.2);s2.add(new Month(4, 2001), 117.2);s2.add(new Month(5, 2001), 124.1);
s2.add(new Month(6, 2001), 122.6);s2.add(new Month(7, 2001), 119.2);s2.add(new Month(8, 2001), 116.5);s2.add(new Month(9, 2001), 112.7);
s2.add(new Month(10, 2001), 101.5);s2.add(new Month(11, 2001), 106.1);s2.add(new Month(12, 2001), 110.3);s2.add(new Month(1, 2002), 111.7);
s2.add(new Month(2, 2002), 111.0);s2.add(new Month(3, 2002), 109.6);s2.add(new Month(4, 2002), 113.2);s2.add(new Month(5, 2002), 111.6);
s2.add(new Month(6, 2002), 108.8);s2.add(new Month(7, 2002), 101.6);
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(s1);
dataset.addSeries(s2);
dataset.setDomainIsPointsInTime(true);
return dataset;
}
public static void main(String[] args)
{
TimeSeriesDemo demo = new TimeSeriesDemo("Time Series Demo 1");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
The above examples and composition, entirely have been about still data, JFreeChart has interfaces for developing a chart system for live and streaming data as well, for example the guide talks about a little app for streaming the system resources usage in terms of dynamic charts. But the guide doesn't support it with high jinks because it could be very slow if you are crunching sizable amount of data because each time a dataset is updated, the ChartPanel reacts by redrawing the entire chart.
Showing posts with label TimeSeries. Show all posts
Showing posts with label TimeSeries. Show all posts
Tuesday, December 30, 2008
Saturday, December 27, 2008
Free JFreeCharts Info.
I had to use JFreeCharts all of a sudden, kind of in a rush. I had no previous information about it until I could get hold of a free JFreeCharts Developer Guide, I am lucky that I could use it since my company paid for it.
After accomplishing a few tasks using JFreeCharts, I wanted to summarize what I discovered about JFreeCharts. Well, without any further due I am going to get right into it:
To start with, it is an API to give a "face" to your data, so in simpler terms JFreeCharts helps you turn numbers into diagrams and the output diagrams can be be piped into as a PDF(using iText) or SVG(using batik) file. It fits well in a situation when you as a developer have to convey the meaning of the data in the database to a separate team like business or maybe the architecture team.
We have a tool that we developed in-hosue, the tool basically saves time since it makes querying the database a lot easier. There are tables in the database that tell how many times queries were fired and how many people were accessing the database and doing the four holy things that we always do with the database(add, edit, get, delete). Now, using the JFreeCharts methods I could give a meaningful representation to the logs in these tables, in terms of Pie Charts, Bar Charts, Line Charts, Scatter Plots, Time Series Charts etc.
I will get into details of my accomplishments with JFreeCharts but before thast just for a simple overview, the following is a list of packages into which all of the class libraries are bundled, for a resonoably advanced reader, the hierarchial names should give an initial insight into what can be accomplished with the classes and inturn the methods inside them.
(for brevity I have put everything that can be dotted in the parenthesis)
org.jfree.chart (annotations, axis, block, entity, event, imagemap, labels, needle, plot, renderer, renderer.category, renderer.xy, servlet, title, ui, urls,
(for brevity I have put everything that can be dotted after "data" in parenthesis)
org.jfree.chart.data (category, contour, function, gantt, general, jdbc, statistics, time, xml, xy)
Pie Chart can be generated using data which can be cast to the PieDataset interface and the same chart can be displayed with a 3D effect.
Data which confirms to the CategoryDatset interface can be displayed as Bar Chart and/or Line Chart
Similarly, data that confirms to the XYDataset interface can be used to generate a range of chart types. By default lines are drawn between datasets. So one can give just the X, Y data points and the interface will draw the line to connect them by default. Extentions of XYDataset such as HighLowDataset and IntervalXYDataset can be used to high-low-open-close data and histograms respectively.
Area Charts and Stacked Area Charts can be generated using data of CategoryDataset and XYDataset interface types. The list of kids of Charts that can be generated using JFreeCharts goes on and on, you name it.
There are three clear-cut steps that goes into getting data, presenting data and displaying the data into diagrams, the steps are as follows:
The steps:
1. Create the appropriate dataset using the data in hand(technically in the tables of your database system)
2. "Presenting the Data" created(or cast) in step 1. Involves creating a JFreeChart object which will draw the chart, eg. ChartFactory can draw the data in step 1.
3. The final step is "Displaying Chart" for eg. displaying the chart in a frame on the screen.
// Step 1 (creating the dataset, in other terms casting your data into a JFreeChart Dataset type)
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("Amar" , 30.4);
dataset.setValue("Akbar" , 19.6);
dataset.setValue("Anthony" , 85.5);
//Step 2 (creating the chart)
JFreeChart chart = ChartFactory.createPieChart("The Chart", dataset, true, true, false);//starting from the frist boolean: legend, tooltips, urls
//Step 3 (creating and displaying a frame)
ChartFrame frame = new ChartFrame("Example", chart);
frame.pack();
frame.setVisible(true);
Once a chart object is created in step 2, a plethora of further customization eg. setting background color, getting the plot object fromt he chart object for further customization and in-turn a "renderer" object can be obtained from the the plot object to customize colors and other like attributes.
Please copy and paste the following for a google search box and click "Google Search" button, to move onto the second of my article on TimeSeriesCharts and how I used it. The text to search for in google is:
Free JFreeCharts Guide, JFreeCharts developer guide, TimeSeries, XYDataSet, Pie Chart, Bar Chart, JFreeCharts API, Premium JFreeCharts
After accomplishing a few tasks using JFreeCharts, I wanted to summarize what I discovered about JFreeCharts. Well, without any further due I am going to get right into it:
To start with, it is an API to give a "face" to your data, so in simpler terms JFreeCharts helps you turn numbers into diagrams and the output diagrams can be be piped into as a PDF(using iText) or SVG(using batik) file. It fits well in a situation when you as a developer have to convey the meaning of the data in the database to a separate team like business or maybe the architecture team.
We have a tool that we developed in-hosue, the tool basically saves time since it makes querying the database a lot easier. There are tables in the database that tell how many times queries were fired and how many people were accessing the database and doing the four holy things that we always do with the database(add, edit, get, delete). Now, using the JFreeCharts methods I could give a meaningful representation to the logs in these tables, in terms of Pie Charts, Bar Charts, Line Charts, Scatter Plots, Time Series Charts etc.
I will get into details of my accomplishments with JFreeCharts but before thast just for a simple overview, the following is a list of packages into which all of the class libraries are bundled, for a resonoably advanced reader, the hierarchial names should give an initial insight into what can be accomplished with the classes and inturn the methods inside them.
(for brevity I have put everything that can be dotted in the parenthesis)
org.jfree.chart (annotations, axis, block, entity, event, imagemap, labels, needle, plot, renderer, renderer.category, renderer.xy, servlet, title, ui, urls,
(for brevity I have put everything that can be dotted after "data" in parenthesis)
org.jfree.chart.data (category, contour, function, gantt, general, jdbc, statistics, time, xml, xy)
Pie Chart can be generated using data which can be cast to the PieDataset interface and the same chart can be displayed with a 3D effect.
Data which confirms to the CategoryDatset interface can be displayed as Bar Chart and/or Line Chart
Similarly, data that confirms to the XYDataset interface can be used to generate a range of chart types. By default lines are drawn between datasets. So one can give just the X, Y data points and the interface will draw the line to connect them by default. Extentions of XYDataset such as HighLowDataset and IntervalXYDataset can be used to high-low-open-close data and histograms respectively.
Area Charts and Stacked Area Charts can be generated using data of CategoryDataset and XYDataset interface types. The list of kids of Charts that can be generated using JFreeCharts goes on and on, you name it.
There are three clear-cut steps that goes into getting data, presenting data and displaying the data into diagrams, the steps are as follows:
The steps:
1. Create the appropriate dataset using the data in hand(technically in the tables of your database system)
2. "Presenting the Data" created(or cast) in step 1. Involves creating a JFreeChart object which will draw the chart, eg. ChartFactory can draw the data in step 1.
3. The final step is "Displaying Chart" for eg. displaying the chart in a frame on the screen.
// Step 1 (creating the dataset, in other terms casting your data into a JFreeChart Dataset type)
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("Amar" , 30.4);
dataset.setValue("Akbar" , 19.6);
dataset.setValue("Anthony" , 85.5);
//Step 2 (creating the chart)
JFreeChart chart = ChartFactory.createPieChart("The Chart", dataset, true, true, false);//starting from the frist boolean: legend, tooltips, urls
//Step 3 (creating and displaying a frame)
ChartFrame frame = new ChartFrame("Example", chart);
frame.pack();
frame.setVisible(true);
Once a chart object is created in step 2, a plethora of further customization eg. setting background color, getting the plot object fromt he chart object for further customization and in-turn a "renderer" object can be obtained from the the plot object to customize colors and other like attributes.
Please copy and paste the following for a google search box and click "Google Search" button, to move onto the second of my article on TimeSeriesCharts and how I used it. The text to search for in google is:
Free JFreeCharts Guide, JFreeCharts developer guide, TimeSeries, XYDataSet, Pie Chart, Bar Chart, JFreeCharts API, Premium JFreeCharts
Free Premium JFreeCharts Info.
I had to use JFreeCharts all of a sudden, kind of in a rush. I had no previous information about it until I could get hold of a free JFreeCharts Developer Guide, I am lucky that I could use it since my company paid for it.
After accomplishing a few tasks using JFreeCharts, I wanted to summarize what I discovered about JFreeCharts. Well, without any further due I am going to get right into it:
To start with, it is an API to give a "face" to your data, so in simpler terms JFreeCharts helps you turn numbers into diagrams and the output diagrams can be be piped into as a PDF(using iText) or SVG(using batik) file. It fits well in a situation when you as a developer have to convey the meaning of the data in the database to a separate team like business or maybe the architecture team.
We have a tool that we developed in-hosue, the tool basically saves time since it makes querying the database a lot easier. There are tables in the database that tell how many times queries were fired and how many people were accessing the database and doing the four holy things that we always do with the database(add, edit, get, delete). Now, using the JFreeCharts methods I could give a meaningful representation to the logs in these tables, in terms of Pie Charts, Bar Charts, Line Charts, Scatter Plots, Time Series Charts etc.
I will get into details of my accomplishments with JFreeCharts but before thast just for a simple overview, the following is a list of packages into which all of the class libraries are bundled, for a resonoably advanced reader, the hierarchial names should give an initial insight into what can be accomplished with the classes and inturn the methods inside them.
(for brevity I have put everything that can be dotted in the parenthesis)
org.jfree.chart (annotations, axis, block, entity, event, imagemap, labels, needle, plot, renderer, renderer.category, renderer.xy, servlet, title, ui, urls,
(for brevity I have put everything that can be dotted after "data" in parenthesis)
org.jfree.chart.data (category, contour, function, gantt, general, jdbc, statistics, time, xml, xy)
Pie Chart can be generated using data which can be cast to the PieDataset interface and the same chart can be displayed with a 3D effect.
Data which confirms to the CategoryDatset interface can be displayed as Bar Chart and/or Line Chart
Similarly, data that confirms to the XYDataset interface can be used to generate a range of chart types. By default lines are drawn between datasets. So one can give just the X, Y data points and the interface will draw the line to connect them by default. Extentions of XYDataset such as HighLowDataset and IntervalXYDataset can be used to high-low-open-close data and histograms respectively.
Area Charts and Stacked Area Charts can be generated using data of CategoryDataset and XYDataset interface types. The list of kids of Charts that can be generated using JFreeCharts goes on and on, you name it.
There are three clear-cut steps that goes into getting data, presenting data and displaying the data into diagrams, the steps are as follows:
The steps:
1. Create the appropriate dataset using the data in hand(technically in the tables of your database system)
2. "Presenting the Data" created(or cast) in step 1. Involves creating a JFreeChart object which will draw the chart, eg. ChartFactory can draw the data in step 1.
3. The final step is "Displaying Chart" for eg. displaying the chart in a frame on the screen.
Once a chart object is created in step 2, a plethora of further customization eg. setting background color, getting the plot object fromt he chart object for further customization and in-turn a "renderer" object can be obtained from the the plot object to customize colors and other like attributes.
Thanks for reading and Thanks for stopping by!!
~Nirmal
Keep your friends close and your enemies closer.
Sun-Tzu
After accomplishing a few tasks using JFreeCharts, I wanted to summarize what I discovered about JFreeCharts. Well, without any further due I am going to get right into it:
To start with, it is an API to give a "face" to your data, so in simpler terms JFreeCharts helps you turn numbers into diagrams and the output diagrams can be be piped into as a PDF(using iText) or SVG(using batik) file. It fits well in a situation when you as a developer have to convey the meaning of the data in the database to a separate team like business or maybe the architecture team.
We have a tool that we developed in-hosue, the tool basically saves time since it makes querying the database a lot easier. There are tables in the database that tell how many times queries were fired and how many people were accessing the database and doing the four holy things that we always do with the database(add, edit, get, delete). Now, using the JFreeCharts methods I could give a meaningful representation to the logs in these tables, in terms of Pie Charts, Bar Charts, Line Charts, Scatter Plots, Time Series Charts etc.
I will get into details of my accomplishments with JFreeCharts but before thast just for a simple overview, the following is a list of packages into which all of the class libraries are bundled, for a resonoably advanced reader, the hierarchial names should give an initial insight into what can be accomplished with the classes and inturn the methods inside them.
(for brevity I have put everything that can be dotted in the parenthesis)
org.jfree.chart (annotations, axis, block, entity, event, imagemap, labels, needle, plot, renderer, renderer.category, renderer.xy, servlet, title, ui, urls,
(for brevity I have put everything that can be dotted after "data" in parenthesis)
org.jfree.chart.data (category, contour, function, gantt, general, jdbc, statistics, time, xml, xy)
Pie Chart can be generated using data which can be cast to the PieDataset interface and the same chart can be displayed with a 3D effect.
Data which confirms to the CategoryDatset interface can be displayed as Bar Chart and/or Line Chart
Similarly, data that confirms to the XYDataset interface can be used to generate a range of chart types. By default lines are drawn between datasets. So one can give just the X, Y data points and the interface will draw the line to connect them by default. Extentions of XYDataset such as HighLowDataset and IntervalXYDataset can be used to high-low-open-close data and histograms respectively.
Area Charts and Stacked Area Charts can be generated using data of CategoryDataset and XYDataset interface types. The list of kids of Charts that can be generated using JFreeCharts goes on and on, you name it.
There are three clear-cut steps that goes into getting data, presenting data and displaying the data into diagrams, the steps are as follows:
The steps:
1. Create the appropriate dataset using the data in hand(technically in the tables of your database system)
2. "Presenting the Data" created(or cast) in step 1. Involves creating a JFreeChart object which will draw the chart, eg. ChartFactory can draw the data in step 1.
3. The final step is "Displaying Chart" for eg. displaying the chart in a frame on the screen.
// Step 1 (creating the dataset, in other terms casting your data into a JFreeChart Dataset type)
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("Amar" , 30.4);
dataset.setValue("Akbar" , 19.6);
dataset.setValue("Anthony" , 85.5);
//Step 2 (creating the chart)
JFreeChart chart = ChartFactory.createPieChart("The Chart", dataset, true, true, false);//starting from the frist boolean: legend, tooltips, urls
//Step 3 (creating and displaying a frame)
ChartFrame frame = new ChartFrame("Example", chart);
frame.pack();
frame.setVisible(true);
Once a chart object is created in step 2, a plethora of further customization eg. setting background color, getting the plot object fromt he chart object for further customization and in-turn a "renderer" object can be obtained from the the plot object to customize colors and other like attributes.
Thanks for reading and Thanks for stopping by!!
~Nirmal
Keep your friends close and your enemies closer.
Sun-Tzu
Subscribe to:
Posts (Atom)