Creating Time Series charts using JFreeChart
I have attached a code snippet which can give you a basic idea on creating a time-series chart using JFreeChart. This is a part of JFreeChart samples which come along with the free distribution of JFreeChart.
// Creating a dataset TimeSeries s1 = new TimeSeries("Name of the Time series", 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);
// Creating the chart JFreeChart chart = ChartFactory.createTimeSeriesChart( "Title of the chart", // title "X axis value", // x-axis label "Y axis value", // y-axis label dataset, // data true, // create legend? true, // generate tooltips? false // generate URLs?);
// Setting the chart properties chart.setBackgroundPaint(Color.white);
// Setting the plot properties XYPlot plot = (XYPlot) chart.getPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); 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);
Online JFreeChart Demo (Java WebStart).
(What is Java WebStart Technology? )
Reply
You must be logged in to post a comment.