使用jfreechart生成柱狀圖、折線圖、和餅狀圖

JFreeChart是JAVA平臺上的一個開放的圖表繪製類庫。它完全使用JAVA語言編寫,是爲applications, applets, servlets 以及JSP等使用所設計。下面我就詳細介紹如何使用jfreechart生成柱狀圖、折線圖、和餅狀圖。

步驟:

①、導入其相應的jcommon-1.0.16.jar和jfreechart-1.0.13.jar文件(可點擊下載

②、下面就可以寫實現各種圖形的代碼了

A、生成柱狀圖:

<span style="font-size:18px;">package com.whp.test;

import java.awt.Font;
import java.io.File;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis3D;
import org.jfree.chart.axis.NumberAxis3D;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.data.category.DefaultCategoryDataset;

public class jfreeChart {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DefaultCategoryDataset dataset=new DefaultCategoryDataset();
		//添加數據
		dataset.addValue(98, "數學", "張三");
		dataset.addValue(87, "語文", "張三");
		dataset.addValue(68, "數學", "李四");
		dataset.addValue(89, "語文", "李四");
		dataset.addValue(56, "數學", "王五");
		dataset.addValue(96, "語文", "王五");
		
		JFreeChart chart=ChartFactory.createBarChart3D(
				"成績統計表",
				"學生姓名",//X軸的標籤 
                "分數",//Y軸的標籤 
                dataset, //圖標顯示的數據集合
                PlotOrientation.VERTICAL, //圖像的顯示形式(水平或者垂直)
                true,//是否顯示子標題 
                true,//是否生成提示的標籤 
                true); //是否生成URL鏈接		
		       //處理圖形上的亂碼
				//處理主標題的亂碼
				chart.getTitle().setFont(new Font("宋體",Font.BOLD,18));
				//處理子標題亂碼
				chart.getLegend().setItemFont(new Font("宋體",Font.BOLD,15));
				//獲取圖表區域對象
				CategoryPlot categoryPlot = (CategoryPlot)chart.getPlot();
				//獲取X軸的對象
				CategoryAxis3D categoryAxis3D = (CategoryAxis3D)categoryPlot.getDomainAxis();
				//獲取Y軸的對象
				NumberAxis3D numberAxis3D = (NumberAxis3D)categoryPlot.getRangeAxis();
				//處理X軸上的亂碼
				categoryAxis3D.setTickLabelFont(new Font("宋體",Font.BOLD,15));
				//處理X軸外的亂碼
				categoryAxis3D.setLabelFont(new Font("宋體",Font.BOLD,15));
				//處理Y軸上的亂碼
				numberAxis3D.setTickLabelFont(new Font("宋體",Font.BOLD,15));
				//處理Y軸外的亂碼
				numberAxis3D.setLabelFont(new Font("宋體",Font.BOLD,15));
				//處理Y軸上顯示的刻度,以10作爲1格
				numberAxis3D.setAutoTickUnitSelection(false);
				NumberTickUnit unit = new NumberTickUnit(10);
				numberAxis3D.setTickUnit(unit);
				//獲取繪圖區域對象
				BarRenderer3D barRenderer3D = (BarRenderer3D)categoryPlot.getRenderer();
				//設置柱形圖的寬度
				barRenderer3D.setMaximumBarWidth(0.07);
				//在圖形上顯示數字
				barRenderer3D.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
				barRenderer3D.setBaseItemLabelsVisible(true);
				barRenderer3D.setBaseItemLabelFont(new Font("宋體",Font.BOLD,15));
				
				//在D盤目錄下生成圖片
				File file = new File("chart.jpeg");
				try {
					ChartUtilities.saveChartAsJPEG(file, chart, 800, 600);
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				//使用ChartFrame對象顯示圖像
				ChartFrame frame = new ChartFrame("xyz",chart);
				frame.setVisible(true);
				frame.pack();
	}

}
</span>
結果:



B、生成折線圖

package com.whp.test;

import java.awt.Font;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryAxis3D;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberAxis3D;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

public class jfreeChart1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		// 添加數據
		dataset.addValue(98, "數學", "張三");
		dataset.addValue(68, "數學", "李四");
		dataset.addValue(56, "數學", "王五");

		JFreeChart chart = ChartFactory.createLineChart("用戶統計報表(所屬單位)", // 主標題的名稱
				"所屬單位名稱",// X軸的標籤
				"數量",// Y軸的標籤
				dataset, // 圖標顯示的數據集合
				PlotOrientation.VERTICAL, // 圖像的顯示形式(水平或者垂直)
				true,// 是否顯示子標題
				true,// 是否生成提示的標籤
				true); // 是否生成URL鏈接
		// 處理圖形上的亂碼
		// 處理主標題的亂碼
		chart.getTitle().setFont(new Font("宋體", Font.BOLD, 18));
		// 處理子標題亂碼
		chart.getLegend().setItemFont(new Font("宋體", Font.BOLD, 15));
		// 獲取圖表區域對象
		CategoryPlot categoryPlot = (CategoryPlot) chart.getPlot();
		// 獲取X軸的對象
		CategoryAxis categoryAxis = (CategoryAxis) categoryPlot.getDomainAxis();
		// 獲取Y軸的對象
		NumberAxis numberAxis = (NumberAxis) categoryPlot.getRangeAxis();
		// 處理X軸上的亂碼
		categoryAxis.setTickLabelFont(new Font("宋體", Font.BOLD, 15));
		// 處理X軸外的亂碼
		categoryAxis.setLabelFont(new Font("宋體", Font.BOLD, 15));
		// 處理Y軸上的亂碼
		numberAxis.setTickLabelFont(new Font("宋體", Font.BOLD, 15));
		// 處理Y軸外的亂碼
		numberAxis.setLabelFont(new Font("宋體", Font.BOLD, 15));
		// 處理Y軸上顯示的刻度,以10作爲1格
		numberAxis.setAutoTickUnitSelection(false);
		NumberTickUnit unit = new NumberTickUnit(10);
		numberAxis.setTickUnit(unit);
		// 獲取繪圖區域對象
		LineAndShapeRenderer lineAndShapeRenderer = (LineAndShapeRenderer) categoryPlot
				.getRenderer();
		// 在圖形上顯示數字
		lineAndShapeRenderer
				.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		lineAndShapeRenderer.setBaseItemLabelsVisible(true);
		lineAndShapeRenderer
				.setBaseItemLabelFont(new Font("宋體", Font.BOLD, 15));
		// 在圖形上添加轉折點(使用小矩形顯示)
		Rectangle shape = new Rectangle(10, 10);
		lineAndShapeRenderer.setSeriesShape(0, shape);
		lineAndShapeRenderer.setSeriesShapesVisible(0, true);

		//在D盤目錄下生成圖片
		File file = new File("chart1.jpg");
		try {
			ChartUtilities.saveChartAsJPEG(file, chart, 800, 600);
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 使用ChartFrame對象顯示圖像
		ChartFrame frame = new ChartFrame("xyz", chart);
		frame.setVisible(true);
		frame.pack();
	}

}

結果:


C、生成餅圖:

package com.whp.test;

import java.awt.Font;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryAxis3D;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberAxis3D;
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.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

public class jfreeChart3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DefaultPieDataset dataset = new DefaultPieDataset();
		//添加數據
		dataset.setValue("張三",40);
		dataset.setValue("李四",32);
		dataset.setValue("王五",28);
		JFreeChart chart = ChartFactory.createPieChart3D("比重統計報表(所屬單位)", //主標題的名稱
				                      dataset, //圖標顯示的數據集合
				                      true,//是否顯示子標題 
				                      true,//是否生成提示的標籤 
				                      true); //是否生成URL鏈接
		//處理圖形上的亂碼
		//處理主標題的亂碼
		chart.getTitle().setFont(new Font("宋體",Font.BOLD,18));
		//處理子標題亂碼
		chart.getLegend().setItemFont(new Font("宋體",Font.BOLD,15));
		//獲取圖表區域對象
		PiePlot3D categoryPlot = (PiePlot3D)chart.getPlot();
		//處理圖像上的亂碼
		categoryPlot.setLabelFont(new Font("宋體",Font.BOLD,15));
		//設置圖形的生成格式爲(上海 2 (10%))
		String format = "{0} {1} ({2})";
		categoryPlot.setLabelGenerator(new StandardPieSectionLabelGenerator(format));
		
		//在D盤目錄下生成圖片
		File file = new File("chart2.jpg");
		try {
			ChartUtilities.saveChartAsJPEG(file, chart, 800, 600);
		} catch (IOException e) {
			e.printStackTrace();
		}
		//使用ChartFrame對象顯示圖像
		ChartFrame frame = new ChartFrame("xyz",chart);
		frame.setVisible(true);
		frame.pack();
	}

}

結果:





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章