package com.cstp.jfreechart;
import java.awt.Color;
import java.awt.Font;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
public class TestChart {
public static void main(String[] args) {
DefaultPieDataset pds= new DefaultPieDataset();
pds.setValue("软件班", 25);
pds.setValue("金融班", 45);
pds.setValue("信管班", 5);
pds.setValue("法律班", 10);
pds.setValue("会计班", 15);
//第2个true表示鼠标移动图上有提示
JFreeChart chart = ChartFactory.createPieChart("班级人数比例图", pds, true, true, true);
Font font=new Font("宋体", Font.PLAIN, 12);
//整个图chart分成三部分 title,plot,legend三个部分,分别对他们分别设置字体就对了。
chart.getTitle().setFont(font);//标题
chart.getLegend().setItemFont(font);//底部
((PiePlot)chart.getPlot()).setLabelFont(font);//图上的字
chart.setBackgroundPaint(Color.red);//背景颜色
ChartFrame cFrame=new ChartFrame("第一个Jfree图",chart);
cFrame.pack();
cFrame.setVisible(true);
}
}
效果图:
