- 浏览: 135823 次
- 性别:
- 来自: 无锡
文章分类
最新评论
-
yh443042575:
楼主造福社会呀,顶顶顶!!!
jfreechart学习笔记三 带刻度的蜘蛛图 -
twtja1:
最后一句"如果需要在父类中仍然要使用类型字段,需要在 ...
hibernate继承之一,单表继承映射 -
qq361837213:
更换新版dwr的jar包就行了,新版本解决了这个问题
dwr文件上传在chrome下的问题及解决方法 -
无象心:
无象心 写道改为这样就可以了
loadingComple ...
dwr文件上传在chrome下的问题及解决方法 -
无象心:
loadingComplete:function(ba ...
dwr文件上传在chrome下的问题及解决方法
用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文件对象。
到此,我需要的象限图就完成了,不过还有个小毛病,在最底下指向数据的提示不见了,这是因为它一般放在数据下面,但是我这个数据是个极端数据,所以这个提示超过了数据区域的范围,也就看不见了,不知道大家有木有好的解决方法。先到这里
发表评论
-
linux下java8 启动spring-boot包
2018-05-09 09:00 0java8如何管理spring-boot的jar包 -
tomcat部署多个项目注意点
2016-11-22 21:45 01、启动老是报jdbc.driver的错。把jar都从 ... -
文件下载和浏览器直接打开
2016-07-09 10:03 0content-disposition=attac ... -
JSP文件下载
2015-11-02 16:03 663JSP工具之,文件下载 <% String fi ... -
maven学习笔记七 仓库
2014-07-03 23:37 0构件的仓库 仓库主要是在构建过程中处理构件和依赖包。 ... -
maven学习笔记六 构建配置
2014-07-02 22:22 0maven一直致力于使构建 ... -
maven学习笔记五 POM
2014-06-26 00:27 0POM project object model项目对象模 ... -
Maven学习笔记四 生命周期
2014-06-24 23:41 0Maven的项目管理和构建是基于生命周期这个概念的。 这意 ... -
maven3读书笔记 第一个maven工程
2014-06-17 10:06 0在命令行中输入: mvn archetype:gener ... -
关于南车项目的一些技术性总结
2013-11-06 10:15 0写道 set JAVA_OPTS=-Xms512m ... -
php5.5 win7 64位系统安装配置
2013-07-23 21:04 0本人系统是win7 64 位所以需要安装64位的php ... -
Hibernate middleGen工具使用
2013-07-16 23:01 0本文所用数据库版本为MySQL5.0.15、Oracle10 ... -
springAOP后对象代理和hibernate不识别问题
2013-07-16 23:03 983场景:自定义注解,AOP拦截,处理前置业务。然后从spri ... -
tomcat7内存溢出错误的一些原因和解决方案
2013-01-11 19:42 10925今天在做公司一个工程2个版本的运行时,突然出现新版的工程在t ... -
在struts2中整合sitemesh
2013-01-10 21:56 2428今天在做个整合时,决定用sitemesh把几个页面的布局整理下 ... -
maven3读书笔记一 认识maven
2013-01-08 18:35 0maven是什么,简单来说,它是一个构建和管理你工程的一个工 ... -
分享两个数据库转换小工具,挺有用的
2013-01-08 17:07 883一个是oracle转mysql的 另一个是mysql转ora ... -
jfreechart学习笔记三 带刻度的蜘蛛图
2013-01-08 16:58 3150其实带刻度的蜘蛛图网上也有不少资料,我这个也是参照别人的资料 ... -
jfreechart使用笔记一 基本知识
2013-01-07 19:20 1576公司要做一些图表,各 ... -
maven3读书笔记2 的安装和设置
2012-11-25 21:54 0一、安装 1、下载maven 地址:http://mav ...
相关推荐
比较适用的JfreeChart 学习笔记
NULL 博文链接:https://langhua9527.iteye.com/blog/395327
jfreeChart学习积累。新手看即可。
JfreeChart学习总结JfreeChart学习总结
Struts 学习笔记 Struts 中扩展JfreeChart 笔记 很详细 很实例 的 对 Jfreechart 描述的很好
NULL 博文链接:https://langhua9527.iteye.com/blog/395194
NULL 博文链接:https://langhua9527.iteye.com/blog/395244
JFreeChart学习资料.doc 中文帮助文档
结合使用心得,详细描述了JFreeChart的构建,主要介绍如何绘制基本图,混合图和动态时序图。
JFreeChart学习 比较详细 希望大家共同进步
jFreeChart学习总结
博文链接:https://xkspace.iteye.com/blog/138314
jfreechart实例教程+笔记+流程图
JFreeChart学习资料大全JFreeChart学习资料大全
JFreeChart双Y轴折线图实例,可以直接运行,实例类为LineChartDemo1.JAVA,有注释。 若想在web工程使用只需如下。 String filename = ServletUtilities.saveChartAsPNG(jfreechart, 600, 400, null, session); ...
jfreechart 堆栈柱状图跟折线图结合拼接 jar包请戳http://download.csdn.net/detail/a156435646/7424707
JFreeChart 饼状图 柱状图 柱状图 堆积柱状图 折线图 这个是我特地开发的一个用JFreeChart开发的用来画图的项目,已经开发成形了各种项目的各种需求的项目,并且自己可以拓展,根据需求修改代码,完成项目的需要。 ...
public JFreeChart createChart(String Ytitle, String title, CategoryDataset lineData, CategoryDataset barData) { //参考附件 return chart; } //struts 部分 <!--begin 维优特例 --> ...