Jfreechar混合图(折线图+柱状图)

package com.gamemag.web.statistics;
 
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.List;
 
import javax.servlet.http.HttpSession;
 
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
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.entity.StandardEntityCollection;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.TextAnchor;
 
import com.gamemag.vo.statistics.ChargeStaVo;
 
public class ChargeStaChar {
 
    private static CategoryDataset createDataset1(List<ChargeStaVo> list) {
       String price = "充值总金额";
       DefaultCategoryDataset dataset = new DefaultCategoryDataset();
       int num = 0;
       for (int i = 0; i < list.size(); i++) {
           num++;
           if (num <= 14) {
              dataset.setValue(Integer.parseInt(String.valueOf(list.get(i)
                     .getPrice())), price, list.get(i).getDateTime()
                     .toString().substring(0, 10));
           }
       }
 
       return dataset;
    }
 
    private static CategoryDataset createDataset2(List<ChargeStaVo> list) {
       String price = "新增充值用户充值金额";
       DefaultCategoryDataset dataset = new DefaultCategoryDataset();
       int num = 0;
       for (int i = 0; i < list.size(); i++) {
           num++;
           if (num <= 14) {
              dataset.setValue(list.get(i).getAddNewPrice(), price, list.get(
                     i).getDateTime().toString().substring(0, 10));
           }
       }
 
       return dataset;
    }
 
    private static JFreeChart createChart(String chartTitle, String Xname,
           String Yname, CategoryDataset dataset1, CategoryDataset dataset2) {
 
       JFreeChart chart = ChartFactory.createBarChart(chartTitle, Xname,
              Yname, dataset1, PlotOrientation.VERTICAL, true, false, false);
       //chart.setBackgroundPaint(Color.white);
 
       chart.getLegend().setPosition(RectangleEdge.RIGHT); // 图例居右
       CategoryPlot categoryplot = chart.getCategoryPlot();// 获得图表对象
       //categoryplot.setDomainGridlinesVisible(true);// x轴 // 分类轴网格是否可见
       //categoryplot.setDomainGridlinePaint(Color.blue);// 虚线色彩
       categoryplot.setRangeGridlinesVisible(true);// y轴 //数据轴网格是否可见
       categoryplot.setRangeGridlinePaint(Color.WHITE);// 虚线色彩
       // 图例字体清晰
       chart.getLegend().setItemFont(new Font("宋体", Font.TRUETYPE_FONT, 14));
       chart.setTextAntiAlias(false);
       // 主标题对象 主标题对象是 TextTitle 类型
       chart.setTitle(new TextTitle(chartTitle, new Font("隶书", Font.TRUETYPE_FONT, 25)));
       // 设置中文
       // x,y轴坐标字体
       Font labelFont = new Font("宋体", Font.TRUETYPE_FONT, 12);
 
       // Y轴数据设置
       NumberAxis vn = (NumberAxis) categoryplot.getRangeAxis();
       vn.setUpperMargin(0.1);
       // 设置最大值是1
       // vn.setUpperBound(1);
       // 设置数据轴坐标从0开始
       // vn.setAutoRangeIncludesZero(true);
       // 数据显示格式是百分比
       //DecimalFormat df = new DecimalFormat("0.0%");
       DecimalFormat df = new DecimalFormat();
       vn.setNumberFormatOverride(df); // 数据轴数据标签的显示格式
 
       // X轴坐标的显示中文 domainAxis是X轴
       CategoryAxis domainAxis = categoryplot.getDomainAxis();
       domainAxis.setLabelFont(labelFont);// 轴标题
       domainAxis.setTickLabelFont(labelFont);// 轴数值
 
       // y轴设置
       ValueAxis rangeAxis = categoryplot.getRangeAxis();
       rangeAxis.setLabelFont(labelFont); // 轴标题
       rangeAxis.setTickLabelFont(labelFont);// 轴数值
 
       //categoryplot.setBackgroundPaint(new Color(238, 238, 255));
        categoryplot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
       // 创建折线图
       CategoryDataset categorydataset = dataset2;
       // 0显示是柱状图,1显示折线图
       categoryplot.setDataset(1, categorydataset);
       // 显示折线图,0,0坐标轴都在左侧
       categoryplot.mapDatasetToRangeAxis(0, 0);
 
       // 设置横轴的label为45度
       CategoryAxis categoryaxis = categoryplot.getDomainAxis();
       categoryaxis.setMaximumCategoryLabelWidthRatio(4f);
       categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
 
       // 在右边显示折线图的坐标和主题
       // NumberAxis numberaxis = new NumberAxis("平均值");
       // / numberaxis.setLabelFont(labelFont);
       // categoryplot.setRangeAxis(1, numberaxis);
 
       // 画折线图
       LineAndShapeRenderer lineandshaperenderer = new LineAndShapeRenderer();
       lineandshaperenderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
        //显示数值
       //lineandshaperenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
       //lineandshaperenderer.setBaseItemLabelsVisible(true);
       categoryplot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);//设置折线图显示在柱形图上面
       BasicStroke realLine = new BasicStroke(5.0f); // 设置实线
       lineandshaperenderer.setSeriesStroke(0, realLine);
       lineandshaperenderer.setShapesVisible(false);//去掉折线的点
       categoryplot.setRenderer(1, lineandshaperenderer);
      
       // 图例字体清晰
 
       // 画柱状图
       StackedBarRenderer renderer = new StackedBarRenderer();
      
       //让柱子显示为一个平面图  
       renderer.setBarPainter(new StandardBarPainter());
       renderer.setItemMargin(-0.01);
       renderer.setShadowVisible(false);//取消阴影
      
       renderer.setItemLabelsVisible(true);
 
       // 设置柱子宽度
       renderer.setMaximumBarWidth(0.03);
       // 设置柱子高度
       renderer.setMinimumBarLength(0.1);
       // 设置柱的边框颜色
       //renderer.setBaseOutlinePaint(Color.BLACK);
       // 设置柱的边框可见
       renderer.setDrawBarOutline(true);
       // // 设置柱的颜色(可设定也可默认)
       renderer.setSeriesPaint(0, new Color(51, 102, 153));
       //renderer.setSeriesPaint(1, new Color(255, 204, 153));
       // 设置每个地区所包含的平行柱的之间距离
       renderer.setItemMargin(0.4);
      
       // 显示数据
       //renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
       // 显示数据的格式是百分比
       //renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", NumberFormat.getPercentInstance()));
       renderer.setItemLabelFont(new Font("黑体", Font.BOLD, 12));
       renderer.setItemLabelsVisible(true);
       // 数据显示格式是百分比
       ItemLabelPosition itemLabelPositionFallback = new ItemLabelPosition(
              ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT,
              TextAnchor.HALF_ASCENT_LEFT, -1.57D);
       // 显示小于显示范围的数据
       renderer.setPositiveItemLabelPositionFallback(itemLabelPositionFallback);
       renderer.setNegativeItemLabelPositionFallback(itemLabelPositionFallback);
      
       // 显示柱状图
       categoryplot.setRenderer(0, renderer);
      
       /*ChartFrame pieFrame = new ChartFrame("折线图预警图",chart);   
        pieFrame.pack();   
        pieFrame.setVisible(true);  */
       return chart;
    }
 
    public static String generateChart(HttpSession session, PrintWriter out,
           List<ChargeStaVo> list) throws IOException {
       String filename = null;
       CategoryDataset dataset1 = createDataset1(list);
       CategoryDataset dataset2 = createDataset2(list);
       JFreeChart chart = createChart("近"+list.size()+"天《武将杀》充值情况", "", "", dataset1,
              dataset2);
      
       /**
        * File directory = new File(".");
        * System.out.println(directory.getCanonicalPath()+"=========="+img_path);
        *
        * FileOutputStream fos_jpg = null; try {
        *  fos_jpg = new  FileOutputStream("fruit.jpg");
        *      ChartUtilities.writeChartAsJPEG(fos_jpg, 1.0f, chart, 400,300,null);
        * } finally {
        *      try {
        *         fos_jpg.close();
        * } catch (Exception e) {} }
        **/
      
       ChartRenderingInfo info = new ChartRenderingInfo(
              new StandardEntityCollection());
       try {
           filename = ServletUtilities.saveChartAsPNG(chart, 900, 350, info,
                  session);
           ChartUtilities.writeImageMap(out, filename, info, false);
       } catch (IOException e) {
           e.printStackTrace();
       }
       out.flush();
       return filename;
    }
 
}
 

 

你可能感兴趣的:(char)