Recently, in doing data analysis related work, the results of data analysis need to be visually displayed for fine-tuning. Here you need to use column chart, pie chart, line chart and so on, Java has Jfreechart library to achieve data statistics, Jfreechart official website: http://www.jfree.org/jfreechart/.
One jar configuration
Currently using the official latest version of 1.0.19, Jcommon-1.0.23.jar and Jfreechart-1.0.19.jar are required to use
Two Example
1. Column Chart
Sample code:
Package Com.ricky.java.chart;
Import Java.awt.Font;
Import Javax.swing.JFrame;
Import Org.jfree.chart.ChartFactory;
Import Org.jfree.chart.ChartPanel;
Import Org.jfree.chart.JFreeChart;
Import Org.jfree.chart.axis.CategoryAxis;
Import Org.jfree.chart.axis.ValueAxis;
Import Org.jfree.chart.plot.CategoryPlot;
Import Org.jfree.data.category.CategoryDataset;
Import Org.jfree.data.category.DefaultCategoryDataset; public class Barchartdemo {/** * @param args */public static void main (string[] args) {Jfreechart chart = Cha
Rtfactory.createbarchart ("Statistics", "fruit", "fruit Kind", getdataset ()); Starting from here Categoryplot plot = Chart.getcategoryplot ();//Get chart Area object Categoryaxis Domainaxis = Plot.getdomainaxis (); Horizontal bottom list Domainaxis.setlabelfont (new Font ("bold", Font.Bold, 14); Horizontal bottom title Domainaxis.setticklabelfont (New Font ("Song Body", Font.Bold, 12));
Vertical title Valueaxis Rangeaxis = Plot.getrangeaxis ();//Get Columnar Rangeaxis.setlabelfont (new Font ("bold", Font.Bold, 15)); Chart.getlegend (). sEtitemfont (New Font ("bold", Font.Bold, 15)); Chart.gettitle (). SetFont (New Font ("Arial", Font.Bold, 20))//Set title font Chartpanel Chartpanel = new Chartpanel (chart, True);
Here also can use Chartframe, can directly generate a separate frame JFrame frame=new JFrame ("Java Data Statistics graph"); Frame.add (Chartpanel);
Add Column Chart Frame.setbounds (50, 50, 900, 600);
Frame.setvisible (TRUE);
private static Categorydataset GetDataSet () {defaultcategorydataset DataSet = new Defaultcategorydataset ();
Dataset.addvalue (100, "Beijing", "Apple");
Dataset.addvalue (100, "Shanghai", "Apple");
Dataset.addvalue (100, "Guangzhou", "Apple");
Dataset.addvalue (200, "Beijing", "pear");
Dataset.addvalue (200, "Shanghai", "pear");
Dataset.addvalue (200, "Guangzhou", "pear");
Dataset.addvalue (300, "Beijing", "grapes");
Dataset.addvalue (300, "Shanghai", "grapes");
Dataset.addvalue (300, "Guangzhou", "Grape");
Dataset.addvalue (400, "Beijing", "banana");
Dataset.addvalue (400, "Shanghai", "banana");
Dataset.addvalue (400, "Guangzhou", "banana");
Dataset.addvalue (500, "Beijing", "Litchi"); Dataset.addvalue (500, "Shanghai", "Litchi");
Dataset.addvalue (500, "Guangzhou", "Litchi");
return dataset;
}
}
Run effect
2. Pie chart
Sample code:
Package Com.ricky.java.chart;
Import Java.awt.Font;
Import Java.text.DecimalFormat;
Import Java.text.NumberFormat;
Import Javax.swing.JFrame;
Import Org.jfree.chart.ChartFactory;
Import Org.jfree.chart.ChartPanel;
Import Org.jfree.chart.JFreeChart;
Import Org.jfree.chart.labels.StandardPieSectionLabelGenerator;
Import Org.jfree.chart.plot.PiePlot;
Import Org.jfree.data.general.DefaultPieDataset; public class Piechartdemo {/** * @param args */public static void main (string[] args) {Defaultpiedataset data
= GetDataSet ();
Jfreechart chart = Chartfactory.createpiechart3d ("fruit yield", data, True, false, false);
Set percent Pieplot Pieplot = (pieplot) chart.getplot (); DecimalFormat df = new DecimalFormat ("0%");//Get a DecimalFormat object, mainly set a decimal problem numberformat NF = numberformat.getnumber Instance ()//Get a NumberFormat object Standardpiesectionlabelgenerator SP1 = new Standardpiesectionlabelgenerator ("{0} {2} ", NF, df);//Get Standardpiesectionlabelgenerator object Pieplot.setlAbelgenerator (SP1)//Set pie chart showing percent///No data displayed when the content pieplot.setnodatamessage ("No data display");
Pieplot.setcircular (FALSE);
Pieplot.setlabelgap (0.02D); Pieplot.setignorenullvalues (true);/setting does not display a null value pieplot.setignorezerovalues (TRUE);//setting does not display a negative value chart.gettitle (). SetFont (New Font ("Arial", Font.Bold, 20))//Set title font Pieplot Pieplot = (pieplot) chart.getplot ();//Get chart Area object Pieplot.setlabelfont
(New Font ("Arial", Font.Bold, 10));//Resolve garbled chart.getlegend (). Setitemfont (New Font ("bold", Font.Bold, 10);
Chartpanel Chartpanel = new Chartpanel (chart, True);
JFrame frame = new JFrame ("Java Data Statistics graph"); Frame.add (Chartpanel);
Add Column chart frame.setbounds (0, 0, 900, 600);
Frame.setvisible (TRUE);
private static Defaultpiedataset GetDataSet () {defaultpiedataset DataSet = new Defaultpiedataset ();
Dataset.setvalue ("Apple", 100);
Dataset.setvalue ("Pear", 200);
Dataset.setvalue ("Grapes", 300);
Dataset.setvalue ("Banana", 400);
Dataset.setvalue ("Litchi", 500);
return dataset;
}
}
Run results
3. Line chart
Sample code:
Package Com.ricky.java.chart;
Import Java.awt.Font;
Import Java.text.SimpleDateFormat;
Import Javax.swing.JFrame;
Import Org.jfree.chart.ChartFactory;
Import Org.jfree.chart.ChartPanel;
Import Org.jfree.chart.JFreeChart;
Import Org.jfree.chart.axis.DateAxis;
Import Org.jfree.chart.axis.ValueAxis;
Import Org.jfree.chart.plot.XYPlot;
Import Org.jfree.data.time.Month;
Import org.jfree.data.time.TimeSeries;
Import org.jfree.data.time.TimeSeriesCollection;
Import Org.jfree.data.xy.XYDataset; public class Timeserieschartdemo {/** * @param args */public static void main (string[] args) {Xydataset xyd
Ataset = CreateDataSet ();
Jfreechart Jfreechart = Chartfactory.createtimeserieschart ("Weather change data", "date", "Price", Xydataset, True, true, true);
Xyplot Xyplot = (xyplot) jfreechart.getplot ();
Dateaxis Dateaxis = (dateaxis) xyplot.getdomainaxis ();
Dateaxis.setdateformatoverride (New SimpleDateFormat ("mmm-yyyy")); Dateaxis.setlabelfont(New Font ("bold", font.bold,14)); Horizontal bottom title Dateaxis.setticklabelfont (New Font ("Song Body", font.bold,12)); Vertical caption Valueaxis Rangeaxis=xyplot.getrangeaxis ();//Get Columnar Rangeaxis.setlabelfont (new Font ("bold", Font.Bold
, 15));
Jfreechart.getlegend (). Setitemfont (New Font ("bold", Font.Bold, 15)); Jfreechart.gettitle (). SetFont (New Font ("Arial", font.bold,20))//Set title font Chartpanel Chartpanel = new CHARTPA
Nel (jfreechart,true);
JFrame frame = new JFrame ("Java Data Statistics graph"); Frame.add (Chartpanel);
Add Column Chart Frame.setbounds (50, 50, 900, 600);
Frame.setvisible (TRUE); private static Xydataset CreateDataSet () {//This dataset is a bit more, but it's not difficult to understand timeseries timeseries = new TimeSeries ("2012
Annual weather change data ");
Timeseries.add (New Month (1), 12.6d);
Timeseries.add (New Month (2), 15.6d);
Timeseries.add (New Month (3), 19.6d);
Timeseries.add (New Month (4), 25.6d); Timeseries.add (New Month (5), 35.6d);
Timeseries.add (New Month (6), 36.6d);
Timeseries.add (New Month (7), 41.9d);
Timeseries.add (New Month (8), 42.9d);
Timeseries.add (New Month (9), 34.2d);
Timeseries.add (new Month), 22.2d);
Timeseries.add (new Month), 19.2d);
Timeseries.add (New Month (a), 16.2d);
TimeSeries timeseries1 = new TimeSeries ("Weather change data for the year 2013");
Timeseries1.add (New Month (1, 2013), 11.99d);
Timeseries1.add (New Month (2, 2013), 13.99d);
Timeseries1.add (New Month (3, 2013), 17.99d);
Timeseries1.add (New Month (4, 2013), 19.99d);
Timeseries1.add (New Month (5, 2013), 22.99d);
Timeseries1.add (New Month (6, 2013), 29.99d);
Timeseries1.add (New Month (7, 2013), 37.99d);
Timeseries1.add (New Month (8, 2013), 39.99d);
Timeseries1.add (New Month (9, 2013), 32.99d); Timeseries1.add (New Month (2013), 21.99d);
Timeseries1.add (New Month (one, 2013), 17.99d);
Timeseries1.add (New Month (2013), 12.99d);
Timeseriescollection timeseriescollection = new Timeseriescollection ();
Timeseriescollection.addseries (timeseries);
Timeseriescollection.addseries (TIMESERIES1);
return timeseriescollection;
}
}
Run results
SOURCE Download Address: