JfreeChart 散點圖
用JfreeChart畫散點圖
首先,看畫圖的API,參數有:
ChartFactory.createScatterPlot(),其中,有一個xydataset,那么,我們先要知道這個xydataset是什么結構的,再看所需xydataset,散點圖,也就是單獨畫出點,也就是一個二維數據了,x ,y坐標嘛!
那么,先做好準備工作,第一步,拿數據,這就不用啰嗦了,就是得到一個List也好,Set也行。
第二步,加載到數據集:
- /**
- *
- * @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;
- }
下一步,另外一個準備工作,畫圖方法:
- 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) {
- // 有可能用戶在后面的版本中故意輸入不正常數值,但是為了保證圖片畫圖的完整,這里先計算
- // 用戶血壓值的最大值。
- 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)); // 坐標軸粗細
- va.setAxisLinePaint(new Color(215, 215, 215)); // 坐標軸顏色
- xyplot.setOutlineStroke(new BasicStroke(1.5f)); // 邊框粗細
- va.setLabelPaint(new Color(10, 10, 10)); // 坐標軸標題顏色
- va.setTickLabelPaint(new Color(102, 102, 102)); // 坐標軸標尺值顏色
- 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);//最小值設置為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); //正常血壓所在區域內邊界
- XYBoxAnnotation box1 = new XYBoxAnnotation(0, 0, 119, 79);//高血壓前期所在區域內邊界
- XYBoxAnnotation box2 = new XYBoxAnnotation(0, 0, 139, 89);//高血壓一期所在區域內邊界
- XYBoxAnnotation box3 = new XYBoxAnnotation(0, 0, 159, 99);//高血壓二期所在區域內邊界
- XYTextAnnotation text1 = new XYTextAnnotation(nomal, 70, 62.5);//標識“正常”
- 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);
- }
效果圖:
來自:
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!