




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java JFreeChart柱状图单组柱子的不同颜色显示JFreeChart柱状图中单组柱子用不同颜色来显示的实现方法是自定义一个Renderer来继承BarRenderer类,然后重载getItemPaint(int i,int j)方法。实现效果如下:实现代码如下:htmlview plaincopy1. publicclassCustomRendererextendsorg.jfree.chart.renderer.category.BarRenderer2. 3. /*4. *5. */6. privatestaticfinallongserialVersionUID=784630226449158436L;7. privatePaintcolors;8. /初始化柱子颜色9. privateStringcolorValues=#AFD8F8,#F6BD0F,#8BBA00,#FF8E46,#008E8E,#D64646;10. 11. publicCustomRenderer()12. colors=newPaintcolorValues.length;13. for(inti=0;icolorValues.length;i+)14. colorsi=Color.decode(colorValuesi);15. 16. 17. 18. /每根柱子以初始化的颜色不断轮循19. publicPaintgetItemPaint(inti,intj)20. returncolorsj%colors.length;21. 22. htmlview plaincopy1. publicclassCreateJFreeChartBarColor2. 3. /*4. *创建JFreeChartBarChart(柱状图)5. */6. publicstaticvoidmain(Stringargs)7. /步骤1:创建CategoryDataset对象(准备数据)8. CategoryDatasetdataset=createDataset();9. /步骤2:根据Dataset生成JFreeChart对象,以及做相应的设置10. JFreeChartfreeChart=createChart(dataset);11. /步骤3:将JFreeChart对象输出到文件,Servlet输出流等12. saveAsFile(freeChart,E:bar.png,500,400);13. 14. 15. /保存为文件16. publicstaticvoidsaveAsFile(JFreeChartchart,StringoutputPath,intweight,intheight)17. FileOutputStreamout=null;18. try19. FileoutFile=newFile(outputPath);20. if(!outFile.getParentFile().exists()21. outFile.getParentFile().mkdirs();22. 23. out=newFileOutputStream(outputPath);24. /保存为PNG文件25. ChartUtilities.writeChartAsPNG(out,chart,weight,height);26. out.flush();27. catch(FileNotFoundExceptione)28. e.printStackTrace();29. catch(IOExceptione)30. e.printStackTrace();31. finally32. if(out!=null)33. try34. out.close();35. catch(IOExceptione)36. /donothing37. 38. 39. 40. 41. 42. /根据CategoryDataset生成JFreeChart对象43. publicstaticJFreeChartcreateChart(CategoryDatasetcategoryDataset)44. JFreeChartjfreechart=ChartFactory.createBarChart(学生统计图,/标题45. 学生姓名,/categoryAxisLabel(category轴,横轴,X轴的标签)46. 年龄,/valueAxisLabel(value轴,纵轴,Y轴的标签)47. categoryDataset,/dataset48. PlotOrientation.VERTICAL,false,/legend49. false,/tooltips50. false);/URLs51. 52. FontlabelFont=newFont(SansSerif,Font.TRUETYPE_FONT,12);53. 54. jfreechart.setTextAntiAlias(false);55. jfreechart.setBackgroundPaint(Color.white);56. 57. CategoryPlotplot=jfreechart.getCategoryPlot();/获得图表区域对象58. 59. /设置横虚线可见60. plot.setRangeGridlinesVisible(true);61. /虚线色彩62. plot.setRangeGridlinePaint(Color.gray);63. /数据轴精度64. NumberAxisvn=(NumberAxis)plot.getRangeAxis();65. /vn.setAutoRangeIncludesZero(true);66. DecimalFormatdf=newDecimalFormat(#0.0);67. vn.setNumberFormatOverride(df);/数据轴数据标签的显示格式68. 69. /x轴设置70. CategoryAxisdomainAxis=plot.getDomainAxis();71. domainAxis.setLabelFont(labelFont);/轴标题72. domainAxis.setTickLabelFont(labelFont);/轴数值73. /Lable(Math.PI/3.0)度倾斜74. /domainAxis.setCategoryLabelPositions(CategoryLabelPositions75. /.createUpRotationLabelPositions(Math.PI/3.0);76. domainAxis.setMaximumCategoryLabelWidthRatio(6.00f);/横轴上的Lable77. /是否完整显示78. 79. /设置距离图片左端距离80. domainAxis.setLowerMargin(0.1);81. /设置距离图片右端距离82. domainAxis.setUpperMargin(0.1);83. /设置columnKey是否间隔显示84. /domainAxis.setSkipCategoryLabelsToFit(true);85. plot.setDomainAxis(domainAxis);86. /设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确)87. plot.setBackgroundPaint(newColor(255,255,204);88. 89. /y轴设置90. ValueAxisrangeAxis=plot.getRangeAxis();91. rangeAxis.setLabelFont(labelFont);92. rangeAxis.setTickLabelFont(labelFont);93. /设置最高的一个Item与图片顶端的距离94. rangeAxis.setUpperMargin(0.15);95. /设置最低的一个Item与图片底端的距离96. rangeAxis.setLowerMargin(0.15);97. plot.setRangeAxis(rangeAxis);98. 99. /解决中文乱码问题(关键)100. TextTitletextTitle=jfreechart.getTitle();101. textTitle.setFont(newFont(黑体,Font.PLAIN,20);102. domainAxis.setTickLabelFont(newFont(sans-serif,Font.PLAIN,11);103. domainAxis.setLabelFont(newFont(宋体,Font.PLAIN,12);104. vn.setTickLabelFont(newFont(sans-serif,Font.PLAIN,12);105. vn.setLabelFont(newFont(黑体,Font.PLAIN,12);106. /jfreechart.getLegend().setItemFont(newFont(宋体,Font.PLAIN,12);107. 108. /使用自定义的渲染器109. CustomRendererrenderer=newCustomRenderer();110. /设置柱子宽度111. renderer.setMaximumBarWidth(0.2);112. /设置柱子高度113. renderer.setMinimumBarLength(0.2);114. /设置柱子边框颜色115. renderer.setBaseOutlinePaint(Color.BLACK);116. /设置柱子边框可见117. renderer.setDrawBarOutline(true);118. /设置每个地区所包含的平行柱的之间距离119. renderer.setItemMargin(0.5);120. jfreechart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);121. /显示每个柱的数值,并修改该数值的字体属性122. renderer.setIncludeBaseInRange(true);123. renderer.setBaseItemLabelGenerator(newStandardCategoryItemLabelGenerator();124. renderer.setBaseItemLabelsVisible(true);125. plot.setRenderer(renderer);126. /设置柱的透明度127. plot.setForegroundAlpha(1.0f);128. 129. /背景色透明度130. plot.setBackgroundAlpha(0.5f);131. 132. returnjfreechart;133. 134. 135. /创建CategoryDataset对象136. publicstaticCategoryD
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 深入了解专利代理人考试试题及答案
- 育婴师情感沟通与亲子联系试题及答案
- 系统规划与管理师考试重点评估试题及答案
- 自我防护措施在临床中的必要性试题及答案
- 网络规划设计师复习规划设计试题及答案
- 编程语言构造与使用方法试题及答案
- 激光设备应用案例试题及答案
- 2025年河南省安全员A证考试题库附答案
- 卫生管理相关证书考试试题及答案解析
- 育婴师心理支持技巧试题及答案
- 苏教版数学三年级下册期中考试试卷及答案
- 山东省自然科学基金申报书-青年基金、面上项目
- 手术室静脉输液课件
- 资金支付计划审批表
- 媒体行业社会责任现状研究
- 英语-第一册-第三版-Unit5
- 读书分享平凡的世界
- 2024年山东济南中考语文作文分析-为了这份繁华
- 医院案例剖析之武汉协和医院:护理人文关怀规范化实践管理体系的构建与应用
- 帕金森病药物治疗 课件
- 2024年医院依法执业培训课件
评论
0/150
提交评论