用JFreeChart 生成報表
阿新 • • 發佈:2018-12-08
JFreeChart是JAVA平臺上的一個開放的圖表繪製類庫。它完全使用JAVA語言編寫,是為applications, applets, servlets 以及JSP等使用所設計。JFreeChart可生成餅圖(pie charts)、柱狀圖(bar charts)、散點圖(scatter plots)、時序圖(time series)、甘特圖(Gantt charts)等等多種圖表,並且可以產生PNG和JPEG格式的輸出,還可以與PDF和EXCEL關聯。
它所有的方法都是靜態的,用起來很簡便.
生成餅圖:
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 {
//如果不使用Font,中文將顯示不出來
Font font = new Font("宋體", Font.BOLD, 15);
DefaultPieDataset pds = new DefaultPieDataset();
pds.setValue("sun", 100);
pds.setValue("ibm", 300);
pds.setValue("bea", 500);
pds.setValue("oracle", 700);
/
* 生成一個餅圖的圖表
*
* 分別是:顯示圖表的標題、需要提供對應圖表的DateSet物件、是否顯示圖例、是否生成貼士以及是否生成URL連結
/
JFreeChart chart = ChartFactory.createPieChart("標題", pds, true, false, true);
//設定圖片標題的字型
chart.getTitle().setFont(font);
//得到圖塊,準備設定標籤的字型
PiePlot plot = (PiePlot) chart.getPlot();
//設定分裂效果,需要指定分裂出去的key
plot.setExplodePercent("oracle", 0.3);
//設定標籤字型
plot.setLabelFont(font);
//設定圖例專案字型
chart.getLegend().setItemFont(font);
/
設定開始角度(弧度計算)
*
* 度 0° 30° 45° 60° 90° 120° 135° 150° 180° 270° 360°
* 弧度 0 π/6 π/4 π/3 π/2 2π/3 3π/4 5π/6 π 3π/2 2π
*/
plot.setStartAngle(new Float(3.14f / 2f));
//設定背景圖片,設定最大的背景
Image img = ImageIO.read(new File("d:/sunset.jpg"));
chart.setBackgroundImage(img);
//設定plot的背景圖片
img = ImageIO.read(new File("d:/Water.jpg"));
plot.setBackgroundImage(img);
//設定plot的前景色透明度
plot.setForegroundAlpha(0.7f);
//設定plot的背景色透明度
plot.setBackgroundAlpha(0.0f);
//設定標籤生成器(預設{0})
//{0}:key {1}:value {2}:百分比 {3}:sum
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({1})/{2}"));
//將記憶體中的圖片寫到本地硬碟
ChartUtilities.saveChartAsJPEG(new File("d:/pie.jpg"), chart, 600, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
}
效果:
還可以生成3D效果的圖片,只需要輕鬆地修改幾個呼叫的方法即可:
JFreeChart chart = ChartFactory.createPieChart3D("標題", pds, true, false, true);
PiePlot3D plot = (PiePlot3D) chart.getPlot();
其他地方都不用改.但是需要注意的是:3D不能使用分裂效果!
生成柱狀圖:
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 {
//種類資料集
DefaultCategoryDataset ds = new DefaultCategoryDataset();
ds.setValue(100, "ibm", "第一季度");
ds.setValue(200, "ibm", "第二季度");
ds.setValue(600, "ibm", "第三季度");
ds.setValue(500, "google", "第一季度");
ds.setValue(333, "google", "第二季度");
ds.setValue(780, "google", "第三季度");
ds.setValue(600, "用友", "第一季度");
ds.setValue(1500, "用友", "第二季度");
ds.setValue(300, "用友", "第三季度");
Font font = new Font("宋體", Font.BOLD, 20);
//建立柱狀圖,柱狀圖分水平顯示和垂直顯示兩種
JFreeChart chart = ChartFactory.createBarChart("前三季度各大公司JEEAS市場銷售情況", "季度", "銷量(萬臺)", ds, PlotOrientation.VERTICAL, true, true, true);
//設定整個圖片的標題字型
chart.getTitle().setFont(font);
//設定提示條字型
font = new Font("宋體", Font.BOLD, 15);
chart.getLegend().setItemFont(font);
//得到繪圖區
CategoryPlot plot = (CategoryPlot) chart.getPlot();
//得到繪圖區的域軸(橫軸),設定標籤的字型
plot.getDomainAxis().setLabelFont(font);
//設定橫軸標籤項字型
plot.getDomainAxis().setTickLabelFont(font);
//設定範圍軸(縱軸)字型
plot.getRangeAxis().setLabelFont(font);
//儲存成圖片
//設定chart的背景圖片
chart.setBackgroundImage(ImageIO.read(new File("d:/sunset.bmp")));
plot.setBackgroundImage(ImageIO.read(new File("d:/Water.jpg")));
plot.setForegroundAlpha(1.0f);
ChartUtilities.saveChartAsJPEG(new File("d:/bar.jpg"), chart, 600, 400);
} catch (Exception e) {
e.printStackTrace();
}
}
}
平面2D效果:
柱狀圖預設的ForegroundAlpha屬性值是0.5,半透明的.建立3D柱狀圖只需要使用createBarChart3D()這個方法即可:
生成拆線圖:
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 AppLine {
public static void main(String[] args) {
try {
//種類資料集
DefaultCategoryDataset ds = new DefaultCategoryDataset();
ds.setValue(100, "ibm", "第一季度");
ds.setValue(200, "ibm", "第二季度");
ds.setValue(600, "ibm", "第三季度");
ds.setValue(500, "google", "第一季度");
ds.setValue(333, "google", "第二季度");
ds.setValue(780, "google", "第三季度");
ds.setValue(600, "用友", "第一季度");
ds.setValue(1500, "用友", "第二季度");
ds.setValue(300, "用友", "第三季度");
Font font = new Font("宋體", Font.BOLD, 20);
//建立柱狀圖
JFreeChart chart = ChartFactory.createLineChart("前三季度各大公司JEEAS市場銷售情況", "季度", "銷量(萬臺)", ds, PlotOrientation.VERTICAL, true, true, true);
//設定整個圖片的標題字型
chart.getTitle().setFont(font);
//設定提示條字型
font = new Font("宋體", Font.BOLD, 15);
chart.getLegend().setItemFont(font);
//得到繪圖區
CategoryPlot plot = (CategoryPlot) chart.getPlot();
//得到繪圖區的域軸(橫軸),設定標籤的字型
plot.getDomainAxis().setLabelFont(font);
//設定橫軸標籤項字型
plot.getDomainAxis().setTickLabelFont(font);
//設定範圍軸(縱軸)字型
plot.getRangeAxis().setLabelFont(font);
//儲存成圖片
//設定chart的背景圖片
chart.setBackgroundImage(ImageIO.read(new File("d:/sunset.bmp")));
plot.setBackgroundImage(ImageIO.read(new File("d:/Water.jpg")));
plot.setForegroundAlpha(1.0f);
ChartUtilities.saveChartAsJPEG(new File("d:/line.jpg"), chart, 600, 400);
} catch (Exception e) {
e.printStackTrace();
}
}
}
平面2D效果:
同樣只需要使用createLineChart3D()方法建立3D拆線圖,效果: