JfreeChart 散點圖

jopen 10年前發布 | 38K 次閱讀 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 XYDataset createxydataset(List<PressureBean> xydatalist,  
  8.             String bloods) {  
  9.         DefaultXYDataset xydataset = new DefaultXYDataset();  
  10.   
  11.         int size = xydatalist.size();  
  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.             datas[0][i] = sys;  
  19.             datas[1][i] = dia;  
  20.         }  
  21.   
  22.         xydataset.addSeries(bloods, datas);  
  23.   
  24.         return xydataset;  
  25.   
  26.     }  

 

下一步,另外一個準備工作,畫圖方法:

  1. public static JFreeChart createChart(XYDataset xydataset,  
  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 (it.hasNext()) {  
  15.                 PressureBean pres = it.next();  
  16.                   
  17.                 if (maxpress < pres.getDiapress()) {  
  18.                     maxpress = pres.getDiapress();  
  19.                 }  
  20.   
  21.                 if (maxpress < pres.getSyspress()) {  
  22.                     maxpress = pres.getSyspress();  
  23.                 }  
  24.             }  
  25.   
  26.             maxpress += addmax;  
  27.   
  28.   
  29.             log.info("high press value is =" + maxpress);  
  30.   
  31.         }  
  32.   
  33.         JFreeChart jfreechart = ChartFactory.createScatterPlot(bloodcattile,  
  34.                 shou, shu, xydataset, PlotOrientation.VERTICAL, truefalse,  
  35.                 false);  
  36.         jfreechart.setBackgroundPaint(Color.white);  
  37.         jfreechart.setBorderPaint(Color.GREEN);  
  38.         jfreechart.setBorderStroke(new BasicStroke(1.5f));  
  39.         XYPlot xyplot = (XYPlot) jfreechart.getPlot();  
  40.         xyplot.setNoDataMessage(nobloodData);  
  41.         xyplot.setNoDataMessageFont(new Font("", Font.BOLD, 14));  
  42.         xyplot.setNoDataMessagePaint(new Color(87149117));  
  43.   
  44.         xyplot.setBackgroundPaint(new Color(255253246));  
  45.         ValueAxis vaaxis = xyplot.getDomainAxis();  
  46.         vaaxis.setAxisLineStroke(new BasicStroke(1.5f));  
  47.   
  48.         ValueAxis va = xyplot.getDomainAxis(0);  
  49.         va.setAxisLineStroke(new BasicStroke(1.5f));  
  50.   
  51.         va.setAxisLineStroke(new BasicStroke(1.5f)); // 坐標軸粗細  
  52.         va.setAxisLinePaint(new Color(215215215)); // 坐標軸顏色  
  53.         xyplot.setOutlineStroke(new BasicStroke(1.5f)); // 邊框粗細  
  54.         va.setLabelPaint(new Color(101010)); // 坐標軸標題顏色  
  55.         va.setTickLabelPaint(new Color(102102102)); // 坐標軸標尺值顏色  
  56.         ValueAxis axis = xyplot.getRangeAxis();  
  57.         axis.setAxisLineStroke(new BasicStroke(1.5f));  
  58.   
  59.         XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot  
  60.                 .getRenderer();  
  61.         xylineandshaperenderer.setSeriesOutlinePaint(0, Color.WHITE);  
  62.         xylineandshaperenderer.setUseOutlinePaint(true);  
  63.         NumberAxis numberaxis = (NumberAxis) xyplot.getDomainAxis();  
  64.         numberaxis.setAutoRangeIncludesZero(false);  
  65.         numberaxis.setTickMarkInsideLength(2.0F);  
  66.         numberaxis.setTickMarkOutsideLength(0.0F);  
  67.         numberaxis.setAxisLineStroke(new BasicStroke(1.5f));  
  68.         numberaxis.setUpperBound(maxpress);  
  69.         numberaxis.setLowerBound(60);//最小值設置為60  
  70.         NumberAxis numberaxis1 = (NumberAxis) xyplot.getRangeAxis();  
  71.         numberaxis1.setTickMarkInsideLength(2.0F);  
  72.         numberaxis1.setTickMarkOutsideLength(0.0F);  
  73.         numberaxis1.setUpperBound(105d);  
  74.         numberaxis1.setLowerBound(35);  
  75.         numberaxis1.setAxisLineStroke(new BasicStroke(1.5f));  
  76.   
  77.         // if (xydataset != null) {  
  78.         XYBoxAnnotation box = new XYBoxAnnotation(008959); //正常血壓所在區域內邊界  
  79.         XYBoxAnnotation box1 = new XYBoxAnnotation(0011979);//高血壓前期所在區域內邊界  
  80.         XYBoxAnnotation box2 = new XYBoxAnnotation(0013989);//高血壓一期所在區域內邊界  
  81.         XYBoxAnnotation box3 = new XYBoxAnnotation(0015999);//高血壓二期所在區域內邊界  
  82.         XYTextAnnotation text1 = new XYTextAnnotation(nomal, 7062.5);//標識“正常”  
  83.         XYTextAnnotation text = new XYTextAnnotation(fore, 7082.5);//“高血壓前期”  
  84.         XYTextAnnotation text2 = new XYTextAnnotation(one, 7091.5);//“高血壓一期”  
  85.         XYTextAnnotation text3 = new XYTextAnnotation(two, 70101.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 jfreechart;  
  100.     }  

 

最后一步,返回圖片URL

  1. public static void drawScatterChart(IrisIoInterface io, Log log,  
  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.         String filename = "";  
  11.         String graphURL = "";  
  12.         try {  
  13.             filename = ServletUtilities.saveChartAsPNG(chart, 400300null,  
  14.                     io.getSession());  
  15.             graphURL = request.getContextPath() + "/displayChart?filename="  
  16.                     + filename;  
  17.         } catch (IOException e) {  
  18.             // TODO Auto-generated catch block  
  19.             e.printStackTrace();  
  20.             log.error(e);  
  21.         }  
  22.   
  23.         io.setData("filename", filename, BeanShare.BEAN_SHARE_REQUEST);  
  24.         io.setData("scatterurl", graphURL, BeanShare.BEAN_SHARE_REQUEST);  
  25.   
  26.     }  

 

效果圖:

eb40e9b3-8ac2-3f4e-8f5e-e1f07e1685d8.png


來自:
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!