Home Tutorial Java Jfreechart Create Line Graph using database values

 
 

Share on Google+Share on Google+
Create Line Graph using database values
Posted on: June 12, 2010 at 12:00 AM
Advertisement
In this section, you will learn how to create a x-y line graph by retrieving the values from the database.

Create Line Graph using database values

JFreeChart library has made programming very easy. Using its classes and their methods, you can create various charts. It also provides a way to create a chart with the values that has been retrieved from the database. In this section, you will learn how to create a x-y line graph by retrieving the values from the database.

Here is the code:

import java.sql.*;
import java.io.*;
import org.jfree.ui.*;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.*;
import org.jfree.data.jdbc.JDBCCategoryDataset;

public class Chart {
	public static void main(String[] args) throws Exception {
		String query = "SELECT * from chart";
		JDBCCategoryDataset dataset = new JDBCCategoryDataset(
				"jdbc:mysql://localhost:3306/test", "com.mysql.jdbc.Driver",
				"root", "root");

		dataset.executeQuery(query);
		JFreeChart chart = ChartFactory.createLineChart("Test", "Id", "Score",
				dataset, PlotOrientation.VERTICAL, true, true, false);
		ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
		ApplicationFrame f = new ApplicationFrame("Chart");
		f.setContentPane(chartPanel);
		f.pack();
		f.setVisible(true);
	}
}

Output:

Advertisement

Related Tags for Create Line Graph using database values:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 12, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com