您好,登錄后才能下訂單哦!
小編給大家分享一下如何使用JFreeChart實現折線圖,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
折線圖,大可分為兩種,
(1)X軸值類型為String的。
2)常用的是X軸值是日期的,并且,有時需要滿足這樣的需求:
1、時間要連續。
2、時間可以設置固定的跨度,比如,2009-02-01,2009-02-04,2009-02-07……
3、由于時間跨度較大,想要做到不同精度的圖表,如時間為10天時,以日(yyyy-MM-dd)格式為精度,時間跨度為2個月時,以周(如2009年第3周)為精度,跨度為6個月時,以月(2009年8月)為精度。
下面,針對比較復雜的(2)來講解:
1、取到業務邏輯需要的數據:(具體過程就不啰嗦了,就是查詢數據庫,得到想要的字段的值,加載到List里面) 返回List<PressureBean>
PressureBean的包含的屬性:
int userId; String bpDate; String bpTime; int syspress; //收縮壓(mmHg) int diapress; //舒張壓(mmHg)
2、加載數據集
public static TimeSeriesCollection createTimeSeries( List<PressureBean> list, int dayOrweekOrmonth, Log log, String shou,String shu ) { TimeSeriesCollection timesers = new TimeSeriesCollection(); int mon = 1; int day = 1; int ye = 2000; int week = 1; // 按天顯示 if (dayOrweekOrmonth == 0) { TimeSeries timeseries = new TimeSeries(shou, org.jfree.data.time.Day.class); TimeSeries timeseries1 = new TimeSeries("c1", org.jfree.data.time.Day.class); TimeSeries timeseriedia = new TimeSeries(shu, org.jfree.data.time.Day.class); TimeSeries timeseriedia1 = new TimeSeries("d1", org.jfree.data.time.Day.class); Iterator<PressureBean> it = list.iterator(); while (it.hasNext()) { PressureBean pres = it.next(); String date = pres.getBpDate(); ye = Integer.parseInt(date.substring(0, 4)); mon = Integer.parseInt(date.substring(5, 7)); day = Integer.parseInt(date.substring(8, date.length())); Day days = new Day(day, mon, ye); double sys = pres.getSyspress(); double dia = pres.getDiapress(); if (sys != -1 && sys > 0) { timeseries.add(days, sys); } else { timeseries1.add(days, null); } if (sys != -1 && sys > 0) { timeseriedia.add(days, dia); } else { timeseriedia1.add(days, null); } } timesers.addSeries(timeseries); timesers.addSeries(timeseriedia); timesers.addSeries(timeseries1); timesers.addSeries(timeseriedia1); } else if (dayOrweekOrmonth == 1) {//按周顯示 TimeSeries timeseries = new TimeSeries(shou, org.jfree.data.time.Week.class); TimeSeries timeseries1 = new TimeSeries("c1", org.jfree.data.time.Week.class); TimeSeries timeseriedia = new TimeSeries(shu, org.jfree.data.time.Week.class); TimeSeries timeseriedia1 = new TimeSeries("d1", org.jfree.data.time.Week.class); Iterator<PressureBean> it = list.iterator(); while (it.hasNext()) { PressureBean pres = it.next(); String date = pres.getBpDate(); String[] spls = date.split("-"); if (spls.length == 2) { ye = Integer.parseInt(spls[0]); mon = Integer.parseInt(spls[1]); } else { log.error("the date of weeks is wrong"); } Week days = new Week(mon, ye); double sys = pres.getSyspress(); double dia = pres.getDiapress(); if (sys != -1 && sys > 0) { timeseries.add(days, sys); } else { timeseries1.add(days, null); } if (sys != -1 && sys > 0) { timeseriedia.add(days, dia); } else { timeseriedia1.add(days, null); } } timesers.addSeries(timeseries); timesers.addSeries(timeseriedia); timesers.addSeries(timeseries1); timesers.addSeries(timeseriedia1); } else {//按月顯示 TimeSeries timeseries = new TimeSeries(shou, org.jfree.data.time.Month.class); TimeSeries timeseries1 = new TimeSeries("c1", org.jfree.data.time.Month.class); TimeSeries timeseriedia = new TimeSeries(shu, org.jfree.data.time.Month.class); TimeSeries timeseriedia1 = new TimeSeries("s", org.jfree.data.time.Month.class); Iterator<PressureBean> it = list.iterator(); while (it.hasNext()) { PressureBean pres = it.next(); String date = pres.getBpDate(); String[] spls = date.split("-"); if (spls.length == 2) { ye = Integer.parseInt(spls[0]); mon = Integer.parseInt(spls[1]); } else { log.error("the date of weeks is wrong"); } Month days = new Month(mon, ye); double sys = pres.getSyspress(); double dia = pres.getDiapress(); if (sys != -1 && sys > 0) { timeseries.add(days, sys); } else { timeseries1.add(days, null); } if (sys != -1 && sys > 0) { timeseriedia.add(days, dia); } else { timeseriedia1.add(days, null); } } timesers.addSeries(timeseries); timesers.addSeries(timeseriedia); timesers.addSeries(timeseries1); timesers.addSeries(timeseriedia1); } return timesers; }
3、畫折線圖,兩個數據集,收縮壓和舒張壓,并且,這兩條曲線還各自包含一個區域范圍,并不單單是一條基準線,而是一個基準范圍。
private static JFreeChart createChartPress(XYDataset xydataset, int weekOrmonth, String title, String y, String index, String week, String year, int searchby, String month, String nodatamess, List list, Log log, String bp_shou, String bp_shuzhang) { // 有可能用戶在后面的版本中故意輸入不正常數值,但是為了保證圖片畫圖的完整,這里先計算 // 用戶血壓值的***值。 double maxpress = 0; double addmax = 50; double min = 40; if (list != null && list.size() > 0) { Iterator<PressureBean> it = list.iterator(); while (it.hasNext()) { PressureBean pres = it.next(); double sys = pres.getSyspress(); double dia = pres.getDiapress(); if (maxpress < sys) { maxpress = sys; } if (maxpress < dia) maxpress = dia; if (min > sys) { min = sys; } if (min > dia) min = dia; } maxpress += addmax; min -= 10; log.info("high press value is =" + maxpress); } if (xydataset != null) { int counts = xydataset.getItemCount(0); if (counts == 0) { xydataset = null; } } JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(title, "", y, xydataset, true, true, false); jfreechart.setBackgroundPaint(Color.white); // 設置標題的顏色 TextTitle text = new TextTitle(title); text.setPaint(new Color(102, 102, 102)); jfreechart.setTitle(text); XYPlot xyplot = jfreechart.getXYPlot(); xyplot.setBackgroundPaint(new Color(255, 253, 246)); xyplot.setOutlineStroke(new BasicStroke(1.5f)); // 邊框粗細 ValueAxis vaxis = xyplot.getDomainAxis(); vaxis.setAxisLineStroke(new BasicStroke(1.5f)); // 坐標軸粗細 vaxis.setAxisLinePaint(new Color(215, 215, 215)); // 坐標軸顏色 xyplot.setOutlineStroke(new BasicStroke(1.5f)); // 邊框粗細 vaxis.setLabelPaint(new Color(10, 10, 10)); // 坐標軸標題顏色 vaxis.setTickLabelPaint(new Color(102, 102, 102)); // 坐標軸標尺值顏色 vaxis.setLowerMargin(0.06d);// 分類軸下(左)邊距 vaxis.setUpperMargin(0.14d);// 分類軸下(右)邊距,防止***邊的一個數據靠近了坐標軸。 //X軸為日期格式,這里是專門的處理日期的類, SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis(); if (weekOrmonth == 0) {//以天為刻度,時間格式為yyyy-MM-dd,如2008-02-06 dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 1, format)); } else if (weekOrmonth == 1) {//以周為刻度,時間顯示為 2009年第4周((這里是SimpleDateFormat的用法, //這里為了作繁體版,英文版和簡體版,用了國際化處理,將這些可變的資源在文字資源里面,注意一下,這里的y,M、w是SimpleDateFormat的關鍵字, //如英文表示09年第3周就是09W3,那么,這里的W需要用‘’引起來) format = new SimpleDateFormat("yyyy" + year + index + "w" + week); dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 7, format)); } else if (weekOrmonth == 2) {//以月為刻度,時間顯示為09-02 (09年2月) format = new SimpleDateFormat("yy-MM"); dateaxis .setTickUnit(new DateTickUnit(DateTickUnit.MONTH, 1, format)); } dateaxis.setVerticalTickLabels(false); // 設為true表示橫坐標旋轉到垂直。 if (searchby == 6 || searchby == 3) { dateaxis.setAutoTickUnitSelection(true); // 由于橫軸標簽過多,這里設置為自動格式 。 dateaxis.setDateFormatOverride(format); } dateaxis.setTickMarkPosition(DateTickMarkPosition.START); ValueAxis valueAxis = xyplot.getRangeAxis(); valueAxis.setUpperBound(maxpress); valueAxis.setAutoRangeMinimumSize(1); valueAxis.setLowerBound(min); valueAxis.setAutoRange(false); valueAxis.setAxisLineStroke(new BasicStroke(1.5f)); // 坐標軸粗細 valueAxis.setAxisLinePaint(new Color(215, 215, 215)); // 坐標軸顏色 valueAxis.setLabelPaint(new Color(10, 10, 10)); // 坐標軸標題顏色 valueAxis.setTickLabelPaint(new Color(102, 102, 102)); // 坐標軸標尺值顏色 xyplot.setRangeGridlinesVisible(true); xyplot.setDomainGridlinesVisible(true); xyplot.setRangeGridlinePaint(Color.LIGHT_GRAY); xyplot.setDomainGridlinePaint(Color.LIGHT_GRAY); xyplot.setBackgroundPaint(new Color(255, 253, 246)); xyplot.setNoDataMessage(nodatamess);//沒有數據時顯示的文字說明。 xyplot.setNoDataMessageFont(new Font("", Font.BOLD, 14));//字體的大小,粗體。 xyplot.setNoDataMessagePaint(new Color(87, 149, 117));//字體顏色 xyplot.setAxisOffset(new RectangleInsets(0d, 0d, 0d, 5d)); // // add range marker(舒張壓的區域marker,范圍是從62到81) double lowpress = 62; double uperpress = 81; IntervalMarker intermarker = new IntervalMarker(lowpress, uperpress); intermarker.setPaint(Color.decode("#66FFCC"));// 域顏色 intermarker.setLabelFont(new Font("SansSerif", 41, 14)); intermarker.setLabelPaint(Color.RED); intermarker.setLabel(bp_shuzhang); if (xydataset != null) { xyplot.addRangeMarker(intermarker, Layer.BACKGROUND); } //(收縮壓的區域marker,范圍是從102到120) double lowpress1 = 102; double uperpress1 = 120; IntervalMarker inter = new IntervalMarker(lowpress1, uperpress1); inter.setLabelOffsetType(LengthAdjustmentType.EXPAND); inter.setPaint(Color.decode("#66FFCC"));// 域顏色 inter.setLabelFont(new Font("SansSerif", 41, 14)); inter.setLabelPaint(Color.RED); inter.setLabel(bp_shou); if (xydataset != null) { xyplot.addRangeMarker(inter, Layer.BACKGROUND); // 加上Layer.BACKGROUND,將maker調到折線下面。 } XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot .getRenderer(); //***條折線的顏色 xylineandshaperenderer.setBaseItemLabelsVisible(true); xylineandshaperenderer.setSeriesFillPaint(0, new Color(127, 128, 0)); xylineandshaperenderer.setSeriesPaint(0, new Color(127, 128, 0)); xylineandshaperenderer.setSeriesShapesVisible(0, true); xylineandshaperenderer.setSeriesShapesVisible(1, true); //第二條折線的顏色 xylineandshaperenderer.setSeriesFillPaint(1, new Color(254, 103, 0)); xylineandshaperenderer.setSeriesPaint(1, new Color(254, 103, 0)); xylineandshaperenderer.setSeriesShapesVisible(1, true); xylineandshaperenderer.setSeriesVisible(2, false);// xylineandshaperenderer.setSeriesVisible(3, false);//不顯示下面標題 //折線的粗細調 StandardXYToolTipGenerator xytool = new StandardXYToolTipGenerator(); xylineandshaperenderer.setToolTipGenerator(xytool); xylineandshaperenderer.setStroke(new BasicStroke(1.5f)); // 顯示節點的值 xylineandshaperenderer.setBaseItemLabelsVisible(true); xylineandshaperenderer .setBasePositiveItemLabelPosition(new ItemLabelPosition( ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER)); xylineandshaperenderer .setBaseItemLabelGenerator(new StandardXYItemLabelGenerator()); xylineandshaperenderer.setBaseItemLabelPaint(new Color(102, 102, 102));// 顯示折點數值字體的顏色 return jfreechart; }
4、將圖片URL返回到頁面
public static void drawPressLineChart(IrisIoInterface io, Log log, TimeSeriesCollection timesers, int weekormonth, String title, String y, String index, String week, String year, int searchby, String month, String nodatamess, List list, String bp_shou, String bp_shuzhang) { JFreeChart chart = createChartPress(timesers, weekormonth, title, y, index, week, year, searchby, month, nodatamess, list, log, bp_shou, bp_shuzhang); HttpServletRequest request = io.getRequest(); String filename = ""; String graphURL = ""; try { filename = ServletUtilities.saveChartAsPNG(chart, 650, 280, null, io.getSession()); graphURL = request.getContextPath() + "/displayChart?filename=" + filename; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); log.error(e); } io.setData("filename1", filename, BeanShare.BEAN_SHARE_REQUEST); io.setData("presslineurl", graphURL, BeanShare.BEAN_SHARE_REQUEST); }
效果圖如下:
以天為刻度:
以周為刻度:
以月為刻度:
以上是“如何使用JFreeChart實現折線圖”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。