jfreechart 多坐标轴组合图

时间:2022-02-15 15:00:08

       最近做个报表项目,要用图表来明确直观的看出数据变化的趋势,网上一搜jfreechart很好用,但网上都是些简单的用法。因为经理要求要用多坐标轴,而在网上又很少找到相关的例子,没办法呀,说真的一开始,真是一头雾水,只有英文版的操作手册,对我这个英文很烂的人来说,真是太难了,但是领导的话,不能不听呀!O(∩_∩)O哈哈~,只好硬着头皮往下看咯。

       好了,废话少说,下面进入正题!

       本文主要介绍下CombinedDomainCategoryPlot类在组合分类轴时的例子,当然也可以用CombinedRangeCategoryPlot类来组合数值轴。

首先要导入相关的类,核心代码如下:

<%
    DefaultCategoryDataset dataset0 =new DefaultCategoryDataset();
  if(rsArrayList.size()>0 && rsArrayList!=null)
  {
   for(int i= 0; i < rsArrayList.size(); i++)
   {
    java.util.Map  map = (java.util.HashMap) rsArrayList.get(i);
    String swjg_mc = map.get("SWJG_MC").toString();
    String swjgmc=(swjg_mc.replaceAll("市国家税务局","")).replaceAll("安徽省","");
    String BYLSBL_ZT=(map.get("BYLSBL_ZT").toString());
    String LJLSBL_YB=(map.get("LJLSBL_YB").toString());
    String LJLSBL_XG=(map.get("LJLSBL_XG").toString());
    dataset0.addValue(Double.parseDouble(BYLSBL_ZT),"蓝色预警比例",swjgmc);
    dataset0.addValue(Double.parseDouble(LJLSBL_YB),"黄色预警比例",swjgmc);
    dataset0.addValue(Double.parseDouble(LJLSBL_XG),"红色预警比例",swjgmc);
   }
  }
  NumberAxis numberAxis0 = new NumberAxis("合肥市");
  numberAxis0.setStandardTickUnits(NumberAxis.createStandardTickUnits());
  //numberAxis0.setAutoRangeIncludesZero(false);
  //设置数值轴的最小值  
    numberAxis0.setLowerBound(0);  
    //设置数值轴的最大值  
    numberAxis0.setUpperBound(1.0);
    //numberAxis0.setAutoRangeMinimumSize(0.1d);
  //BarRenderer3D renderer0 = new BarRenderer3D();
  StackedBarRenderer3D renderer0 = new StackedBarRenderer3D();
  //设置每种水果代表的柱的颜色
  renderer0.setSeriesPaint(2, Color.red);
  renderer0.setSeriesPaint(1, Color.yellow);
  renderer0.setSeriesPaint(0, Color.blue);  
  renderer0.setItemMargin(0.2);
  //设置当鼠标放到相应颜色的柱上面,显示相应的详细信息
  //renderer0.setBaseToolTipGenerator(new StandardCategoryItemLabelGenerator());
        CategoryPlot subplot0 = new CategoryPlot(dataset0, null, numberAxis0, renderer0);
        subplot0.setDomainGridlinesVisible(true);
       
        NumberAxis numberAxis1 = new NumberAxis("芜湖市");
        BarRenderer3D renderer1 = new BarRenderer3D();
        //设置每种水果代表的柱的颜色
  renderer1.setSeriesPaint(0, Color.red);
  renderer1.setSeriesPaint(1, Color.yellow);
  renderer1.setSeriesPaint(2, Color.blue);  
  renderer1.setItemMargin(0.1);
        CategoryPlot subplot1 = new CategoryPlot(dataset0, null, numberAxis1, renderer1);
        subplot0.setDomainGridlinesVisible(true);
       
        CategoryAxis domainAxis = new CategoryAxis("所有行业");
  CombinedDomainCategoryPlot combinedcategoryplot = new CombinedDomainCategoryPlot(domainAxis);
  combinedcategoryplot.add(subplot0, 2);
        combinedcategoryplot.add(subplot1, 2);
        combinedcategoryplot.setOrientation(PlotOrientation.HORIZONTAL);
        //java docs says JFreeChart(java.lang.String title, java.awt.Font titleFont, Plot plot, boolean createLegend)
        JFreeChart jfreechart = new JFreeChart("Combined Range Category Plot Demo", new Font("SansSerif", 1, 12), combinedcategoryplot, false);
        //---------combinedjfreechart--end--
  //设置URL的链接
  CategoryURLGenerator urls = new StandardCategoryURLGenerator("left.jsp&swjg_dm=");
  renderer0.setItemURLGenerator(urls);
  renderer1.setItemURLGenerator(urls);
  //页面根据dataset的值生成相应的值
  StandardEntityCollection sec = new StandardEntityCollection();
  ChartRenderingInfo info = new ChartRenderingInfo(sec);
  PrintWriter w = new PrintWriter(out);//输出MAP信息
  int picWidth = tasks.size()*30;
  String filename ="";
  if(tasks.size()<10){
   //filename =ServletUtilities.saveChartAsPNG(chart, 500, 400, info, session);
   filename =ServletUtilities.saveChartAsPNG(jfreechart, 500, 400, info, session);
  }else{
   if(picWidth>=500){
    filename =ServletUtilities.saveChartAsPNG(jfreechart, picWidth, 400, info, session);
   }else{
    filename =ServletUtilities.saveChartAsPNG(jfreechart, 500, 400, info, session);
   }
  }
  
  org.jfree.chart.ChartUtilities.writeImageMap(w, "map0", info, false);
  String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename; 
%>
  <%if(tasks.size()<10){%>
   <img src="<%=graphURL %>" width=500 height=400 border=0 usemap="#map0">
  <%}else{
   if(picWidth>=500){
  %>
    <img src="<%=graphURL %>" width=<%=picWidth%> height=400 border=0 usemap="#map0">
   <%}else{%>
    <img src="<%=graphURL %>" width=500 height=400 border=0 usemap="#map0">
  <%}}%>

至于,一些环境方面的设置,网上已经有很多,这里就不多说了!