日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Jfreechart學習筆記1-簡單Pie圖形創建

系統 1889 0
餅形狀圖形的創建的例子
    package com.langhua.Pie;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;

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.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;

public class Pie {
	public static void main(String[] args) {
		//數據
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("中文", 43.2);
		dataset.setValue("Category 2", 27.9);
		dataset.setValue("Category 3", 79.5);
		dataset.setValue("Category 4", null);
		dataset.setValue("Category 5", 0);
		dataset.setValue("Category 6", -50.0f);
		//創建 Jfreechart對象
		JFreeChart jfreechart = ChartFactory.createPieChart("Simple Pie Chart", dataset, true, true, false);
		PiePlot piePlot = (PiePlot) jfreechart.getPlot();
		//指定分類餅的顏色
		piePlot.setSectionPaint("中文",Color.blue);
		piePlot.setSectionPaint("Category 2",Color.red);
		piePlot.setSectionPaint("Category 3",Color.yellow);
		
		//設置Pie的邊框是否可見
		piePlot.setSectionOutlinesVisible(true);
		// 指定圖片的透明度(0.0-1.0)
		piePlot.setForegroundAlpha(1.0f);
		//設置邊框的顏色
		piePlot.setBaseSectionOutlinePaint(Color.green);
		//設置邊框的粗細,new BasicStroke(2.0f)
		piePlot.setBaseSectionOutlineStroke(new BasicStroke(1));
		//設置空值,0值,負值是否顯示出來,如果顯示的話就是false
		piePlot.setIgnoreNullValues(true);
		piePlot.setIgnoreZeroValues(true);
		
		//設置上面的樣式,0表示KEY,1表示VALUE,2表示百分之幾,DecimalFormat用來顯示百分比的格式
		piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}=>{1}({2})",NumberFormat.getNumberInstance(),new DecimalFormat("0.00%")));
		
		//設置下面方框的樣式,0表示KEY,1表示VALUE,2表示百分之幾,DecimalFormat用來顯示百分比的格式
		piePlot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}=>{1}({2})",NumberFormat.getNumberInstance(),	new DecimalFormat("0.00%")));
		
		//爆炸模式,使Pie的一塊分離出去,不支持3D
		piePlot.setExplodePercent("Category 1", 0.10);
		piePlot.setExplodePercent("Category 2", 0.10);
		piePlot.setExplodePercent("Category 3", 0.10);
		
		//定義字體格式
		Font font = new Font("微軟雅黑", Font.CENTER_BASELINE, 12);
		//定義圖片標題
		TextTitle title = new TextTitle("Pie狀圖");
		//設置標題的格式
		title.setFont(font);
		//把標題設置到圖片里面
		jfreechart.setTitle(title);
		
		//設置字體,非常關鍵不然會出現亂碼的,下方的字體
		jfreechart.getLegend().setItemFont(font);
		//Pie圖的字體
		piePlot.setLabelFont(font);
		
		//創建輸出流
		FileOutputStream fos_jpg = null;
		try {			
			fos_jpg = new FileOutputStream("c:\\Pie.jpg");
			//用工具把圖象寫到硬盤,支持兩種格式,JPG,PNG,還支持MAP
			ChartUtilities.writeChartAsJPEG(fos_jpg,0.99f,jfreechart,640,480,null);
			fos_jpg.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
  

PiePlot3D圖象的創建和PiePlot基本一樣,就是創建的時候的區別,還有3D不支持爆炸模式
    JFreeChart jfreechart = ChartFactory.createPieChart3D("Simple Pie Chart", dataset, true, true, false);
		PiePlot3D piePlot = (PiePlot3D) jfreechart.getPlot();
  


解決亂碼的問題

1.標題亂碼,重新定義TextTitle title = new TextTitle("Pie狀圖");標題,再設置字體,再把標題到圖像上面

2.下面底部字體的亂碼jfreechart.getLegend().setItemFont(font);這樣設置就可以解決

3.圖形旁邊的字體亂碼piePlot.setLabelFont(font);
如下
    
	//定義字體格式
		Font font = new Font("微軟雅黑", Font.CENTER_BASELINE, 12);
		//定義圖片標題
		TextTitle title = new TextTitle("Pie狀圖");
		//設置標題的格式
		title.setFont(font);
		//把標題設置到圖片里面
		jfreechart.setTitle(title);
		
		//設置字體,非常關鍵不然會出現亂碼的,下方的字體
		jfreechart.getLegend().setItemFont(font);
		//Pie圖的字體
		piePlot.setLabelFont(font);

  

附件有簡單例子下載




Jfreechart學習筆記1-簡單Pie圖形創建


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 遵化市| 库伦旗| 浮山县| 山阳县| 涟水县| 句容市| 孙吴县| 昔阳县| 松原市| 利辛县| 金坛市| 尼勒克县| 桦南县| 南开区| 岑巩县| 黄冈市| 唐海县| 隆德县| 崇阳县| 芷江| 习水县| 越西县| 喀喇| 吉木萨尔县| 海城市| 河曲县| 潍坊市| 闻喜县| 新密市| 八宿县| 沂水县| 中江县| 江安县| 濮阳市| 陆川县| 肥乡县| 女性| 宜兰市| 云和县| 晋城| 延长县|