JFreeChart最佳實(shí)踐:散點(diǎn)圖
用JfreeChart畫散點(diǎn)圖,查看JfreeChart的Demo,寫的都挺復(fù)雜的,關(guān)鍵是Demo中把簡(jiǎn)單的事情復(fù)雜化了,比如展示的例子是一個(gè)正弦曲線什么的,讓初次畫散點(diǎn)圖的我們摸不著頭腦。關(guān)鍵是他們得到數(shù)據(jù)集搞得太過復(fù)雜,后來想明白了,不就是二維數(shù)組嘛。想通了這一點(diǎn),一切問題都解決了,不過,對(duì)于我們項(xiàng)目的特殊要求,并不是只畫幾個(gè)點(diǎn)那么簡(jiǎn)單,還要加上區(qū)域范圍與文字說明,在查看文檔及自己摸索下,2天時(shí)間,終于搞定。下面分享一下成果,呵,還是有點(diǎn)成就感的。
首先,看畫圖的API,參數(shù)有:
ChartFactory.createScatterPlot(),其中,有一個(gè)xydataset,那么,我們先要知道這個(gè)xydataset是什么結(jié)構(gòu)的,再看所需xydataset,散點(diǎn)圖,也就是單獨(dú)畫出點(diǎn),也就是一個(gè)二維數(shù)據(jù)了,x ,y坐標(biāo)嘛!
那么,先做好準(zhǔn)備工作,***步,拿數(shù)據(jù),這就不用啰嗦了,就是得到一個(gè)List也好,Set也行。
第二步,加載到數(shù)據(jù)集:
- /**
- *
- * @param xydatalist
- * @param bloods
- * @return
- */
- public static XYDataset createxydataset(List<PressureBean> xydatalist,
- String bloods) {
- DefaultXYDataset xydataset = new DefaultXYDataset();
- int size = xydatalist.size();
- double[][] datas = new double[2][size];
- for (int i = 0; i < size; i++) {
- PressureBean pres = xydatalist.get(i);
- int sys = pres.getSyspress();//收縮壓
- int dia = pres.getDiapress();//舒張壓
- datas[0][i] = sys;
- datas[1][i] = dia;
- }
- xydataset.addSeries(bloods, datas);
- return xydataset;
- }
下一步,另外一個(gè)準(zhǔn)備工作,畫圖方法:
- public static JFreeChart createChart(XYDataset xydataset,
- String bloodcattile, String shou, String shu, String nobloodData,
- String bloods, String nomal, String fore, String one, String two,
- List<PressureBean> list, Log log) {
- // 有可能用戶在后面的版本中故意輸入不正常數(shù)值,但是為了保證圖片畫圖的完整,這里先計(jì)算
- // 用戶血壓值的***值。
- int maxpress = 160;
- int addmax = 20;
- if (list != null && list.size() > 0) {
- Iterator<PressureBean> it = list.iterator();
- while (it.hasNext()) {
- PressureBean pres = it.next();
- if (maxpress < pres.getDiapress()) {
- maxpress = pres.getDiapress();
- }
- if (maxpress < pres.getSyspress()) {
- maxpress = pres.getSyspress();
- }
- }
- maxpress += addmax;
- log.info("high press value is =" + maxpress);
- }
- JFreeChart jfreechart = ChartFactory.createScatterPlot(bloodcattile,
- shou, shu, xydataset, PlotOrientation.VERTICAL, true, false,
- false);
- jfreechart.setBackgroundPaint(Color.white);
- jfreechart.setBorderPaint(Color.GREEN);
- jfreechart.setBorderStroke(new BasicStroke(1.5f));
- XYPlot xyplot = (XYPlot) jfreechart.getPlot();
- xyplot.setNoDataMessage(nobloodData);
- xyplot.setNoDataMessageFont(new Font("", Font.BOLD, 14));
- xyplot.setNoDataMessagePaint(new Color(87, 149, 117));
- xyplot.setBackgroundPaint(new Color(255, 253, 246));
- ValueAxis vaaxis = xyplot.getDomainAxis();
- vaaxis.setAxisLineStroke(new BasicStroke(1.5f));
- ValueAxis va = xyplot.getDomainAxis(0);
- va.setAxisLineStroke(new BasicStroke(1.5f));
- va.setAxisLineStroke(new BasicStroke(1.5f)); // 坐標(biāo)軸粗細(xì)
- va.setAxisLinePaint(new Color(215, 215, 215)); // 坐標(biāo)軸顏色
- xyplot.setOutlineStroke(new BasicStroke(1.5f)); // 邊框粗細(xì)
- va.setLabelPaint(new Color(10, 10, 10)); // 坐標(biāo)軸標(biāo)題顏色
- va.setTickLabelPaint(new Color(102, 102, 102)); // 坐標(biāo)軸標(biāo)尺值顏色
- ValueAxis axis = xyplot.getRangeAxis();
- axis.setAxisLineStroke(new BasicStroke(1.5f));
- XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot
- .getRenderer();
- xylineandshaperenderer.setSeriesOutlinePaint(0, Color.WHITE);
- xylineandshaperenderer.setUseOutlinePaint(true);
- NumberAxis numberaxis = (NumberAxis) xyplot.getDomainAxis();
- numberaxis.setAutoRangeIncludesZero(false);
- numberaxis.setTickMarkInsideLength(2.0F);
- numberaxis.setTickMarkOutsideLength(0.0F);
- numberaxis.setAxisLineStroke(new BasicStroke(1.5f));
- numberaxis.setUpperBound(maxpress);
- numberaxis.setLowerBound(60);//最小值設(shè)置為60
- NumberAxis numberaxis1 = (NumberAxis) xyplot.getRangeAxis();
- numberaxis1.setTickMarkInsideLength(2.0F);
- numberaxis1.setTickMarkOutsideLength(0.0F);
- numberaxis1.setUpperBound(105d);
- numberaxis1.setLowerBound(35);
- numberaxis1.setAxisLineStroke(new BasicStroke(1.5f));
- // if (xydataset != null) {
- XYBoxAnnotation box = new XYBoxAnnotation(0, 0, 89, 59); //正常血壓所在區(qū)域內(nèi)邊界
- XYBoxAnnotation box1 = new XYBoxAnnotation(0, 0, 119, 79);//高血壓前期所在區(qū)域內(nèi)邊界
- XYBoxAnnotation box2 = new XYBoxAnnotation(0, 0, 139, 89);//高血壓一期所在區(qū)域內(nèi)邊界
- XYBoxAnnotation box3 = new XYBoxAnnotation(0, 0, 159, 99);//高血壓二期所在區(qū)域內(nèi)邊界
- XYTextAnnotation text1 = new XYTextAnnotation(nomal, 70, 62.5);//標(biāo)識(shí)“正常”
- XYTextAnnotation text = new XYTextAnnotation(fore, 70, 82.5);//“高血壓前期”
- XYTextAnnotation text2 = new XYTextAnnotation(one, 70, 91.5);//“高血壓一期”
- XYTextAnnotation text3 = new XYTextAnnotation(two, 70, 101.5);//“高血壓二期”
- //將上面的邊界線條,說明文字加入到xyplot中。
- xyplot.addAnnotation(box);
- xyplot.addAnnotation(box1);
- xyplot.addAnnotation(box2);
- xyplot.addAnnotation(box3);
- xyplot.addAnnotation(text);
- xyplot.addAnnotation(text1);
- xyplot.addAnnotation(text2);
- xyplot.addAnnotation(text3);
- // }
- return jfreechart;
- }
***一步,返回圖片URL
- public static void drawScatterChart(IrisIoInterface io, Log log,
- XYDataset xydataSet, String title, String shou, String shu,
- String nodata, String boolds, String nomal, String fore,
- String one, String two, List<PressureBean> list) {
- JFreeChart chart = createChart(xydataSet, title, shou, shu, nodata,
- boolds, nomal, fore, one, two, list, log);
- HttpServletRequest request = io.getRequest();
- String filename = "";
- String graphURL = "";
- try {
- filename = ServletUtilities.saveChartAsPNG(chart, 400, 300, null,
- io.getSession());
- graphURL = request.getContextPath() + "/displayChart?filename="
- + filename;
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- log.error(e);
- }
- io.setData("filename", filename, BeanShare.BEAN_SHARE_REQUEST);
- io.setData("scatterurl", graphURL, BeanShare.BEAN_SHARE_REQUEST);
- }
效果圖:
原文鏈接:http://juliana-only.iteye.com/blog/544104
【編輯推薦】