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; } }