hai this is anitha , i am persuing my B.Tech. 3rd year,i am doing a mini project,my task is to develope a code in java,to generate a graph using sql database values. so,kindly help me with my mini project.please try to reply me as early as possible
Java Line Chart using jfreechart
The given code retrieves the data from the database and create a line chart. The jfreechart api has provided some classes which can be used directly to connect database values with the chart.
import java.sql.*; import org.jfree.ui.*; import org.jfree.data.*; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.jdbc.JDBCCategoryDataset; public class Chart{ public static void main(String[] args) throws Exception { String query = "SELECT * from cricket_match"; JDBCCategoryDataset dataset = new JDBCCategoryDataset("jdbc:mysql://localhost:3306/test", "com.mysql.jdbc.Driver","root", "root"); dataset.executeQuery(query); JFreeChart chart = ChartFactory.createLineChart("Score", "Runs", "Overs", 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); } }
Java XY Line Chart using jfreechart
The given code retrieves the data from the database and create a xy line chart. The jfreechart api has provided some classes which can be used directly to connect database values with the chart.
import java.sql.*; import org.jfree.chart.*; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.general.DefaultPieDataset; import org.jfree.data.xy.*; import org.jfree.data.*; public class Chart{ public static void main(String arg[])throws Exception{ Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root" ); Statement st=conn.createStatement(); ResultSet rs=st.executeQuery("SELECT * from cricket_match"); XYSeries series = new XYSeries(""); while(rs.next()){ series.add((double)rs.getInt("runs"),(double)rs.getInt("overs")); } XYDataset xyDataset = new XYSeriesCollection(series); JFreeChart chart = ChartFactory.createXYLineChart ("Match Score", "Runs", "Overs",xyDataset, PlotOrientation.VERTICAL, true, true, false); ChartFrame frame1=new ChartFrame("",chart); frame1.setVisible(true); frame1.setSize(300,300); } }
Java Bar Chart using jfreechart
The given code retrieves the data from the database and create a bar chart. The jfreechart api has provided some classes which can be used directly to connect database values with the chart.
import java.sql.*; import java.io.*; 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.createBarChart3D("Test", "Id", "Score", dataset, PlotOrientation.VERTICAL, true, true, false); try { ChartUtilities.saveChartAsJPEG(new File("C:/chart.jpg"), chart,400, 300); } catch (IOException e) { System.out.println("Problem in creating chart."); } } }
Java Pie Chart using jfreechart
The given code retrieves the data from the database and create a pie chart. The jfreechart api has provided some classes which can be used directly to connect database values with the chart.
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.data.*; import org.jfree.data.jdbc.JDBCPieDataset; public class Chart { public static void main(String[] args) throws Exception { String query = "SELECT * from chart"; JDBCPieDataset dataset = new JDBCPieDataset( "jdbc:mysql://localhost:3306/test", "com.mysql.jdbc.Driver", "root", "root"); dataset.executeQuery(query); JFreeChart chart = ChartFactory.createPieChart("Test", dataset, 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); } }
Ads