Problem Description
There are sometimes requirement to draw multiple plots against time in any java application. JFreechart provides an excellent API for this purpose. But knowing and remembering all the methods in this API is painfull and time consumingSolution Description
1) Saves the chart in some specified location of local machine as JPG image.
2) Draws the chart on the screen using Swing API.
Dependencies
jfreechart-1.0.4.jar
jcommon-1.0.9.jar
2) Draws the chart on the screen using Swing API.
Dependencies
jfreechart-1.0.4.jar
jcommon-1.0.9.jar
Sample Code
MyMain.java
package graph;
/**
* Helper class. Used to testing purpose
*
*/
public class Mymain {
/**
*
* @param args
*/
public static void main(String[] args) {
System.out.println("MAIN : START");
Graph graph = new Graph();
Value value;
Value localvalues[]= new Value[names.length];
graph.setNames(names);
for (int i=0;i
value = new Value();
value.setI(values[i]);
localvalues[i] = value;
}
graph.setValues(localvalues);
new MyGraphs().createChart(graph,GraphConstants.PERIOD_TYPE_MONTHLY);
System.out.println("MAIN : END");
}
protected final static Integer values[][] = {
{12,1,123,10,200,12,45,15},
{1,12,10,112,0,122,15},
{121,34,21,13,90,6,78},
{5,67,15,104,2,123,4}
};
protected final static String names[] = {
"Report1",
"Report2",
"Report3",
"Report4"
};
}
Graph.java
package graph;
/**
*
*
*/
public class Graph {
String names[];
Value values[];
/**
*
* @return names
*/
public String[] getNames() {
return names;
}
/**
*
* @param names
*/
public void setNames(String[] names) {
this.names = names;
}
/**
*
* @return values
*/
public Value[] getValues() {
return values;
}
/**
*
* @param values
*/
public void setValues(Value[] values) {
this.values = values;
}
}
GraphConstants.java
package graph;
/**
* Constant file
*
*/
public interface GraphConstants {
/**period type if weekly*/
public final int PERIOD_TYPE_WEEKLY = 0;
/**period type if monthly*/
public final int PERIOD_TYPE_MONTHLY = 1;
/**period type if monthly*/
public final int PERIOD_TYPE_QUATERLY = 2;
/** the location of the file to be stored*/
public static final String FILE_PATH = "d:\\chart.jpg";
/**plotting details : width of x-axis*/
public static final int X_AXIS_WIDTH = 500;
/**plotting details : width of y-axis*/
public static final int Y_AXIS_WIDTH = 300;
/**graph name : This part should be moved to property file*/
public static final String GRAPH_NAME = "Graph Name";
/**x-axis label : This part should be moved to property file*/
public static final String X_AXIS = "X Axis";
/**y-axis label : This part should be moved to property file*/
public static final String Y_AXIS = "Y Axis";
/** Sample data error. This part should be moved to property file*/
public static final String ERROR_MESSAGE = "Name and values must be equal in number";
/** Null string*/
public static final String NULL = null;
}
MyGraphs.java
package graph;
import java.awt.BorderLayout;
import java.io.File;
import java.io.IOException;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.XYToolTipGenerator;
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.Quarter;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.Week;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
/**
* This class is responsible for creating the graph using the data provided.
*
*/
public class MyGraphs extends ApplicationFrame implements XYToolTipGenerator{
/**
*
*/
private static final long serialVersionUID = -4019769545419857920L;
/** JFreeChart */
private JFreeChart chart;
/** Time series collection */
private TimeSeriesCollection dataset;
/**
* no-arg constructor
*/
public MyGraphs(){
this(GraphConstants.GRAPH_NAME);
}
/**
*
* @param title
*/
public MyGraphs(String title){
super(title);
}
/**
* This method takes care of all the aspects of drawing the graph
* @param graph
* @param periodType
*/
protected void createChart(Graph graph,int periodType) {
dataset = new TimeSeriesCollection();
// Add series data from table model to chart
try{
addSeriesToChart(dataset, graph,periodType);
// Create chart
chart = ChartFactory.createTimeSeriesChart(GraphConstants.GRAPH_NAME,GraphConstants.X_AXIS,GraphConstants.Y_AXIS,
dataset, true, true, false);
XYPlot plot = (XYPlot) chart.getPlot();
//Configured the axis of the graph
configureAxis(plot);
//Configures the rendered
configureRenderer(plot);
//Saves the graph image at the mentioned file path
saveChart();
//Draws the chart on screen using java swing
drawChart();
}catch (IOException ioe){
ioe.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* saves the chart as a jpg image at specified location.
*/
private void saveChart()throws IOException{
ChartUtilities.saveChartAsJPEG(new File(GraphConstants.FILE_PATH), chart, GraphConstants.X_AXIS_WIDTH, GraphConstants.Y_AXIS_WIDTH);
}
/**
* creates the graph on screen, with the aid of the class XYChartDemo.
*/
private void drawChart(){
this.setSize(new java.awt.Dimension(GraphConstants.X_AXIS_WIDTH,GraphConstants.Y_AXIS_WIDTH));
ChartPanel chartPanel = new ChartPanel(chart);
// size
chartPanel.setPreferredSize(new java.awt.Dimension(GraphConstants.X_AXIS_WIDTH,GraphConstants.Y_AXIS_WIDTH));
final JPanel main = new JPanel(new BorderLayout());
final JPanel optionsPanel = new JPanel();
main.add(optionsPanel, BorderLayout.SOUTH);
main.add(chartPanel);
setContentPane(main);
RefineryUtilities.centerFrameOnScreen(this);
setVisible(true);
}
/**
*
* @param dataset
* @param graph
* @param periodType
* @throws Exception
*/
protected void addSeriesToChart(TimeSeriesCollection dataset,Graph graph,int periodType)throws Exception{
int count_from = 0;
int count_to = graph.getNames().length;
if (graph.getNames().length!= graph.getValues().length){
throw new Exception(GraphConstants.ERROR_MESSAGE);
}
for (int j = count_from;j
TimeSeries g = new TimeSeries(graph.getNames()[j], getTimePeriod(periodType).getClass());
RegularTimePeriod start = getTimePeriod(periodType);
for (int i = 0; i
start = start.previous();
double value = (graph.getValues()[j].getI()[i]).doubleValue();
g.add(start,value);
}
dataset.addSeries(g);
}
}
/**
* @param xyPlot
*/
protected void configureAxis(XYPlot plot){
// Make crosshair visible for Range and Domain axis.
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
// Set upper margin of Domain axis for proper display of Item label.
plot.getDomainAxis().setUpperMargin(.05);
// Set margin of range axis for proper display of Item label.
plot.getRangeAxis().setUpperMargin(0.15);
plot.getRangeAxis().setLowerMargin(0.15);
//plot.getRangeAxis().setAutoRange(true);
}
/**
*
* @param plot
*/
protected void configureRenderer(XYPlot plot){
XYItemRenderer r = plot.getRenderer();
if (r instanceof XYLineAndShapeRenderer) {
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);
}
}
/**
*
* @param periodType
* @return
*/
protected RegularTimePeriod getTimePeriod(int periodType){
switch(periodType){
case GraphConstants.PERIOD_TYPE_WEEKLY:
return new Week();
case GraphConstants.PERIOD_TYPE_MONTHLY:
return new Month();
case GraphConstants.PERIOD_TYPE_QUATERLY:
return new Quarter();
default:
return new Month();
}
}
/**
* @param dataset
* @param series
* @param item
* @return tooltip
*/
public String generateToolTip(XYDataset dataset, int series, int item) {
String tooltip;
if(item == 0){
tooltip = GraphConstants.NULL;
}else{
tooltip = " ";
}
return tooltip;
}
}
No comments:
Post a Comment