Java绘制图表——JFreeChart

Java绘制图表的工具——JFreeChart

说明:

JFreeChart是java的一个绘图工具,需要的jar包有jcommon.jar和jfreechart.jar。

以下的例子是柱状图、时间序列折线图和饼状图的使用方法。

案例:

package com.test;

import java.awt.Font;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeriesDataItem;

public class Test {
	public static void main(String[] args) {
		setLanguageCN();
		testBar();
		testLine();
		testPie();
	}

	/**
	 * 设置语言,防止中文乱码
	 */
	public static void setLanguageCN() {
		// 创建主题样式
		StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
		// 设置标题字体
		standardChartTheme.setExtraLargeFont(new Font("宋书", Font.BOLD, 20));
		// 设置图例的字体
		standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 15));
		// 设置轴向的字体
		standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 15));
		// 应用主题样式
		ChartFactory.setChartTheme(standardChartTheme);
	}

	/**
	 * 测试柱状图
	 */
	public static void testBar() {
		DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
		dataSet.setValue(123, "a", "苹果");
		dataSet.setValue(321, "b", "葡萄");
		dataSet.setValue(456, "c", "香蕉");
		dataSet.setValue(654, "d", "西瓜");
		JFreeChart chart = ChartFactory.createBarChart("水果销量统计表", "水果种类", "销量", dataSet, PlotOrientation.HORIZONTAL,
				true, true, true);
		FileOutputStream out = null;
		try {
			out = new FileOutputStream("C:\\coding\\JavaWeb\\TestJFreeChart\\WebContent\\imgs\\testBar.jpg");
			ChartUtilities.writeChartAsJPEG(out, 0.5f, chart, 1200, 900, null);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

	/**
	 * 测试时间序列的折线图
	 */
	public static void testLine() {
		TimeSeries ts = new TimeSeries("测试例子");
		ts.add(new TimeSeriesDataItem(new Day(string2Date("2020-05-01")), new Double(Math.random() * 100)));
		ts.add(new TimeSeriesDataItem(new Day(string2Date("2020-05-02")), new Double(Math.random() * 100)));
		ts.add(new TimeSeriesDataItem(new Day(string2Date("2020-05-03")), new Double(Math.random() * 100)));
		ts.add(new TimeSeriesDataItem(new Day(string2Date("2020-05-04")), new Double(Math.random() * 100)));
		ts.add(new TimeSeriesDataItem(new Day(string2Date("2020-05-05")), new Double(Math.random() * 100)));
		ts.add(new TimeSeriesDataItem(new Day(string2Date("2020-05-06")), new Double(Math.random() * 100)));
		ts.add(new TimeSeriesDataItem(new Day(string2Date("2020-05-07")), new Double(Math.random() * 100)));
		ts.add(new TimeSeriesDataItem(new Day(string2Date("2020-05-08")), new Double(Math.random() * 100)));

		TimeSeriesCollection dataSet = new TimeSeriesCollection();
		dataSet.addSeries(ts);
		JFreeChart chart = ChartFactory.createTimeSeriesChart("某软件用户数量变化", "日期", "用户数量", dataSet, true, true, false);
		FileOutputStream out = null;
		try {
			out = new FileOutputStream("C:\\coding\\JavaWeb\\TestJFreeChart\\WebContent\\imgs\\testLine.jpg");
			ChartUtilities.writeChartAsJPEG(out, 0.5f, chart, 1200, 900, null);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

	/**
	 * 测试饼状图
	 */
	public static void testPie() {
		DefaultPieDataset dataSet = new DefaultPieDataset();
		dataSet.setValue("西瓜", 100);
		dataSet.setValue("苹果", 80);
		dataSet.setValue("梨子", 120);
		dataSet.setValue("香蕉", 300);
		JFreeChart chart = ChartFactory.createPieChart("水果销量统计表", dataSet, true, true, true);
		FileOutputStream out = null;
		try {
			out = new FileOutputStream("C:\\coding\\JavaWeb\\TestJFreeChart\\WebContent\\imgs\\testPie.jpg");
			ChartUtilities.writeChartAsJPEG(out, 0.5f, chart, 1200, 900, null);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}

	}

	/**
	 * 将日期字符串转为Date对象
	 * 
	 * @param date 日期字符串
	 * @return Date对象
	 */
	private static Date string2Date(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date day = null;
		try {
			day = sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return day;
	}

}

结果如下图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值