struts2.1.8.1+JFreeChart -1.0.13 的整合之------曲线图和时序图

来源:互联网 发布:万博软件 编辑:程序博客网 时间:2024/05/13 15:56

1。曲线图:(雷同柱状图)

 

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;

public class LineChart3DDemo {

 public static void main(String[] args) throws IOException
 {
  JFreeChart chart = ChartFactory.createLineChart3D(
    "图书销售统计图",  //标题
    "图书",          //横轴
    "销量",          //纵轴
    getDataSet(),
    PlotOrientation.VERTICAL,
    true,
    false,
    false);
  //重新设置图表标题,改变字体
  chart.setTitle(new TextTitle("图书销售统计图",new Font("黑体",Font.ITALIC,22)));
  //取得统计图表的第一个图例,并修改图例的字体
  LegendTitle legend = chart.getLegend(0);
  legend.setItemFont(new Font("宋体",Font.ITALIC,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.PLAIN,22));//设置纵轴的标签(字体)
  //输出到图片文件
  FileOutputStream fos = null;
  fos = new FileOutputStream("line3D1.jpg");
  //将统计图表输出成JPG文件
  ChartUtilities.writeChartAsJPEG(fos, 1, chart, 800, 600); //输出并生成文件到当前文件目录下
  fos.close();
 }
 
 private static CategoryDataset getDataSet()
 {
  //提供创建统计图表的数据
  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  dataset.addValue(45000, "北京", "疯狂JAVA讲义");
  dataset.addValue(38000, "北京", "轻量级JAVA EE企业实战");
  dataset.addValue(24000, "北京", "疯狂AJAX讲义");
  dataset.addValue(32000, "北京", "STRUTS 2权威指南");
  dataset.addValue(21000, "北京", "疯狂XML讲义");
  dataset.addValue(37000, "上海", "疯狂JAVA讲义");
  dataset.addValue(36000, "上海", "轻量级JAVA EE企业实战");
  dataset.addValue(34000, "上海", "疯狂AJAX讲义");
  dataset.addValue(42000, "上海", "STRUTS 2权威指南");
  dataset.addValue(12000, "上海", "疯狂XML讲义");
  dataset.addValue(42000, "广州", "疯狂JAVA讲义");
  dataset.addValue(40000, "广州", "轻量级JAVA EE企业实战");
  dataset.addValue(34000, "广州", "疯狂AJAX讲义");
  dataset.addValue(18000, "广州", "STRUTS 2权威指南");
  dataset.addValue(26000, "广州", "疯狂XML讲义");
  return dataset;
 }
}

 

效果图如下:

 

2。时序图

 

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.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
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 {

 public static void main(String[] args) throws IOException
 {
  JFreeChart chart = ChartFactory.createTimeSeriesChart(
    "图书销售统计图",  //标题
    "图书",          //横轴
    "销量",          //纵轴
    getDataSet(),
    true,
    false,
    false);
  //重新设置图表标题,改变字体
  chart.setTitle(new TextTitle("图书销售统计图",new Font("黑体",Font.ITALIC,22)));
  //取得统计图表的第一个图例,并修改图例的字体
  LegendTitle legend = chart.getLegend(0);
  legend.setItemFont(new Font("宋体",Font.ITALIC,14));
  
  XYPlot plot = (XYPlot)chart.getPlot();
  //取得横轴
  ValueAxis categoryAxis = plot.getDomainAxis();
  //设置横轴显示标签的字体
  categoryAxis.setLabelFont(new Font("宋体",Font.BOLD,22)); //设置横轴的标签(字体)
  //分类标签以45度角倾斜
  categoryAxis.setTickLabelFont(new Font("宋体",Font.BOLD,18)); //设置横轴的每个柱子的标签(字体)
  //取得纵轴
  NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
  //设置纵轴显示标签的字体
  numberAxis.setLabelFont(new Font("宋体",Font.PLAIN,22));//设置纵轴的标签(字体)
  //输出到图片文件
  FileOutputStream fos = null;
  fos = new FileOutputStream("timer1.jpg");
  //将统计图表输出成JPG文件
  ChartUtilities.writeChartAsJPEG(fos, 1, chart, 800, 600); //输出并生成文件到当前文件目录下
  fos.close();
 }
 
 private static XYDataset getDataSet()
 {
  //创建第一个TimeSeries对象
  TimeSeries first = new TimeSeries("疯狂JAVA讲义",Month.class);
  first.add(new Month(10,2006),3400);
  first.add(new Month(11,2006),2700);
  first.add(new Month(12,2006),3100);
  first.add(new Month(1,2007),1800);
  first.add(new Month(2,2007),2200);
  //创建第二个TimeSeries对象
  TimeSeries second = new TimeSeries("疯狂AJAX讲义",Month.class);
  second.add(new Month(10,2006),2800);
  second.add(new Month(11,2006),3700);
  second.add(new Month(12,2006),2600);
  second.add(new Month(1,2007),2100);
  second.add(new Month(2,2007),1100);
  //将两个TimeSeries对象组合成TimeSeriesCollection
  TimeSeriesCollection dataset = new TimeSeriesCollection();
  dataset.addSeries(first);
  dataset.addSeries(second);
  return dataset;
 }
}

效果图: