jfreechart应用总结

jar包:之前用jfreechart-1.0.0的包时,不支持组合图片生成。

1.构造数据源

       1) CategoryDataset data = new DefaultCategoryDataset();
  
data.addValue(y, series1, x);

  2)DatasetUtilities.createCategoryDataset(rowKeys, colKeys, dataPlf))  //(序列数组,横轴数组,数据集)

2.绘图,生成JFreeChart对象  

/**
     * 组合图
     * @param categoryDataset
     * @param title
     * @param x
     * @param y1
     * @param y2
     * @return
     */
     public JFreeChart createChartTwo(List<CategoryDataset> categoryDataset,String title,String x,String y1,String y2){
            //创建主题样式  
            StandardChartTheme standardChartTheme=new StandardChartTheme("CN"); 
            //设置标题字体  
            standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));  
            //设置图例的字体  
            standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));  
            //设置轴向的字体  
            standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,10));  
            //应用主题样式  
            ChartFactory.setChartTheme(standardChartTheme);      
            //y1轴
             NumberAxis numberaxis = new NumberAxis(y1);
             numberaxis.setUpperBound(1D);
            numberaxis.setLowerBound(0D);
             numberaxis.setTickMarksVisible(true);
            numberaxis.setTickLabelsVisible(true);
            LineAndShapeRenderer lineandshaperenderer = new LineAndShapeRenderer();
            CategoryPlot categoryplot = new CategoryPlot(categoryDataset.get(0), null,
                    numberaxis, lineandshaperenderer);
            categoryplot.setDomainGridlinesVisible(true);
            
            //y2轴
            NumberAxis numberaxis1 = new NumberAxis(y2);
            numberaxis1.setUpperBound(3D);
            numberaxis1.setLowerBound(0D);
            numberaxis1.setTickMarksVisible(true);
            numberaxis1.setTickLabelsVisible(true);
            LineAndShapeRenderer lineandshaperenderer2 = new LineAndShapeRenderer();
            CategoryPlot categoryplot1 = new CategoryPlot(categoryDataset.get(1), null,
                    numberaxis1, lineandshaperenderer2);
            categoryplot1.setDomainGridlinesVisible(true);
             
            // 创建组合数据集
            CategoryAxis categoryaxis = new CategoryAxis();
            categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); 
            CombinedDomainCategoryPlot combineddomaincategoryplot = new CombinedDomainCategoryPlot(
                    categoryaxis);
            //参数二标识纵坐标占比,两个1标识同样高
            combineddomaincategoryplot.add(categoryplot, 1);
            combineddomaincategoryplot.add(categoryplot1, 1);
            // 创建图表对象
            JFreeChart jfreechart = new JFreeChart(title, null,
                    combineddomaincategoryplot, true);
            // 使用CategoryPlot设置各种参数。以下设置可以省略。  
            CategoryPlot plot = (CategoryPlot)jfreechart.getPlot();  
            //轴对象
            
         return jfreechart;
     }
     /**
      * 双y轴 
      * @param categoryDataset
      * @param title
      * @param x
      * @param y1
      * @param y2
      * @return
      */
    public JFreeChart createChart(List<CategoryDataset> categoryDataset,String title,String x,String y1,String y2) {  
        //创建主题样式  
        StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); 
        //设置标题字体  
        standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));  
        //设置图例的字体  
        standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));  
        //设置轴向的字体  
        standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,10));  
        //应用主题样式  
        ChartFactory.setChartTheme(standardChartTheme);  
        // 创建JFreeChart对象:ChartFactory.createLineChart  
        JFreeChart jfreechart = ChartFactory.createLineChart(title, // 标题  
                x, // categoryAxisLabel (category轴,横轴,X轴标签)  
                y1, // valueAxisLabel(value轴,纵轴,Y轴的标签)  
                categoryDataset.get(0), // dataset  
                PlotOrientation.VERTICAL,//绘制方向
                true, // legend  
                true, // tooltips  
                false); // URLs  
        // 使用CategoryPlot设置各种参数。以下设置可以省略。  
        CategoryPlot plot = jfreechart.getCategoryPlot();
        plot.setBackgroundPaint(new Color(0xEE, 0xEE, 0xFF));
        plot.setRangeGridlinePaint(Color.BLUE); //纵坐标格线颜色
        plot.setDomainGridlinePaint(Color.BLACK); //横坐标格线颜色
        plot.setDomainGridlinesVisible(true); //显示横坐标格线
        plot.setRangeGridlinesVisible(true); //显示纵坐标格线
        plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
        plot.setBackgroundAlpha(0.5f);
        plot.setForegroundAlpha(0.8f);
        ValueAxis valueAxis = (ValueAxis) plot.getRangeAxis();
        valueAxis.setUpperBound(1); // 数据轴上的显示最大值
        valueAxis.setLowerBound(0); // 数据轴上的显示最小值
        plot.setRangeAxis(valueAxis);
      
        //轴对象
        CategoryAxis categoryaxis = plot.getDomainAxis();
        categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); 
        // 添加第2个Y轴  
        ValueAxis axis2 = new NumberAxis(y2);  
        axis2.setUpperBound(3D);
        axis2.setLowerBound(0D);
        
        LineAndShapeRenderer renderer2 = new LineAndShapeRenderer();
        
        ///设置数据点是否可见
        renderer2.setBaseShapesVisible(false);
        //设置数据连线是否可见
        renderer2.setBaseLinesVisible(true);
        plot.setRangeAxis(1, axis2);   
        plot.setDataset(1, categoryDataset.get(1)); 
        plot.setRenderer(1,renderer2);
        plot.mapDatasetToRangeAxis(1, 1);
        plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);
        return jfreechart;  
    }  
  

 

3.保存

  ChartUtilities.saveChartAsJPEG(file, chart, width, height);  //保存为文件

  ChartUtilities.writeChartAsJPEG(outputStram, chart, width, height);  //保存到输出流

  

 

转载于:https://www.cnblogs.com/zengjp/p/7797390.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值