使用JFreeChart生成带刻度的雷达图(蜘蛛网图)

JFreeChart是老牌Java开源图表工具包,也是JFreeReport的重要组成部分,在业界被广泛应用。当然,这个工具包依然有很多美中不足,确切的说,有很多具体的应用还需要我们拓展,比如蜘蛛网图(Spider Web)的刻度问题。
带刻度问题比较复杂,JFreeChart本身没有提供其功能,需要继承SpiderWebPlot,覆盖drawLabel方法。

开发中需要用到jcommon-1.0.16.jar和jfreechart-1.0.13.jar

为了方便我只提供Struts下的ACTION中需要用到的生成图表的类中的全部代码

public class MySpiderWebPlot extends SpiderWebPlot { 
    /**
	 * 
	 */
	private static final long serialVersionUID = 4005814203754627127L;
	private int ticks = DEFAULT_TICKS; 
    private static final int DEFAULT_TICKS = 5; 
    private NumberFormat format = NumberFormat.getInstance(); 
    private static final double PERPENDICULAR = 90; 
    private static final double TICK_SCALE = 0.015; 
    private int valueLabelGap = DEFAULT_GAP; 
    private static final int DEFAULT_GAP = 10; 
    private static final double THRESHOLD = 15; 
   
    
  
    MySpiderWebPlot(CategoryDataset createCategoryDataset) { 
            super(createCategoryDataset); 
    } 
    @Override 
    protected void drawLabel(final Graphics2D g2, final Rectangle2D plotArea, final double value, 
                    final int cat, final double startAngle, final double extent) { 
            super.drawLabel(g2, plotArea, value, cat, startAngle, extent); 
            final FontRenderContext frc = g2.getFontRenderContext(); 
            final double[] transformed = new double[2]; 
            final double[] transformer = new double[2]; 
            final Arc2D arc1 = new Arc2D.Double(plotArea, startAngle, 0, Arc2D.OPEN); 
            for (int i = 1; i <= ticks; i++) { 
                    final Point2D point1 = arc1.getEndPoint(); 
                    final double deltaX = plotArea.getCenterX(); 
                    final double deltaY = plotArea.getCenterY(); 
                    double labelX = point1.getX() - deltaX; 
                    double labelY = point1.getY() - deltaY; 
                    final double scale = ((double) i / (double) ticks); 
                    final AffineTransform tx = AffineTransform.getScaleInstance(scale, scale); 
                    final AffineTransform pointTrans = AffineTransform.getScaleInstance(scale + TICK_SCALE, scale + TICK_SCALE); 
                    transformer[0] = labelX; 
                    transformer[1] = labelY; 
                    pointTrans.transform(transformer, 0, transformed, 0, 1); 
                    final double pointX = transformed[0] + deltaX; 
                    final double pointY = transformed[1] + deltaY; 
                    tx.transform(transformer, 0, transformed, 0, 1); 
                    labelX = transformed[0] + deltaX; 
                    labelY = transformed[1] + deltaY; 
                    double rotated = (PERPENDICULAR); 
                    AffineTransform rotateTrans = AffineTransform.getRotateInstance(Math.toRadians(rotated), labelX, labelY); 
                    transformer[0] = pointX; 
                    transformer[1] = pointY; 
                    rotateTrans.transform(transformer, 0, transformed, 0, 1); 
                    final double x1 = transformed[0]; 
                    final double y1 = transformed[1]; 
                    rotated = (-PERPENDICULAR); 
                    rotateTrans = AffineTransform.getRotateInstance(Math.toRadians(rotated), labelX, labelY); 
                    rotateTrans.transform(transformer, 0, transformed, 0, 1); 
                    final Composite saveComposite = g2.getComposite(); 
                    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)); 
                    g2.draw(new Line2D.Double(transformed[0], transformed[1], x1, y1)); 
                    if (startAngle == this.getStartAngle()) { 
                            final String label = format.format(((double) i / (double) ticks) * this.getMaxValue()); 
                            final LineMetrics lm = getLabelFont().getLineMetrics(label, frc); 
                            final double ascent = lm.getAscent(); 
                            if (Math.abs(labelX - plotArea.getCenterX()) < THRESHOLD) { 
                                    labelX += valueLabelGap; 
                                    labelY += ascent / (float) 2; 
                            } else if (Math.abs(labelY - plotArea.getCenterY()) < THRESHOLD) { 
                                    labelY += valueLabelGap; 
                            } else if (labelX >= plotArea.getCenterX()) { 
                                    if (labelY < plotArea.getCenterY()) { 
                                            labelX += valueLabelGap; 
                                            labelY += valueLabelGap; 
                                    } else { 
                                            labelX -= valueLabelGap; 
                                            labelY += valueLabelGap; 
                                    } 
                            } else { 
                                    if (labelY > plotArea.getCenterY()) { 
                                            labelX -= valueLabelGap; 
                                            labelY -= valueLabelGap; 
                                    } else { 
                                            labelX += valueLabelGap; 
                                            labelY -= valueLabelGap; 
                                    } 
                            } 
                            g2.setPaint(getLabelPaint()); 
                            g2.setFont(getLabelFont()); 
                            g2.drawString(label, (float) labelX, (float) labelY); 
                    } 
                    g2.setComposite(saveComposite); 
            } 
    } 
}


public class MySpriderWebPlotTest {
    public static void main(String args[]) {
     //在SWING中显示
        JFrame jf = new JFrame();
        jf.add(erstelleSpinnenDiagramm());
        jf.pack();
        jf.setVisible(true);
        //将JFreeChart保存为图片存在文件路径中
        saveAsFile("E:/JfreeChart/MySpiderWebPlot.png",500,400);
    }
    public static JPanel erstelleSpinnenDiagramm() {
        JFreeChart jfreechart =createChart();
        ChartPanel chartpanel = new ChartPanel(jfreechart);
        return chartpanel;
    }
    
    
    public static void saveAsFile(String outputPath,
   int weight, int height) {
  FileOutputStream out = null;
  try {
   File outFile = new File(outputPath);
   if (!outFile.getParentFile().exists()) {
    outFile.getParentFile().mkdirs();
   }
   out = new FileOutputStream(outputPath);

   // 保存为PNG
   ChartUtilities.writeChartAsPNG(out, createChart(),weight, height);
   // 保存为JPEG
   // ChartUtilities.writeChartAsJPEG(out, chart, 500, 400);
   out.flush();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if (out != null) {
    try {
     out.close();
    } catch (IOException e) {
     // do nothing
    }
   }
  }
 }
 public static JFreeChart createChart() {
     MySpiderWebPlot spiderwebplot = new MySpiderWebPlot(createDataset());
         JFreeChart jfreechart = new JFreeChart("前三个季度水果销售报告", TextTitle.DEFAULT_FONT,spiderwebplot, false);
         LegendTitle legendtitle = new LegendTitle(spiderwebplot);
         legendtitle.setPosition(RectangleEdge.BOTTOM);
         jfreechart.addSubtitle(legendtitle);
         return jfreechart;
 }
 public static DefaultCategoryDataset createDataset() {
   DefaultCategoryDataset dataset = new DefaultCategoryDataset();
         String group1 = "苹果 ";
         
         dataset.addValue(5, group1, "一月份");
         dataset.addValue(6, group1, "二月份");
         dataset.addValue(4, group1, "三月份");
         dataset.addValue(2, group1, "四月份");
         dataset.addValue(5, group1, "五月份");
         dataset.addValue(5, group1, "六月份");
         dataset.addValue(5, group1, "七月份");
         dataset.addValue(8, group1, "八月份");
         
         String group2 = "橙子";
         dataset.addValue(3, group2, "一月份");
         dataset.addValue(3, group2, "二月份");
         dataset.addValue(4, group2, "三月份");
         dataset.addValue(7, group2, "四月份");
         dataset.addValue(4, group2, "五月份");
         dataset.addValue(5, group2, "六月份");
         dataset.addValue(3, group2, "七月份");
         dataset.addValue(3, group2, "八月份");
         
         String group3 = "香蕉";
         dataset.addValue(4, group3, "一月份");
         dataset.addValue(5, group3, "二月份");
         dataset.addValue(2, group3, "三月份");
         dataset.addValue(5, group3, "四月份");
         dataset.addValue(6, group3, "五月份");
         dataset.addValue(6, group3, "六月份");
         dataset.addValue(4, group3, "七月份");
         dataset.addValue(4, group3, "八月份");
         return dataset;
 }
}

 

 

 

顺便说下,在开发中我常用到的生成jFreeChart的步骤有三个,首先我会先创建数据集合DataSet,然后通过数据集合我会创建jFreeChart对象,最后我会通过jFreeChart提供的ChartUtilities类的writeChartAsPNG()方法将创建为图片保存到磁盘文件中,如果保存到与项目相对路径的话在WEB页面就可以呈现出我们生成的jFreeChart图片了!

你可能感兴趣的:(Web,swing,struts,jfreechart)