使用JFreeChart生成时间顺序图实例(生成图片)

本文展示了一个使用JFreeChart库创建的时间序列图表示例,该图表用于展示不同书籍的月度销量变化趋势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.lhhc.jfreechart.test;

import java.awt.Font;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;

public class TimeChartDemo {

/**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
JFreeChart chart = ChartFactory.createTimeSeriesChart("图书销量统计图", "图书",
"销量", getDataSet(), true, false, false);
LegendTitle legend = chart.getLegend(0);
// 修改图例的字体
legend.setItemFont(new Font("宋体", Font.BOLD, 14));

// 设置标题
chart
.setTitle(new TextTitle("图书销量统计图", new Font("黑体", Font.ITALIC,
22)));
XYPlot plot = (XYPlot) chart.getPlot();
ValueAxis categoryAxis = plot.getDomainAxis();
categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 22));
categoryAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 18));
NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();// 取得纵轴
numberAxis.setLabelFont(new Font("宋体", Font.BOLD, 22));// 设置纵轴显示标签字体
FileOutputStream fos = null;
fos = new FileOutputStream("book2.jpg");
ChartUtilities.writeChartAsJPEG(fos, 1, chart, 800, 600, null

);
fos.close();

}

private static XYDataset getDataSet() {
TimeSeries spring = new TimeSeries("Spring2.0宝典", Month.class);
spring.add(new Month(10, 2006), 3400);
spring.add(new Month(11, 2006), 2700);
spring.add(new Month(12, 2006), 0);
spring.add(new Month(1, 2007), 1800);
spring.add(new Month(2, 2007), 2200);

TimeSeries lightWeight = new TimeSeries("轻量级J2EE企业级应用实战", Month.class);
lightWeight.add(new Month(10, 2006), 2800);
lightWeight.add(new Month(11, 2006), 3700);
lightWeight.add(new Month(12, 2006), 0);
lightWeight.add(new Month(1, 2007), 2100);
lightWeight.add(new Month(2, 2007), 1100);

TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(spring);
dataset.addSeries(lightWeight);
return dataset;

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值