0
点赞
收藏
分享

微信扫一扫

JfreeChart 散点图


 


用JfreeChart画散点图

 首先,看画图的API,参数有:

ChartFactory.createScatterPlot(),其中,有一个xydataset,那么,我们先要知道这个xydataset是什么结构的,再看所需xydataset,散点图,也就是单独画出点,也就是一个二维数据了,x ,y坐标嘛!

那么,先做好准备工作,第一步,拿数据,这就不用啰嗦了,就是得到一个List也好,Set也行。

 

第二步,加载到数据集:


1. /**
2. *
3. * @param xydatalist
4. * @param bloods
5. * @return
6. */
7. public static
8. String bloods) {
9. new
10.
11. int
12. double[][] datas = new double[2][size];
13. for (int i = 0; i < size; i++) {
14. PressureBean pres = xydatalist.get(i);
15. int sys = pres.getSyspress();//收缩压
16. int dia = pres.getDiapress();//舒张压
17.
18. 0][i] = sys;
19. 1][i] = dia;
20. }
21.
22. xydataset.addSeries(bloods, datas);
23.
24. return
25.
26. }


 

下一步,另外一个准备工作,画图方法:

1. public static
2. String bloodcattile, String shou, String shu, String nobloodData,
3. String bloods, String nomal, String fore, String one, String two,
4. List<PressureBean> list, Log log) {
5.
6. // 有可能用户在后面的版本中故意输入不正常数值,但是为了保证图片画图的完整,这里先计算
7. // 用户血压值的最大值。
8. int maxpress = 160;
9. int addmax = 20;
10.
11. if (list != null && list.size() > 0) {
12.
13. Iterator<PressureBean> it = list.iterator();
14. while
15. PressureBean pres = it.next();
16.
17. if
18. maxpress = pres.getDiapress();
19. }
20.
21. if
22. maxpress = pres.getSyspress();
23. }
24. }
25.
26. maxpress += addmax;
27.
28.
29. "high press value is ="
30.
31. }
32.
33. JFreeChart jfreechart = ChartFactory.createScatterPlot(bloodcattile,
34. true, false,
35. false);
36. jfreechart.setBackgroundPaint(Color.white);
37. jfreechart.setBorderPaint(Color.GREEN);
38. new BasicStroke(1.5f));
39. XYPlot xyplot = (XYPlot) jfreechart.getPlot();
40. xyplot.setNoDataMessage(nobloodData);
41. new Font("", Font.BOLD, 14));
42. new Color(87, 149, 117));
43.
44. new Color(255, 253, 246));
45. ValueAxis vaaxis = xyplot.getDomainAxis();
46. new BasicStroke(1.5f));
47.
48. 0);
49. new BasicStroke(1.5f));
50.
51. new BasicStroke(1.5f)); // 坐标轴粗细
52. new Color(215, 215, 215)); // 坐标轴颜色
53. new BasicStroke(1.5f)); // 边框粗细
54. new Color(10, 10, 10)); // 坐标轴标题颜色
55. new Color(102, 102, 102)); // 坐标轴标尺值颜色
56. ValueAxis axis = xyplot.getRangeAxis();
57. new BasicStroke(1.5f));
58.
59. XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot
60. .getRenderer();
61. 0, Color.WHITE);
62. true);
63. NumberAxis numberaxis = (NumberAxis) xyplot.getDomainAxis();
64. false);
65. 2.0F);
66. 0.0F);
67. new BasicStroke(1.5f));
68. numberaxis.setUpperBound(maxpress);
69. 60);//最小值设置为60
70. NumberAxis numberaxis1 = (NumberAxis) xyplot.getRangeAxis();
71. 2.0F);
72. 0.0F);
73. numberaxis1.setUpperBound(105d);
74. 35);
75. new BasicStroke(1.5f));
76.
77. // if (xydataset != null) {
78. new XYBoxAnnotation(0, 0, 89, 59); //正常血压所在区域内边界
79. new XYBoxAnnotation(0, 0, 119, 79);//高血压前期所在区域内边界
80. new XYBoxAnnotation(0, 0, 139, 89);//高血压一期所在区域内边界
81. new XYBoxAnnotation(0, 0, 159, 99);//高血压二期所在区域内边界
82. new XYTextAnnotation(nomal, 70, 62.5);//标识“正常”
83. new XYTextAnnotation(fore, 70, 82.5);//“高血压前期”
84. new XYTextAnnotation(one, 70, 91.5);//“高血压一期”
85. new XYTextAnnotation(two, 70, 101.5);//“高血压二期”
86.
87.
88. //将上面的边界线条,说明文字加入到xyplot中。
89. xyplot.addAnnotation(box);
90. xyplot.addAnnotation(box1);
91. xyplot.addAnnotation(box2);
92. xyplot.addAnnotation(box3);
93.
94. xyplot.addAnnotation(text);
95. xyplot.addAnnotation(text1);
96. xyplot.addAnnotation(text2);
97. xyplot.addAnnotation(text3);
98. // }
99. return
100. }


 

最后一步,返回图片URL


1. public static void
2. XYDataset xydataSet, String title, String shou, String shu,
3. String nodata, String boolds, String nomal, String fore,
4. String one, String two, List<PressureBean> list) {
5.
6. JFreeChart chart = createChart(xydataSet, title, shou, shu, nodata,
7. boolds, nomal, fore, one, two, list, log);
8.
9. HttpServletRequest request = io.getRequest();
10. "";
11. "";
12. try
13. 400, 300, null,
14. io.getSession());
15. "/displayChart?filename="
16. + filename;
17. catch
18. // TODO Auto-generated catch block
19. e.printStackTrace();
20. log.error(e);
21. }
22.
23. "filename", filename, BeanShare.BEAN_SHARE_REQUEST);
24. "scatterurl", graphURL, BeanShare.BEAN_SHARE_REQUEST);
25.
26. }


 

效果图:



举报

相关推荐

0 条评论