JFreeChar详细

来源:互联网 发布:实体店营销软件 编辑:程序博客网 时间:2024/05/04 19:34

一、利用JFreeChart工厂类创建不同类型的图表对象
 JFreeChart chart=ChartFactory.createBarChart3D(title, categoryAxisLabel, valueAxisLabel, dataset, orientation, legend, tooltips, urls);
二、设置chart字体
1、获取CategoryPlot
 (1)有横纵坐标的
 CategoryPlot plot=(CategoryPlot) chart.getPlot();
 (2)饼状图
 PiePlot3D plot = (PiePlot3D)chart.getPlot();
2、定义字体格式
 Font font=new Font("SansSerif",Font.BOLD,12);
3、为chart的各个标签设置字体
 (1)纵坐标轴 (RangeAxis)
  plot.getRangeAxis().setLabelFont(font);
 (2)横坐标轴 (DomainAxis)
  plot.getDomainAxis().setLabelFont(font);
 (3)横坐标轴的各个小分段
  plot.getDomainAxis().setTickLabelFont(font);
 (4)标题 (Title)
  chart.getTitle().setFont(font);
 (5)图例 (Legend)
  chart.getLegend().setItemFont(font);
三、定义chart输出
1、利用FileOutputStream,不带缓存,直接输出
2、org.jfree.chart.ChartUtilities 定义chart图表的输出方式
 try{
  FileOutputStream fos_jpg=new FileOutputStream("d://fos.jpg");
  ChartUtilities.writeChartAsJPEG(fos_jpg,chart,1024,600); //(1024,600)为图片的大小
  fos_jpg.close();
 }catch(Exception ex){
  ex.printStackTrace();
 }
四、为图标添加数据
利用org.jfree.data.category.DefaultCategoryDataset添加数据
 DefaultCategoryDataset dataset=new DefaultCategoryDataset();
 dataset.addValue(90, "计算机系", "07信管");
 dataset.addValue(80, "计算机系", "07软件");
 dataset.addValue(70, "计算机系", "07网络");
 dataset.addValue(90, "会计系", "07财会");
 dataset.addValue(80, "会计系", "07企业会计");
 dataset.addValue(70, "会计系", "07商品");
 dataset.addValue(90, "金融系", "07国际金融");
 dataset.addValue(80, "金融系", "07商务金融");
 dataset.addValue(70, "金融系", "07金融");


五、3D柱状图
package com.cstp.lifuqiang.freechart;

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

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class TestFreeChart {
 public static void main(String[] args) {
  getChart();
 }
 public static void getChart(){
//  ___Dataset  :  数据集对象,用于提供图表中显示的数据,不同的图表类型,数据集 对象也不同
//  ___Plot     :  图表区域对象
//  ___Axis     :  用于处理图表的横纵坐标轴
//  ___Renderer :  用于负责如何显示图表对象
//  ___URLGenerator:用于生成图像的Hot区域[Map]
//  ___ToolTipGenerator:用于生成图像的帮助提示
  String title="班级成绩统计表";
  String categoryAxisLabel="班级";
  String valueAxisLabel="分数";
  CategoryDataset dataset=getData();
  PlotOrientation orientation=PlotOrientation.VERTICAL;
  boolean legend=true;
  boolean tooltips=true;
  boolean urls=false;
  JFreeChart chart=ChartFactory.createBarChart3D(title, categoryAxisLabel, valueAxisLabel, dataset, orientation, legend, tooltips, urls);
  //为各标签设置字体
  CategoryPlot plot=(CategoryPlot) chart.getPlot();
  Font f=new Font("SansSerif", Font.BOLD, 12);
  plot.getRangeAxis().setLabelFont(f);
  plot.getDomainAxis().setLabelFont(f);
  plot.getDomainAxis().setTickLabelFont(f);
  chart.getTitle().setFont(f);
  chart.getLegend().setItemFont(f);
  //输出图像
  try {
   FileOutputStream fos_jpg=new FileOutputStream("d://fos.jpg");
   ChartUtilities.writeChartAsJPEG(fos_jpg, chart, 1024, 600);
   fos_jpg.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 private static CategoryDataset getData(){
  DefaultCategoryDataset dataset=new DefaultCategoryDataset();
  dataset.addValue(90, "计算机系", "07信管");
  dataset.addValue(80, "计算机系", "07软件");
  dataset.addValue(70, "计算机系", "07网络");
  dataset.addValue(90, "会计系", "07财会");
  dataset.addValue(80, "会计系", "07企业会计");
  dataset.addValue(70, "会计系", "07商品");
  dataset.addValue(90, "金融系", "07国际金融");
  dataset.addValue(80, "金融系", "07商务金融");
  dataset.addValue(70, "金融系", "07金融");
  return dataset;
 }

原创粉丝点击