Java根据word模板生成word文档之后台解析和实现及部分代码(三)D

来源:互联网 发布:seo网络推广公司 编辑:程序博客网 时间:2024/04/28 20:48

现在贴出我封装了的jfreechar工具类,可以根据自己的要求去扩展和封装,大概封装了一下:

import java.awt.BasicStroke;import java.awt.Color;import java.awt.Font;import java.awt.GradientPaint;import java.awt.RenderingHints;import java.io.FileOutputStream;import java.text.DecimalFormat;import java.text.NumberFormat;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.labels.ItemLabelAnchor;import org.jfree.chart.labels.ItemLabelPosition;import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;import org.jfree.chart.labels.StandardPieSectionLabelGenerator;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.plot.PiePlot;import org.jfree.chart.plot.PlotOrientation;import org.jfree.chart.plot.PolarPlot;import org.jfree.chart.renderer.DefaultPolarItemRenderer;import org.jfree.chart.renderer.category.BarRenderer3D;import org.jfree.chart.renderer.category.LineAndShapeRenderer;import org.jfree.chart.servlet.ServletUtilities;import org.jfree.chart.title.TextTitle;import org.jfree.data.category.CategoryDataset;import org.jfree.data.general.PieDataset;import org.jfree.data.xy.XYDataset;import org.jfree.ui.TextAnchor;import org.springframework.stereotype.Component;/** * 通用的生成柱状图的方法 * @author th * */@Componentpublic class CommonStatisticsCreateImagesDao extends ServletUtilities{/** * 生成多柱状图(柱状图) * @param imagePath 生成后的图片路径 * @param imageName 生成后的图片名称 * @param categoryDataset  生成图片的数据结构 * @param yName  Y坐标的展示名称 * @param width  图片宽度 * @param height  图片高度 * @return */public boolean creatPic(String imagePath,String imageName,String yName,CategoryDataset categoryDataset, int width,int height){       //设置默认宽度和高度    if(width == 0)    width = 700;    if(height == 0)       height = 450;    // 图表标题     // 目录轴的显示标签 x// 数值轴的显示标签 y// 数据集 // 图表方向:水平、垂直 // 是否显示图例(对于简单的柱状图必须是false) // 是否生成工具 // 是否生成URL链接  createBarChart        JFreeChart jfreechart = ChartFactory.createBarChart3D(imageName, "", yName, categoryDataset, PlotOrientation.VERTICAL, true, true, false);        // 设置图标题的字体重新设置title new Font("宋体", Font.PLAIN, 10)    Font font = new Font("隶书", Font.BOLD, 25);     TextTitle title = new TextTitle(imageName);     title.setFont(font);     jfreechart.setTitle(title);         Font labelFont = new Font("黑体", Font.BOLD, 12);     /*     * VALUE_TEXT_ANTIALIAS_OFF表示将文字的抗锯齿关闭,     * 使用的关闭抗锯齿后,字体尽量选择12到14号的宋体字,这样文字最清晰好看     */     jfreechart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);     jfreechart.setTextAntiAlias(true);         jfreechart.setBackgroundPaint(Color.white);     //显示图例    jfreechart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));     // create plot     CategoryPlot plot = jfreechart.getCategoryPlot();     // 设置横虚线可见     plot.setRangeGridlinesVisible(true);     // 虚线色彩     plot.setRangeGridlinePaint(Color.gray);     // 数据轴精度     NumberAxis vn = (NumberAxis) plot.getRangeAxis();     vn.setAutoRangeIncludesZero(true);     DecimalFormat df = new DecimalFormat("#0.00");     vn.setNumberFormatOverride(df); // 数据轴数据标签的显示格式     // x轴设置     CategoryAxis domainAxis = plot.getDomainAxis();     domainAxis.setLabelFont(labelFont);// 轴标题     domainAxis.setTickLabelFont(labelFont);// 轴数值     // Lable(Math.PI/3.0)度倾斜     domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI/3.0));     domainAxis.setMaximumCategoryLabelWidthRatio(0.5f);// 横轴上的 Lable 是否完整显示     // 设置距离图片左端距离     domainAxis.setLowerMargin(0.1);     // 设置距离图片右端距离     domainAxis.setUpperMargin(0.1);     // 设置 columnKey 是否间隔显示     // domainAxis.setSkipCategoryLabelsToFit(true);     plot.setDomainAxis(domainAxis);     // 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确)     plot.setBackgroundPaint(new Color(255, 255, 204));             // y轴设置     ValueAxis rangeAxis = plot.getRangeAxis();     rangeAxis.setLabelFont(labelFont);     rangeAxis.setTickLabelFont(labelFont);     // 设置最高的一个 Item 与图片顶端的距离     rangeAxis.setUpperMargin(0.15);     // 设置最低的一个 Item 与图片底端的距离     rangeAxis.setLowerMargin(0.15);     plot.setRangeAxis(rangeAxis);     BarRenderer3D renderer = new BarRenderer3D();     // 设置柱子宽度     renderer.setMaximumBarWidth(0.05);     // 设置柱子高度     //renderer.setMinimumBarLength(0.2);     // 设置柱子边框颜色     //renderer.setBaseOutlinePaint(Color.BLACK);     // 设置柱子边框可见     //renderer.setDrawBarOutline(true);     //设置柱的颜色     renderer.setSeriesPaint(0, new GradientPaint(0.0F, 0.0F, Color.green, 0.0F, 0.0F, new Color(0, 255, 0)));    renderer.setSeriesPaint(1, new GradientPaint(0.0F, 0.0F, Color.blue, 0.0F, 0.0F, new Color(0, 0, 255)));    renderer.setSeriesPaint(2, new GradientPaint(0.0F, 0.0F, Color.yellow, 0.0F, 0.0F, new Color(255, 255, 0)));    renderer.setSeriesPaint(3, new GradientPaint(0.0F, 0.0F, Color.orange, 0.0F, 0.0F, new Color(255, 200, 0)));    renderer.setSeriesPaint(4, new GradientPaint(0.0F, 0.0F, Color.red, 0.0F, 0.0F, new Color(255, 0, 0)));    // 设置每个地区所包含的平行柱的之间距离     //renderer.setItemMargin(0.2);     // 显示每个柱的数值,并修改该数值的字体属性     renderer.setIncludeBaseInRange(true);     renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());     renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));//CENTER_RIGHT    renderer.setBaseItemLabelsVisible(true);     plot.setRenderer(renderer);    // 设置柱的透明度     plot.setForegroundAlpha(1.0f);               //生成水印类似//        CategoryTextAnnotation categorytextannotation = new CategoryTextAnnotation("Minimum grade to pass", "Robert", 0.70999999999999996D);//        categorytextannotation.setCategoryAnchor(CategoryAnchor.START);//        categorytextannotation.setFont(new Font("SansSerif", 0, 12));//        categorytextannotation.setTextAnchor(TextAnchor.BOTTOM_LEFT);//        categoryplot.addAnnotation(categorytextannotation);                FileOutputStream fos_jpg = null;     try{    fos_jpg = new FileOutputStream(imagePath);     ChartUtilities.writeChartAsPNG(fos_jpg,jfreechart,width,height);     }catch (Exception e) {     e.printStackTrace();     return false;     }finally{     try{     fos_jpg.close();     System.out.println("create pie-chart.柱状图创建成功!!!");     }catch (Exception e){     e.printStackTrace();       }     }     return true;    } /** * 生成(曲线图片) * @param imagePath 生成后的图片路径 * @param imageName 生成后的图片名称 * @param categoryDataset  生成图片的数据结构 * @param yName  Y坐标的展示名称 * @param width  图片宽度 * @param height  图片高度 * @return */ public boolean createLinePic(String imagePath,String imageName,String yName,CategoryDataset categoryDataset, int width,int height){    //设置默认宽度和高度    if(width == 0)    width = 700;    if(height == 0)       height = 450;    JFreeChart jfreechart = ChartFactory.createLineChart(imageName, null, yName, categoryDataset, PlotOrientation.VERTICAL, false, true, false);        //jfreechart.addSubtitle(new TextTitle("Number of Classes By Release"));        //TextTitle texttitle = new TextTitle("Source: Java In A Nutshell (5th Edition) by David Flanagan (O'Reilly)");        /*texttitle.setFont(new Font("SansSerif", 0, 10));        texttitle.setPosition(RectangleEdge.BOTTOM);        texttitle.setHorizontalAlignment(HorizontalAlignment.RIGHT);        jfreechart.addSubtitle(texttitle);*/        CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();        categoryplot.setRangePannable(true);        categoryplot.setRangeGridlinesVisible(false);              NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();        numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());        ChartUtilities.applyCurrentTheme(jfreechart);        LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)categoryplot.getRenderer();        lineandshaperenderer.setBaseShapesVisible(true);        lineandshaperenderer.setDrawOutlines(true);        lineandshaperenderer.setUseFillPaint(true);        lineandshaperenderer.setBaseFillPaint(Color.white);        lineandshaperenderer.setSeriesStroke(0, new BasicStroke(3F));        lineandshaperenderer.setSeriesOutlineStroke(0, new BasicStroke(2.0F));        lineandshaperenderer.setSeriesShape(0, new java.awt.geom.Ellipse2D.Double(-5D, -5D, 10D, 10D));                /*-------------处理乱码-------------------*/        Font font = new Font("隶书", Font.BOLD, 25);     TextTitle title = new TextTitle(imageName);     title.setFont(font);     jfreechart.setTitle(title);         Font labelFont = new Font("黑体", Font.BOLD, 12);         CategoryAxis categoryAxis =  categoryplot.getDomainAxis();categoryAxis.setLabelFont(labelFont);categoryAxis.setTickLabelFont(labelFont);ValueAxis valueAxis =  categoryplot.getRangeAxis();valueAxis.setLabelFont(labelFont);CategoryAxis categoryAxis2 =  categoryplot.getDomainAxis();categoryAxis2.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(0.66666));            FileOutputStream fos_jpg = null;     try{    fos_jpg = new FileOutputStream(imagePath);     ChartUtilities.writeChartAsPNG(fos_jpg,jfreechart,width,height);     }catch (Exception e) {     e.printStackTrace();     return false;     }finally{     try{     fos_jpg.close();     System.out.println("create line-chart.曲线图创建成功!!!");     }catch (Exception e){     e.printStackTrace();       }     }     return true;    }    /** * 生成(饼状图片) * @param imagePath 生成后的图片路径 * @param imageName 生成后的图片名称 * @param categoryDataset  生成图片的数据结构 * @param  * @param width  图片宽度 * @param height  图片高度 * @return */ @SuppressWarnings("deprecation")public boolean createPiePic(String imagePath,String imageName,PieDataset categoryDataset, int width,int height){    //设置默认宽度和高度    if(width == 0)    width = 700;    if(height == 0)       height = 450;                JFreeChart jfreechart = ChartFactory.createPieChart(imageName, categoryDataset, true, true, false);       // 设置图标题的字体重新设置title new Font("宋体", Font.PLAIN, 10)    Font font = new Font("隶书", Font.BOLD, 25);     TextTitle title = new TextTitle(imageName);     title.setFont(font);     jfreechart.setTitle(title);         /*     * VALUE_TEXT_ANTIALIAS_OFF表示将文字的抗锯齿关闭,     * 使用的关闭抗锯齿后,字体尽量选择12到14号的宋体字,这样文字最清晰好看     */     jfreechart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);     jfreechart.setTextAntiAlias(true);         jfreechart.setBackgroundPaint(Color.white);     jfreechart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 15));         title.setToolTipText("A title tooltip!");            PiePlot pieplot = (PiePlot)jfreechart.getPlot();        pieplot.setLabelFont(new Font("黑体",Font.PLAIN,15));        pieplot.setNoDataMessage("No data available");                pieplot.setCircular(false);        pieplot.setLabelGap(0.02D);        jfreechart.getLegend().setItemFont(new Font("黑体",Font.BOLD,15));        pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator(         "{2}", NumberFormat.getNumberInstance(),         new DecimalFormat("0.00%")));         // 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例        /* pieplot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(     "{0}={1}({2})")); */                //饼状图的颜色定义        pieplot.setSectionPaint(0, new GradientPaint(0.0F, 0.0F, Color.green, 0.0F, 0.0F, new Color(0, 255, 0)));    pieplot.setSectionPaint(1, new GradientPaint(0.0F, 0.0F, Color.blue, 0.0F, 0.0F, new Color(0, 0, 255)));    pieplot.setSectionPaint(2, new GradientPaint(0.0F, 0.0F, Color.yellow, 0.0F, 0.0F, new Color(255, 255, 0)));    pieplot.setSectionPaint(3, new GradientPaint(0.0F, 0.0F, Color.orange, 0.0F, 0.0F, new Color(255, 200, 0)));    pieplot.setSectionPaint(4, new GradientPaint(0.0F, 0.0F, Color.red, 0.0F, 0.0F, new Color(255, 0, 0)));                                /*-------------处理乱码-------------------*/                FileOutputStream fos_jpg = null;     try{    fos_jpg = new FileOutputStream(imagePath);     ChartUtilities.writeChartAsPNG(fos_jpg,jfreechart,width,height);     }catch (Exception e) {     e.printStackTrace();     return false;     }finally{     try{     fos_jpg.close();     System.out.println("create pie-chart.饼状图创建成功!!!");     }catch (Exception e){     e.printStackTrace();       }     }     return true;    }     /** * 生成(雷达图) * @param imagePath 生成后的图片路径 * @param imageName 生成后的图片名称 * @param xydataset  生成图片的数据结构 * @param width  图片宽度 * @param height  图片高度 * @param pplotArr  PolarPlot数组图例名字(生成图例的 可有可无) * @return */  public boolean createPolarChart(String imagePath,String imageName,XYDataset xydataset, int width,int height,String[] pplotArr){      //设置默认宽度和高度    if(width == 0)    width = 700;    if(height == 0)       height = 450;            JFreeChart jfreechart = ChartFactory.createPolarChart(imageName, xydataset, true, false, false);        PolarPlot polarplot = (PolarPlot)jfreechart.getPlot();//        if(pplotArr != null && pplotArr.length > 0){//        for (int i = 0; i < pplotArr.length; i++) {//        polarplot.addCornerTextItem(pplotArr[i]);//}//        }        polarplot.setAngleGridlinePaint(Color.white);        polarplot.setRadiusGridlinePaint(Color.white);        NumberAxis numberaxis = (NumberAxis)polarplot.getAxis();        numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());        ChartUtilities.applyCurrentTheme(jfreechart);               //显示网格度数        polarplot.setAngleGridlinesVisible(false);                // 设置雷达颜色        GradientPaint gradientpaint3 = new GradientPaint(0.0F, 0.0F,          Color.black, 0.0F, 0.0F, Color.black);        polarplot.setRadiusGridlinePaint(gradientpaint3);//        // 两个四边形颜色        GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F, Color.red,          0.0F, 0.0F, Color.red); //        GradientPaint gradientpaint2 = new GradientPaint(0.0F, 0.0F,          Color.blue, 0.0F, 0.0F, Color.blue); //        // 设置两个四边形颜色        DefaultPolarItemRenderer renderer = new DefaultPolarItemRenderer();        renderer.setSeriesPaint(0, gradientpaint1);        renderer.setSeriesPaint(1, gradientpaint2);        renderer.setItemLabelFont(new Font("黑体", Font.BOLD, 15));        renderer.setItemLabelsVisible(true);        polarplot.setRenderer(renderer);        numberaxis.setAutoRange(true);        //numberaxis.setVisible(false);        //numberaxis.setTickUnit(new NumberTickUnit(2));// 设置雷达网格数量                // 设置说明的字体        jfreechart.getTitle().setFont(new Font("隶书", Font.BOLD, 25));            /*     * VALUE_TEXT_ANTIALIAS_OFF表示将文字的抗锯齿关闭,     * 使用的关闭抗锯齿后,字体尽量选择12到14号的宋体字,这样文字最清晰好看     */     jfreechart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);     jfreechart.setTextAntiAlias(true);         jfreechart.setBackgroundPaint(Color.white);     //显示图例    jfreechart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));                         FileOutputStream fos_jpg = null;             try{    fos_jpg = new FileOutputStream(imagePath);     ChartUtilities.writeChartAsPNG(fos_jpg,jfreechart,width,height);     }catch (Exception e) {     e.printStackTrace();     return false;     }finally{     try{     fos_jpg.close();     System.out.println("create pie-chart.雷达图创建成功!!!");     }catch (Exception e){     e.printStackTrace();       }     }     return true;    }}


未完待续


原创粉丝点击