package com.cstp.jfreechart;
import java.awt.Font;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
public class JFreeChartTest2 extends ApplicationFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
public JFreeChartTest2(String title)//构造方法
{
super(title);
this.setContentPane(createPanel());//设置为当前的面板
}
public static CategoryDataset createDataset()//建立数据集
{
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(10, "管理", "管理人员");
dataset.setValue(20, "市场", "市场人员");
dataset.setValue(40, "开发", "开发人员");
dataset.setValue(15, "其他", "其他人员");
return dataset;
}
public static JFreeChart createChart(CategoryDataset dataset)//以数据集对象为参数产生chart
{
JFreeChart chart = ChartFactory.createBarChart3D("hello", "人员分布", "人员数量",
dataset, PlotOrientation.VERTICAL, true, true, false);
Font font=new Font("宋体", Font.PLAIN, 12);
//设置标题字体
chart.setTitle(new TextTitle("某公司组织结构图", new Font("宋体", Font.BOLD
+ Font.ITALIC, 20)));
//设置底部字体("管理","市场","开发","其他")
chart.getLegend().setItemFont(font);
//-------------图上的乱码问题-------------
CategoryPlot plot = (CategoryPlot) chart.getPlot();
//设置横坐标字体
CategoryAxis categoryAxis = plot.getDomainAxis();
categoryAxis.setLabelFont(font);//x轴的说明字("人员分布")
categoryAxis.setTickLabelFont(font);//x轴上的字("管理人员","市场人员"...)
//设置纵坐标字体
NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
numberaxis.setLabelFont(font);//y轴的说明字("人员数量")
numberaxis.setTickLabelFont(font);//y轴上的字(本例为数字,所以不是必需的)
return chart;
}
public static JPanel createPanel()//返回带有chart的ChartPanel对象
{
JFreeChart chart = createChart(createDataset());
return new ChartPanel(chart);
}
public static void main(String[] args)
{
JFreeChartTest2 chart = new JFreeChartTest2("JfreeChart第二个图");
chart.pack();
chart.setSize(800, 600);
chart.setVisible(true);
}
}
效果图: