Import Org.jfree.ui.ApplicationFrame;
Import Iaqms.com.CustomRenderer;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import Javax.swing.JPanel;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.GradientPaint;
Import java.awt.RenderingHints;
Import Javax.swing.JPanel;
Import Org.jfree.chart.ChartFactory;
Import Org.jfree.chart.ChartFrame;
Import Org.jfree.chart.ChartPanel;
Import Org.jfree.chart.JFreeChart;
Import Org.jfree.chart.StandardChartTheme;
Import Org.jfree.chart.axis.CategoryAxis;
Import org.jfree.chart.axis.CategoryLabelPositions;
Import Org.jfree.chart.axis.NumberAxis;
Import Org.jfree.chart.axis.NumberTickUnit;
Import Org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
Import Org.jfree.chart.labels.StandardPieSectionLabelGenerator;
Import Org.jfree.chart.plot.CategoryPlot;
Import Org.jfree.chart.plot.PiePlot;
Import org.jfree.chart.plot.PlotOrientation;
Import Org.jfree.chart.plot.RingPlot;
Import Org.jfree.chart.renderer.category.BarRenderer;
Import Org.jfree.chart.title.LegendTitle;
Import Org.jfree.chart.title.TextTitle;
Import Org.jfree.data.category.CategoryDataset;
Import Org.jfree.data.category.DefaultCategoryDataset;
Import Org.jfree.data.general.DefaultPieDataset;
Import Org.jfree.data.general.PieDataset;
Import org.jfree.ui.RefineryUtilities;
public class Piechartdb extends Applicationframe {
Private static final long serialversionuid = 1L;
public static void Main (string[] args) {
Piechartdb Piechart = new Piechartdb ("pie chart");
Piechart.pack ();
Refineryutilities.centerframeonscreen (Piechart);
Piechart.setvisible (TRUE);
}
Constructor Generation instance
Public Piechartdb (String number) {
Super (number);
Setcontentpane (Createpiepanel ());
}
Panels that generate reports
public static JPanel Createpiepanel () {
Jfreechart Jfreechart = Createchart (CreateDataSet ());
return new Chartpanel (Jfreechart, false);
}
private static Jfreechart Createchart (Piedataset DataSet) {
Solve garbled problem (cannot display Chinese problem)
Standardcharttheme mcharttheme = new Standardcharttheme ("CN");
Mcharttheme.setlargefont (New Font ("Blackbody", Font.Bold, 20));
Mcharttheme.setextralargefont (New Font ("Arial", Font.plain, 15)); Title
Mcharttheme.setregularfont (New Font ("Arial", Font.plain, 15));
Chartfactory.setcharttheme (Mcharttheme);
Object that produces a pie chart
Jfreechart chart = Chartfactory.createpiechart (
"Book Sales Chart",//Icon title
DataSet,//Data
true,//whether the legend is displayed
false,//whether to show ToolTips
false//whether to generate a URL
);
Set a series of properties
Get the plot object for a pie chart
Pieplot plot = (Pieplot) chart.getplot ();
The annotation used to display the callout, {0}:{1} to set the format of the display
Plot.setlabelgenerator (New Standardpiesectionlabelgenerator ("{0}:{1}"));
Plot.setbackgroundpaint (Color.gray);//Set Background color
Set a simple label
Plot.setsimplelabels (TRUE);
Title
Texttitle Texttitle=chart.gettitle ();
Texttitle.setfont (New Font ("Arial", Font.Bold, 30));
Icon
Legendtitle legendtitle =chart.getlegend ();
Legendtitle.setitemfont (New Font ("Arial", Font.Bold, 14));
Set background transparency (between 0-1.0)
Plot.setbackgroundalpha (0.5f);
Set foreground transparency (between 0-1.0)
Plot.setforegroundalpha (0.60f);
return chart;
}
private static Piedataset CreateDataSet () {
double[][] table = null;
string[] name = NULL;
string[] number = NULL;
int result = 0;
try{
Class.forName ("Com.mysql.jdbc.Driver"). newinstance ();
Connection conn = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/sale", "root", "fab");
ResultSet rs = conn.createstatement (). ExecuteQuery ("Select Name,number from Sale");
Number of rows obtained
if (Rs.last ()) {
result = Rs.getrow ();
Rs.beforefirst (); Pointer rollback
}
Table = new Double[result][rs.getmetadata (). getColumnCount ()-1];//defines a two-dimensional array that holds data
name = new String[result];
Number = new String[rs.getmetadata (). getColumnCount ()-1]; Defines a one-dimensional array that holds the header of a data table
Rs.next ();
for (int j = 0; J < Rs.getmetadata (). getColumnCount ()-1; j + +) {
NUMBER[J] = Rs.getmetadata (). getColumnName (j + 2); Remove the table header and store the array
}
int i = 0;
Rs.beforefirst ();
while (Rs.next ()) {
Name[i] = rs.getstring (1);
for (int j = 0; J < Rs.getmetadata (). getColumnCount ()-1; j + +)
TABLE[I][J] = rs.getdouble (j + 2); Take out the data and store it in a two-dimensional array
i++;
}
Conn.close (); Close connection
Rs.close (); Close Query
} catch (Exception ex) {
System.err.println ("Exception:" + ex.getmessage ());
}
Create a dataset of type Defaultpiedataset, add data in a parallel dataset (data taken from the database)
Defaultpiedataset DataSet = new Defaultpiedataset ();
for (int i = 0; i < number.length; i++)
for (int j = 0; J < result; J + +) {
Dataset.setvalue (Name[j],table[j][i]);
Tune trial
System.out.println (Table[j][i]);
System.out.println (Name[j]);
}
return dataset;
}
}
Database script:
CREATE DATABASE/*!32312 IF not exists*/' sale '/*!40100 DEFAULT CHARACTER SET GBK */;
Use ' sale ';
DROP TABLE IF EXISTS ' sale ';
CREATE TABLE ' Sale ' (
' Name ' varchar (COMMENT) default NULL ' title ',
' Number ' varchar (total) default NULL COMMENT ' quantity '
) Engine=innodb DEFAULT CHARSET=GBK;
Insert INTO ' sale ' (' name ', ' number ') VALUES (' C + + ', ' + '), (' Java ', ' 20 '), (' Data structure ', ' 30 '), (' ann ', ' + '), (' IOS ', ' 25 '), (' Computer network ', ' 28 ');
Run the result diagram:
Jfreechart in conjunction with MySQL database