Jfreechart is an open chart drawing class library on the Java platform. It is written entirely in the Java language and is designed for use by applications, applets, Servlets, and JSP. Jfreechart can generate a variety of charts (pie charts), histogram (bar charts), scatter (scatter plots), time series (time series), Gantt chart (Gantt charts), and more. It also produces PNG and JPEG output, and can be associated with PDFs and Excel.
All of its methods are static and easy to use.
To generate a pie chart:
Package Cn.zhangao.jfreechart;
Import Java.awt.Font;
Import Java.awt.Image;
Import Java.io.File;
Import Javax.imageio.ImageIO;
Import Org.jfree.chart.ChartFactory;
Import org.jfree.chart.ChartUtilities;
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 App {
public static void Main (string[] args) {
try {
If you do not use font, Chinese will not show up
Font font = new Font ("Song Body", Font.Bold, 15);
Defaultpiedataset PDS = new Defaultpiedataset ();
Pds.setvalue ("Sun", 100);
Pds.setvalue ("IBM", 300);
Pds.setvalue ("Bea", 500);
Pds.setvalue ("Oracle", 700);
/**
* Generate a chart of pie charts
* are: Display the title of the chart, the Dateset object that needs to provide the corresponding chart, whether to display the legend, whether to generate tips, and whether to generate URL links
*/
Jfreechart chart = Chartfactory.createpiechart ("title", PDS, True, false, true);
Set the font for a picture title
Chart.gettitle (). SetFont (font);
Get the tile to set the font for the label
Pieplot plot = (Pieplot) chart.getplot ();
Set splitting effect, you need to specify the key to split out
Plot.setexplodepercent ("Oracle", 0.3);
Set Label font
Plot.setlabelfont (font);
Set Legend Item Font
Chart.getlegend (). Setitemfont (font);
/**
* Set Starting angle (Radian calculation)
* Degree of 0° 30° 45° 60° 90° 120° 135° 150° 180° 270° 360°
* Radian 0 π/6 π/4 π/3 π/2 2π/3 3π/4 5π/6 π 3π/2 2π
*/
Plot.setstartangle (New Float (3.14f/2f));
Set the background picture to set the maximum background
Image img = imageio.read (New File ("d:/sunset.jpg"));
Chart.setbackgroundimage (IMG);
Set the background picture for plot
img = imageio.read (New File ("d:/water.jpg"));
Plot.setbackgroundimage (IMG);
Set the foreground color transparency of plot
Plot.setforegroundalpha (0.7f);
Set the background color transparency of plot
Plot.setbackgroundalpha (0.0f);
Set Tag Builder (default {0})
{0}:key {1}:value {2}: percent {3}:sum
Plot.setlabelgenerator (New Standardpiesectionlabelgenerator ("{0} ({1})/{2}"));
Write an in-memory picture to your local hard drive
Chartutilities.savechartasjpeg (New File ("D:/pie.jpg"), chart, 600, 300);
catch (Exception e) {
E.printstacktrace ();
}
}
}
Effect:
You can also generate a picture of a 3D effect, and you only need to easily modify several calling methods:
Jfreechart chart = Chartfactory.createpiechart3d ("title", PDS, True, false, true);
Pieplot3d plot = (Pieplot3d) chart.getplot ();
There is no need to change anywhere else. But note:3D can not use split effect!
To generate a column chart:
Package Cn.zhangao.jfreechart;
Import Java.awt.Font;
Import Java.io.File;
Import Javax.imageio.ImageIO;
Import Org.jfree.chart.ChartFactory;
Import org.jfree.chart.ChartUtilities;
Import Org.jfree.chart.JFreeChart;
Import Org.jfree.chart.plot.CategoryPlot;
Import org.jfree.chart.plot.PlotOrientation;
Import Org.jfree.data.category.DefaultCategoryDataset;
public class AppBar {
public static void Main (string[] args) {
try {
Kinds of data sets
Defaultcategorydataset ds = new Defaultcategorydataset ();
Ds.setvalue ("IBM", "first quarter");
Ds.setvalue ("IBM", "second quarter");