欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jfreechart学习笔记三 带刻度的蜘蛛图

程序员文章站 2022-03-05 12:34:29
...



 其实带刻度的蜘蛛图网上也有不少资料,我这个也是参照别人的资料来做的,不管咋样,还是记上吧。

 

先上图

 

jfreechart学习笔记三 带刻度的蜘蛛图
            
    
    博客分类: 各类工具java小技巧  

 

1、构造dataset

private DefaultCategoryDataset createDataSet2(Map<String, Map<String, Object>> resultMap, List<String> dimNameList) {
       DefaultCategoryDataset dataset = new DefaultCategoryDataset();//创建默认的种类数据类型就可以了,蜘蛛图的每个维度可以看成一种类型
       Set<String> keySet = resultMap.keySet();
       for(String key : keySet){
	Map<String,Object> infoMap = resultMap.get(key);
	String vendorCode = key.split("&")[0];
	String vendorName = key.split("&")[1];
	for(String dimName : dimNameList){
	        if(infoMap.get(dimName)==null){
		continue;
	        }
	       double score = (Double) infoMap.get(dimName);
	      dataset.addValue(score, vendorName.trim()      +"("+vendorCode.trim()+")", dimName);
	}
        }
        return dataset;
}

 

2、因为jfreechart默认的蜘蛛图内容区域对象是不带刻度的,所以我们扩展一个好了,写一个扩展类继承SpiderWebPlot,主要是重写drawLabel方法,这个网上很多地方都有。

public class SpiderWebPlotExpand 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;   
	    
	     
	   
	   public SpiderWebPlotExpand(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);   
	           }   
	   }   


}

 3、创建chart

private JFreeChart createChart2(DefaultCategoryDataset dataSet) {
	SpiderWebPlotExpand plot = new SpiderWebPlotExpand(dataSet);
	JFreeChart chart = new JFreeChart("维度得分分析", plot);	return chart;
}

 这次就比较简单了,因为网上例子不少,记在这里只是给自己以后查找方便,哈

 

  • jfreechart学习笔记三 带刻度的蜘蛛图
            
    
    博客分类: 各类工具java小技巧  
  • 大小: 27.9 KB