程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> JFreeChart蜘蛛網圖添加刻度解決方案

JFreeChart蜘蛛網圖添加刻度解決方案

編輯:關於JAVA

JFreeChart是老牌Java開源圖表工具包,也是JFreeReport的重要組成部分, 在業界被廣泛應用。當然,這個工具包依然有很多美中不足,確切的說,有很多 具體的應用還需要我們拓展,比如蜘蛛網圖(Spider Web)的刻度問題。

這是使用原來SpiderWebPlot時解決前的圖示:

下面是使用MySpiderWebPlot解決後的圖示:

MySpiderWebPlot代碼如下:

public class MySpiderWebPlot extends SpiderWebPlot {
     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 Rectangle2D labelBounds = getLabelFont().getStringBounds(label, frc);
                 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);
         }
     }
}

有其它需要的朋友可以參考這個思路繼續擴展。

遇到問題,不抱怨社會,不在一棵樹上吊死。

本文出自 “王傑瑞的技術博客” 博客,請務必保留此出處 http://wangjierui.blog.51cto.com/186879/116845

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved