jfreechart画饼形图swing形式的

package com.wkx.jfreechart;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Random;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.urls.StandardPieURLGenerator;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class MyPieChartTest extends ApplicationFrame {
	
	//private String[] engStr={"One","Two","Three","Four","Five","Six","Seven","eight","Nine","Ten"};
	private String[] cheStr={"第一","第二","第三","第四","第五","第六","第七","第八","第九","第十"};
	
	public MyPieChartTest(String title) {
		super(title);
		JFreeChart pieChart = createChart(createDataSet());
		JPanel localPanel = new ChartPanel(pieChart);
		localPanel.setPreferredSize(new Dimension(500,270));
		setContentPane(localPanel);
	}

	public static void main(String[] args) {
		MyPieChartTest pieChartTest = new MyPieChartTest("饼形图测试");
		pieChartTest.pack();
	    RefineryUtilities.centerFrameOnScreen(pieChartTest);
	    pieChartTest.setVisible(true);
	}
	
	public PieDataset createDataSet(){
		DefaultPieDataset dataset = new DefaultPieDataset();
		Random r = new Random();
		for(int i=0;i<cheStr.length;i++){
			dataset.setValue(cheStr[i], r.nextDouble()*10);
		}
		return dataset;
	}
	
	public JFreeChart createChart(PieDataset dataset){
		
		//创建绘图区
		JFreeChart pieChart = ChartFactory.createPieChart("测试饼形图", dataset, true, true, true);
		//设置绘图区背景色
		pieChart.setBackgroundPaint(Color.PINK);
		//获取图片标题
		TextTitle title = pieChart.getTitle();		
		//设置标题样式
		title.setFont(new Font("黑体", Font.PLAIN, 20));		
		//设置鼠标悬浮字
		title.setToolTipText("这是我的测试饼形图。");		
		//获取绘图区
		PiePlot plot = (PiePlot)pieChart.getPlot();
		//设置没有数据时显示的消息
		plot.setNoDataMessage("没有数据。");
		//设置没有数据时显示消息的字体
		plot.setNoDataMessageFont(new Font("黑体",Font.PLAIN,20));
		//设置图片超链接
		plot.setURLGenerator(new StandardPieURLGenerator("http://wenkaixuan.iteye.com"));		
		//设置内容字体
		plot.setLabelFont(new Font("宋体",Font.PLAIN,14));		
		//获取绘图区底部标签
		LegendTitle legTitle = pieChart.getLegend();		
		//设置底部标签字体
		legTitle.setItemFont(new Font("黑体",Font.PLAIN,14));
		
		return pieChart;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值