JFreeChart --- 折线图

package com.tvu.project.util;
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.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
/**
 * @author  rock
 */

public class LineChartDemo
{
 public static void main(String[] args) throws IOException
 {
  JFreeChart chart = ChartFactory.createLineChart(
       "图书销量统计图", // 图表标题
       "图书", // 目录轴的显示标签
       "销量", // 数值轴的显示标签
       getDataSet(), // 数据集
       //PlotOrientation.HORIZONTAL , // 图表方向:水平
       PlotOrientation.VERTICAL , // 图表方向:垂直
       true,  // 是否显示图例(对于简单的柱状图必须是false)
       false,  // 是否生成工具
       false  // 是否生成URL链接
       );
       
  //重新设置图标标题,改变字体
  chart.setTitle(new TextTitle("图书销量统计图", new Font("黑体", Font.ITALIC , 22)));
  //取得统计图标的第一个图例
  LegendTitle legend = chart.getLegend(0);
  //修改图例的字体
  legend.setItemFont(new Font("宋体", Font.BOLD, 14));
  CategoryPlot plot = (CategoryPlot)chart.getPlot();
  //取得横轴
  CategoryAxis categoryAxis = plot.getDomainAxis();
  //设置横轴显示标签的字体
  categoryAxis.setLabelFont(new Font("宋体" , Font.BOLD , 22));
  //分类标签以45度角倾斜
  categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
  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("book.jpg");
  //将统计图标输出成JPG文件
  ChartUtilities.writeChartAsJPEG(
   fos, //输出到哪个输出流
   1, //JPEG图片的质量,0~1之间
   chart, //统计图标对象
   800, //宽
   600,//宽
   null //ChartRenderingInfo 信息
   );
  fos.close();
 }
 //返回一个CategoryDataset实例
 private static CategoryDataset getDataSet()
 {
  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  dataset.addValue(45000 , "北京" , "Spring2.0宝典");
  dataset.addValue(38000 , "北京" , "轻量级J2EE企业实战");
  dataset.addValue(24000 , "北京" , "基于J2EE的Ajax宝典");
  dataset.addValue(32000 , "北京" , "JavaScript权威指南");
  dataset.addValue(21000 , "北京" , "Ajax In Action");
  dataset.addValue(37000 , "上海" , "Spring2.0宝典");
  dataset.addValue(36000 , "上海" , "轻量级J2EE企业实战");
  dataset.addValue(34000 , "上海" , "基于J2EE的Ajax宝典");
  dataset.addValue(42000 , "上海" , "JavaScript权威指南");
  dataset.addValue(12000 , "上海" , "Ajax In Action");
  dataset.addValue(42000 , "广州" , "Spring2.0宝典");
  dataset.addValue(40000 , "广州" , "轻量级J2EE企业实战");
  dataset.addValue(34000 , "广州" , "基于J2EE的Ajax宝典");
  dataset.addValue(18000 , "广州" , "JavaScript权威指南");
  dataset.addValue(26000 , "广州" , "Ajax In Action");
  return dataset;
 }
}

book

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值