Create Wind Chart Using JFreeChart
Create Wind Chart Using JFreeChart explains about creating a simple wind chart using JFreechart API
How To Create Wind Chart Using JFreeChart library?
JFreeChart is a free and open source java chart library used for creating professional quality charts. JFreeChart is purely written in java language, we can very easily incorporate JFreeChart in our java standalone and web applications.
Referenece - > http://www.jfree.org/jfreechart/
Here I am showing an example about How to create a simple Wind Chart using JFreeChart.
Required Libraries
You need to download
Following jar must be in classpath
- jfreechart-1.5.0.jar
- jcommon-1.0.24.jar
pom.xml
<dependency> <groupId>org.jfree</groupId> <artifactId>jfreechart</artifactId> <version>1.5.0</version> </dependency> <dependency> <groupId>org.jfree</groupId> <artifactId>jcommon</artifactId> <version>1.0.24</version> </dependency>
We can create following professional quality charts by using jfreechart.
- Single valued Charts such as compass, speedometer, thermometer
- Line Chart
- Pie Chart
- Bar Chart
- Bubble Chart
- Wind Chart
- Polar Chart
JFreeChart Example (Wind Chart)
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.DefaultWindDataset;
import org.jfree.data.xy.WindDataset;
public class JFreeChartWindChartExample extends JFrame {
private static final long serialVersionUID = 1L;
private static Calendar cal = new GregorianCalendar();
public JFreeChartWindChartExample(String applicationTitle) {
super(applicationTitle);
// Creates a sample dataset for wind chart
WindDataset dataSet = getWindDataset();
// Based on the dataset we are creating WindChart
JFreeChart windChart = ChartFactory.createWindPlot("WindChart Example", "Date", "Direction", dataSet, true, false, false);
// Adding chart into a chart panel
ChartPanel chartPanel = new ChartPanel(windChart);
// setting default size
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
// add to contentPane
setContentPane(chartPanel);
}
public static WindDataset getWindDataset() {
Object[][][] data = new Object[][][] { { { createDate(2012, Calendar.MARCH, 3), new Double(0.0), new Double(10.0) },
{ createDate(2012, Calendar.MARCH, 4), new Double(1.0), new Double(8.5) },
{ createDate(2012, Calendar.MARCH, 5), new Double(2.0), new Double(10.0) },
{ createDate(2012, Calendar.MARCH, 6), new Double(3.0), new Double(10.0) },
{ createDate(2012, Calendar.MARCH, 7), new Double(4.0), new Double(7.0) },
{ createDate(2012, Calendar.MARCH, 8), new Double(5.0), new Double(10.0) },
{ createDate(2012, Calendar.MARCH, 9), new Double(6.0), new Double(8.0) },
{ createDate(2012, Calendar.MARCH, 10), new Double(7.0), new Double(11.0) },
{ createDate(2012, Calendar.MARCH, 11), new Double(8.0), new Double(10.0) },
{ createDate(2012, Calendar.MARCH, 12), new Double(9.0), new Double(11.0) },
{ createDate(2012, Calendar.MARCH, 13), new Double(10.0), new Double(3.0) },
{ createDate(2012, Calendar.MARCH, 14), new Double(11.0), new Double(9.0) },
{ createDate(2012, Calendar.MARCH, 15), new Double(12.0), new Double(11.0) },
{ createDate(2012, Calendar.MARCH, 16), new Double(0.0), new Double(0.0) } } };
return new DefaultWindDataset(new String[] { "Wind!!" }, data);
}
private static Date createDate(int year, int month, int day) {
cal.set(year, month, day);
return cal.getTime();
}
public static void main(String[] args) {
JFreeChartWindChartExample chart = new JFreeChartWindChartExample("Wind Statistics");
chart.pack();
chart.setVisible(true);
}
}
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.DefaultWindDataset;
import org.jfree.data.xy.WindDataset;
public class JFreeChartWindChartExample extends JFrame {
private static final long serialVersionUID = 1L;
private static Calendar cal = new GregorianCalendar();
public JFreeChartWindChartExample(String applicationTitle) {
super(applicationTitle);
// Creates a sample dataset for wind chart
WindDataset dataSet = getWindDataset();
// Based on the dataset we are creating WindChart
JFreeChart windChart = ChartFactory.createWindPlot("WindChart Example", "Date", "Direction", dataSet, true, false, false);
// Adding chart into a chart panel
ChartPanel chartPanel = new ChartPanel(windChart);
// setting default size
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
// add to contentPane
setContentPane(chartPanel);
}
public static WindDataset getWindDataset() {
Object[][][] data = new Object[][][] { { { createDate(2012, Calendar.MARCH, 3), new Double(0.0), new Double(10.0) },
{ createDate(2012, Calendar.MARCH, 4), new Double(1.0), new Double(8.5) },
{ createDate(2012, Calendar.MARCH, 5), new Double(2.0), new Double(10.0) },
{ createDate(2012, Calendar.MARCH, 6), new Double(3.0), new Double(10.0) },
{ createDate(2012, Calendar.MARCH, 7), new Double(4.0), new Double(7.0) },
{ createDate(2012, Calendar.MARCH, 8), new Double(5.0), new Double(10.0) },
{ createDate(2012, Calendar.MARCH, 9), new Double(6.0), new Double(8.0) },
{ createDate(2012, Calendar.MARCH, 10), new Double(7.0), new Double(11.0) },
{ createDate(2012, Calendar.MARCH, 11), new Double(8.0), new Double(10.0) },
{ createDate(2012, Calendar.MARCH, 12), new Double(9.0), new Double(11.0) },
{ createDate(2012, Calendar.MARCH, 13), new Double(10.0), new Double(3.0) },
{ createDate(2012, Calendar.MARCH, 14), new Double(11.0), new Double(9.0) },
{ createDate(2012, Calendar.MARCH, 15), new Double(12.0), new Double(11.0) },
{ createDate(2012, Calendar.MARCH, 16), new Double(0.0), new Double(0.0) } } };
return new DefaultWindDataset(new String[] { "Wind!!" }, data);
}
private static Date createDate(int year, int month, int day) {
cal.set(year, month, day);
return cal.getTime();
}
public static void main(String[] args) {
JFreeChartWindChartExample chart = new JFreeChartWindChartExample("Wind Statistics");
chart.pack();
chart.setVisible(true);
}
}