用jfreechart画一个象限图,因为在jfreechart的例子中并没有象限图的举例,我怀疑他没有此类图的chart类型,所以处理会比较特殊一些
先上图,我要画的就是下面这个图,因为只是个举例,所以数据造的比较极端,都在最顶上
那就正式开始
1、先用数据构造一个dataset
/** * 构造数据集合 * @param resultMap * @return */ private XYDataset createDataSet( Map<String, Map<String, Object>> resultMap,double minScore, double midScore, double maxScore, double minAmount, double midAmount, double maxAmount) { Set<String> keySet = resultMap.keySet();//传递过来的数据 XYSeriesCollection xyseriescollection = new XYSeriesCollection();//先构造一个jfreechart数据类型的集合,这个集合是用来放XYSeries类型数据的 xyseriescollection.setIntervalWidth(4.0D);//设置数据的区域宽度 xyseriescollection.getDomainBounds(true);//设置边界的时候计算x方向的区域宽度 for(String key : keySet){ Map<String, Object> dataMap = resultMap.get(key); String label = dataMap.get("vendorName").toString().trim()+"("+key.trim()+")"; XYSeries xyseries = new XYSeries(label);//创建一个数据点 double x = (Double)dataMap.get("score");//数据的X值 double y = (Double)dataMap.get("amount");//数据的Y值 //如果数据点是顶点位置,为了防止看不清楚,将数据点的位置调整进入数据区域 if(x==maxScore){ x = x-1d; }else if(x==minScore){ x = x+1d; } if(y==maxAmount){ y = y-1d; }else if(y==minAmount){ y = y+1d; } xyseries.add(x, y); xyseries.setDescription((Double)dataMap.get("amount")+":"+(Double)dataMap.get("score")); xyseriescollection.addSeries(xyseries); } return xyseriescollection;//返回此数据集合 }
2、构造chart
/** * 构造采购策略图 * @return */ private JFreeChart createAmountChart(XYDataset dataSet, double minScore, double midScore, double maxScore, double minAmount, double midAmount, double maxAmount) { //因为象限图比较特殊,所以不采用jfreechart提供的chart类型,创建一个最基本的chart,剩下的都靠我们自己来处理了 //创建数据区域对象 XYPlot plot = new XYPlot(); plot.setOrientation(PlotOrientation.VERTICAL); plot.setForegroundAlpha(0.5f); //数据区的设置 plot.setNoDataMessage("没有数据"); plot.setDomainPannable(true); plot.setRangePannable(true); plot.setDomainZeroBaselineVisible(true); plot.setRangeZeroBaselineVisible(true); //将x轴方向的网格线设置为0 plot.setDomainGridlineStroke(new BasicStroke(0.0f)); plot.setDomainMinorGridlineStroke(new BasicStroke(0.0f)); plot.setDomainGridlinePaint(Color.red);//设置x轴的网格线的颜色,我这里因为设置没网格线了,所以没有用处 //设置Y轴的网格线 plot.setRangeGridlineStroke(new BasicStroke(0.0f)); plot.setRangeMinorGridlineStroke(new BasicStroke(0.0f)); plot.setRangeGridlinePaint(Color.blue); plot.setDomainGridlinesVisible(false); plot.setRangeGridlinesVisible(false); plot.setBackgroundPaint(Color.WHITE);//设置数据区域的背景色 plot.setOutlineVisible(false);//是否显示数据区域的外边框 plot.setDataset(dataSet);//给数据区域设置展示数据 //创建XY轴对象,并进行设置 NumberAxis xAxis = new NumberAxis("评分"); xAxis.setAutoRangeIncludesZero(false); xAxis.setAutoTickUnitSelection(false); xAxis.setTickUnit(new NumberTickUnit(5.0)); xAxis.setTickMarkInsideLength(2.0F); xAxis.setTickMarkOutsideLength(2.0F); xAxis.setMinorTickCount(1); xAxis.setMinorTickMarksVisible(true); xAxis.setRange(minScore, maxScore); NumberAxis yAxis = new NumberAxis("采购量"); yAxis.setAutoRangeIncludesZero(false); yAxis.setAutoTickUnitSelection(false); yAxis.setTickUnit(new NumberTickUnit(5.0)); yAxis.setTickMarkInsideLength(2.0F); yAxis.setTickMarkOutsideLength(2.0F); yAxis.setMinorTickCount(1); yAxis.setMinorTickMarksVisible(true); yAxis.setRange(minAmount, maxAmount); plot.setDomainAxis(xAxis);//数据区域加入X轴 plot.setRangeAxis(yAxis);//数据区域加入Y轴 //设置标记,因为象限图需要把数据区分成四个区域,所以采用标记的方式来做这个处理 //先设置X轴上的标记,也就是跟Y轴平行的那根线 ValueMarker makers = new ValueMarker(midScore); makers.setPaint(Color.BLACK); plot.addDomainMarker(makers,Layer.FOREGROUND); //再设置Y轴上的标记,跟X轴平行的线 makers = new ValueMarker(midAmount); makers.setPaint(Color.BLACK); plot.addRangeMarker(makers,Layer.FOREGROUND); //上面X,Y轴的Marker设置完,就能在数据区域内出现“十”字形的两根线将数据区域分成四块了 //数据区域分成四块后,那么要用不同的背景来区分,而且公司希望好看点,最好有渐变效果,那么就要做一些特殊处理 XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer(false,true);//创建一个渲染器 GradientPaint paint = new GradientPaint(0, 0, new Color(255, 195, 035), 0,250 , Color.white);//创建一个渐变模式的Paint对象 XYPolygonAnnotation xypolygonannotation = new XYPolygonAnnotation( new double[] { midScore,midAmount,midScore, maxAmount,maxScore, maxAmount,maxScore, midAmount }, new BasicStroke(5f), Color.white, paint);//创建一块区域的注解 xylineandshaperenderer.addAnnotation(xypolygonannotation, Layer.BACKGROUND);//渲染器加入注解块 //同上,因为是四个象限,所以需要加入四个注解块 paint = new GradientPaint(0, 0, new Color(99, 173, 255), -50,500 , Color.white); xypolygonannotation = new XYPolygonAnnotation( new double[] { midScore,minAmount,midScore, midAmount,maxScore, midAmount,maxScore, minAmount }, new BasicStroke(5f), Color.white, paint); xylineandshaperenderer.addAnnotation(xypolygonannotation, Layer.BACKGROUND); paint = new GradientPaint(0, 0, new Color(99, 173, 255), -50,500 , Color.white); xypolygonannotation = new XYPolygonAnnotation( new double[] { midScore,minAmount,midScore, midAmount,maxScore, midAmount,maxScore, minAmount }, new BasicStroke(5f), Color.white, paint); xylineandshaperenderer.addAnnotation(xypolygonannotation, Layer.BACKGROUND); paint = new GradientPaint(0, 0, new Color(99, 173, 255), -300,500 , Color.white); xypolygonannotation = new XYPolygonAnnotation( new double[] { minScore,minAmount,minScore, midAmount,midScore, midAmount,midScore, minAmount }, new BasicStroke(5f), Color.white, paint); xylineandshaperenderer.addAnnotation(xypolygonannotation, Layer.BACKGROUND); paint = new GradientPaint(0, 0, new Color(255, 102, 101), -50,250 , Color.white); xypolygonannotation = new XYPolygonAnnotation( new double[] { minScore,midAmount,minScore, maxAmount,midScore, maxAmount,midScore, midAmount }, new BasicStroke(5f), Color.white, paint); xylineandshaperenderer.addAnnotation(xypolygonannotation, Layer.BACKGROUND); XYSeriesCollection xySeries = (XYSeriesCollection) dataSet; List<XYSeries> xySeriesList = xySeries.getSeries(); for(XYSeries series : xySeriesList){ XYPointerAnnotation xypointerannotation = new XYPointerAnnotation( (String) series.getKey(), series.getX(0).doubleValue(), series.getY(0).doubleValue(), 2.3561944901923448D); xypointerannotation.setBaseRadius(35D); xypointerannotation.setTipRadius(10D); xypointerannotation.setFont(new Font("SansSerif", 0, 9)); xypointerannotation.setPaint(Color.BLACK); xypointerannotation.setTextAnchor(TextAnchor.HALF_ASCENT_RIGHT); plot.addAnnotation(xypointerannotation); } xylineandshaperenderer.setBaseItemLabelsVisible(true); xylineandshaperenderer.setAutoPopulateSeriesShape(false); xylineandshaperenderer.setAutoPopulateSeriesOutlinePaint(false); xylineandshaperenderer.setAutoPopulateSeriesOutlineStroke(false); xylineandshaperenderer.setBaseShape(new Ellipse2D.Double(-3D, -3D, 8D, 8D));//渲染器设置数据的展现形状,这里采用圆形 xylineandshaperenderer.setBaseOutlinePaint(Color.WHITE); xylineandshaperenderer.setBaseOutlineStroke(new BasicStroke(5.0f)); plot.setRenderer(xylineandshaperenderer);//数据区域采用此渲染器 //因为chart构造器的原因,将chart放在最后进行创建 JFreeChart chart = new JFreeChart("采购额策略分析",plot); return chart; }
三、生成图片
ChartUtilities.saveChartAsJPEG(img, chart, 500, 500);
一句话即可,当然,在此之前先创建img文件对象。
到此,我需要的象限图就完成了,不过还有个小毛病,在最底下指向数据的提示不见了,这是因为它一般放在数据下面,但是我这个数据是个极端数据,所以这个提示超过了数据区域的范围,也就看不见了,不知道大家有木有好的解决方法。先到这里