Logic For Graphs using JFreeChart

How to write the logic to draw a graph of the power consumed by a consumer in specific dates with a const line showing the max power can be consumed.

Assuming my data is like this:

15-12-2012 150

16-12-2012 200

Max power ------400
 

Coding example:

Try the following code:

import java.awt.*;

import java.text.*;

import javax.swing.*;
 

import org.jfree.chart.*;

import org.jfree.chart.axis.DateAxis;

import org.jfree.chart.plot.XYPlot;

import org.jfree.chart.renderer.xy.*;

import org.jfree.data.time.*;

import org.jfree.data.xy.XYDataset;

import org.jfree.ui.*;
 

public class LineChart extends ApplicationFrame {

public LineChart(String title) {

super(title);

TimeSeries s1 = new TimeSeries("Power", Day.class);

s1.add(new Day(15,12, 2009), 150);

s1.add(new Day(16,12, 2009), 200);

TimeSeriesCollection dataset = new TimeSeriesCollection();

dataset.addSeries(s1);

dataset.setDomainIsPointsInTime(true);
 
 

JFreeChart chart = ChartFactory.createTimeSeriesChart(

"Power consumed by a consumer in specific date", 

"Date", 

"Power", 

dataset, 

true, 

true, 

false 

);

ChartPanel chartPanel = new ChartPanel(chart, false);

chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));

chartPanel.setMouseZoomable(true, false);

setContentPane(chartPanel);

XYPlot plot = (XYPlot) chart.getPlot();

XYItemRenderer r = plot.getRenderer();

if (r instanceof XYLineAndShapeRenderer) {

XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;

}
 
 

DateAxis axis = (DateAxis) plot.getDomainAxis();

axis.setDateFormatOverride(new SimpleDateFormat("dd-MM-yyyy"));

}

public static void main(String[]args){

LineChart chart = new LineChart("Chart");

chart.pack();

chart.setVisible(true);
 

}
 

}
 

Note:

JFreeChart is a free open source java chart library. David Gilbert founded the JFreeChart project in February 2000. Nowadays, it is used by around 40000 to 50000 developers. It is used to generate the charts like Area and Line Charts, Pie charts, Bar Charts, Bubble Charts, Gantt Charts. Developers get a better choice to add professional quality charts in swing and web based application by JFreeChart.

Java Tips

See also
Sorting Objects Using Collections Sort

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.