JFreeChart饼状图、3D柱状图、折线图

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.general.DefaultPieDataset;

public class TestJFree {
	private static Font font = new Font("宋体", Font.BOLD, 18);

	public static void main(String[] args) {
		// 饼状图
		// JFreeChart chart = createPieChart();
		// 简单3D柱状图
		// JFreeChart chart = createBarChart();
		// 3D柱状图
		// JFreeChart chart = createBarChart3D();
		// 折线图
		JFreeChart chart = createLineChart();

		ChartFrame frame = new ChartFrame("公司组织架构图 ", chart, true);
		frame.pack();
		frame.setVisible(true);
	}

	// 创建饼状图
	public static JFreeChart createPieChart() {
		// 创建饼图数据对象
		DefaultPieDataset dfp = new DefaultPieDataset();
		dfp.setValue("管理人员", 25);
		dfp.setValue("市场人员", 35);
		dfp.setValue("开发人员", 20);
		dfp.setValue("后勤人员", 5);
		dfp.setValue("财务人员", 15);

		// Create JFreeChart object
		JFreeChart chart = ChartFactory.createPieChart("公司组织架构图", dfp, true, true, true);
		chart.getTitle().setFont(font);// 解决底部乱码问题
		chart.getLegend().setItemFont(font);
		// 获取饼图对象并解决乱码问题
		PiePlot piePlot = (PiePlot) chart.getPlot();
		piePlot.setLabelFont(new Font("黑体", Font.BOLD, 10));

		return chart;
	}

	// 创建简单柱状图
	public static JFreeChart createBarChart() {
		// 创建3D柱状图数据对象
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(100, "", "苹果");
		dataset.addValue(200, "", "梨子");
		dataset.addValue(300, "", "葡萄");
		dataset.addValue(400, "", "香蕉");
		dataset.addValue(500, "", "荔枝");
		dataset.addValue(355, "", "菠萝");
		dataset.addValue(245, "", "西瓜");

		// Create JFreeChart object
		JFreeChart chart = ChartFactory.createBarChart3D("水果销量图", // 图表标题
				"水果", // 目录轴的显示标签
				"销量", // 数值轴的显示标签
				dataset, // 数据集
				PlotOrientation.VERTICAL, // HORIZONTAL,// 图表方向:水平、垂直
				false, // 是否显示图例(对于简单的柱状图是false,因为只有一种,没有必要显示)
				false, // 是否生成工具
				false // 是否生成URL链接
				);

		Font titleFont = new Font("楷体", Font.BOLD, 18);
		Font plotFont = new Font("宋体", Font.PLAIN, 16);
		TextTitle textTitle2 = chart.getTitle();
		textTitle2.setFont(titleFont);// 为标题设置上字体

		CategoryPlot categoryPlot = chart.getCategoryPlot();
		categoryPlot.getRangeAxis().setLabelFont(plotFont);// 设置Y轴标识字体
		categoryPlot.getDomainAxis().setLabelFont(plotFont);// 设置X轴标识字体
		categoryPlot.getDomainAxis().setTickLabelFont(plotFont);// 设置轴标记的坐标的标记字体

		return chart;
	}

	// 创建3D柱状图
	public static JFreeChart createBarChart3D() {
		// 创建3D柱状图数据对象
		CategoryDataset dataset = getDataSet4();// 取得数据集合
		// Create JFreeChart object
		JFreeChart chart = ChartFactory.createBarChart3D("水果产量图", // 图表标题
				"水果", // 目录轴的显示标签
				"产量", // 数值轴的显示标签
				dataset, // 数据集
				PlotOrientation.VERTICAL, // HORIZONTAL,// 图表方向:水平、垂直
				true, // 是否显示图例(对于简单的柱状图是false,因为只有一种,没有必要显示)
				false, // 是否生成工具
				false // 是否生成URL链接
				);

		Font titleFont = new Font("黑体", Font.BOLD, 20);
		Font plotFont = new Font("宋体", Font.PLAIN, 16);
		Font LegendFont = new Font("楷体", Font.PLAIN, 18);
		TextTitle textTitle2 = chart.getTitle();
		textTitle2.setFont(titleFont);// 为标题设置上字体

		LegendTitle legend2 = chart.getLegend(0);
		legend2.setItemFont(LegendFont);// 为图例说明设置字体

		CategoryPlot categoryPlot = chart.getCategoryPlot();
		categoryPlot.getRangeAxis().setLabelFont(plotFont);// 设置Y轴标识字体
		categoryPlot.getDomainAxis().setLabelFont(plotFont);// 设置X轴标识字体
		categoryPlot.getDomainAxis().setTickLabelFont(plotFont);// 设置轴标记的坐标的标记字体

		return chart;
	}

	// 创建折线图
	public static JFreeChart createLineChart() {
		// 创建3D柱状图数据对象
		CategoryDataset dataset = getDataSet4();// 取得数据集合
		// CategoryDataset dataset = getDataSet3();// 取得数据集合
		// Create JFreeChart object
		JFreeChart chart = ChartFactory.createLineChart("水果产量图", // 图表标题
				"水果", // 目录轴的显示标签
				"产量", // 数值轴的显示标签
				dataset, // 数据集
				PlotOrientation.VERTICAL, // HORIZONTAL,// 图表方向:水平、垂直
				true, // 是否显示图例(对于简单的柱状图是false,因为只有一种,没有必要显示)
				false, // 是否生成工具
				false // 是否生成URL链接
				);

		Font titleFont = new Font("黑体", Font.BOLD, 20);
		Font plotFont = new Font("宋体", Font.PLAIN, 16);
		Font LegendFont = new Font("楷体", Font.PLAIN, 18);
		TextTitle textTitle2 = chart.getTitle();
		textTitle2.setFont(titleFont);// 为标题设置上字体

		LegendTitle legend2 = chart.getLegend(0);
		legend2.setItemFont(LegendFont);// 为图例说明设置字体

		CategoryPlot categoryPlot = chart.getCategoryPlot();
		categoryPlot.setBackgroundPaint(Color.decode("#e6e6e6"));
		categoryPlot.getRangeAxis().setLabelFont(plotFont);// 设置Y轴标识字体
		categoryPlot.getDomainAxis().setLabelFont(plotFont);// 设置X轴标识字体
		categoryPlot.getDomainAxis().setTickLabelFont(plotFont);// 设置轴标记的坐标的标记字体

		CategoryAxis categoryAxis = (CategoryAxis) categoryPlot.getDomainAxis();
		categoryAxis.setUpperMargin(0.06);
		categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);// 倾斜45度

		// 获取绘图区域对象 xxxRender
		NumberAxis numberAxis = (NumberAxis) categoryPlot.getRangeAxis();
		numberAxis.setUpperMargin(0.06);

		LineAndShapeRenderer lineAndShapeRenderer = (LineAndShapeRenderer) categoryPlot.getRenderer();
		// 处理上的数字
		lineAndShapeRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		// 设置颜色
		// lineAndShapeRenderer.setSeriesPaint(0, Color.decode("#ffbe0d"));
		// lineAndShapeRenderer.setSeriesPaint(1, Color.decode("#4ad100"));
		// 设置的数字可见
		lineAndShapeRenderer.setBaseItemLabelsVisible(true);
		// 设置数字的字体
		lineAndShapeRenderer.setBaseItemLabelFont(new Font("楷体", Font.PLAIN, 11));

		// 处理拐角处的点(矩形)
		Rectangle rectangle = new Rectangle(4, 4);
		lineAndShapeRenderer.setSeriesShape(0, rectangle);
		lineAndShapeRenderer.setSeriesShapesFilled(0, true);
		lineAndShapeRenderer.setSeriesShapesVisible(0, true);
		lineAndShapeRenderer.setSeriesShape(1, rectangle);
		lineAndShapeRenderer.setSeriesShapesFilled(1, true);
		lineAndShapeRenderer.setSeriesShapesVisible(1, true);
		lineAndShapeRenderer.setSeriesShape(2, rectangle);
		lineAndShapeRenderer.setSeriesShapesFilled(2, true);
		lineAndShapeRenderer.setSeriesShapesVisible(2, true);
		lineAndShapeRenderer.setSeriesShape(3, rectangle);
		lineAndShapeRenderer.setSeriesShapesFilled(3, true);
		lineAndShapeRenderer.setSeriesShapesVisible(3, true);
		return chart;
	}

	// 数据源方式一
	private static CategoryDataset getDataSet3() {
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(100, "北京", "苹果");
		dataset.addValue(100, "上海", "苹果");
		dataset.addValue(100, "广州", "苹果");
		dataset.addValue(200, "北京", "梨子");
		dataset.addValue(200, "上海", "梨子");
		dataset.addValue(200, "广州", "梨子");
		dataset.addValue(300, "北京", "葡萄");
		dataset.addValue(300, "上海", "葡萄");
		dataset.addValue(300, "广州", "葡萄");
		dataset.addValue(400, "北京", "香蕉");
		dataset.addValue(400, "上海", "香蕉");
		dataset.addValue(400, "广州", "香蕉");
		dataset.addValue(500, "北京", "荔枝");
		dataset.addValue(500, "上海", "荔枝");
		dataset.addValue(500, "广州", "荔枝");
		return dataset;
	}

	// 数据源方式二
	private static CategoryDataset getDataSet4() {
		double[][] data = new double[][] { { 1230, 1110, 1120, 1210, 222 }, { 720, 780, 860, 800, 333 },
				{ 830, 780, 790, 700, 444 }, { 400, 380, 390, 450, 555 } };
		String[] rowKeys = { "苹果", "香蕉", "橘子", "梨子" };
		String[] columnKeys = { "长沙市", "武汉", "南京", "天津", "北京" };
		CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
		return dataset;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值