JfreeChart折線圖 CSDN-李鵬飛
阿新 • • 發佈:2017-05-17
.net comment ans minimum sage 日期格式 .info n) 學校
今天公司裏分配給我的工作是JfreeChart折線圖本人之前也沒接觸過如今讓我們大家一起完畢!
在這個公司,用到了太多的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的包括的屬性:
Java代碼
- int userId;
- String bpDate;
- String bpTime;
- int syspress; //收縮壓(mmHg)
- int diapress; //舒張壓(mmHg)
2、載入數據集
Java代碼
- 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
- 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、畫折線圖,兩個數據集,收縮壓和舒張壓。而且,這兩條曲線還各自包括一個區域範圍,並不單單是一條基準線,而是一個基準範圍
Java代碼
- 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返回到頁面
Java代碼
- 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);
- }
如有轉載請註明出處:版權全部---李鵬飛 2014/6/3日
心情日誌:聽著歌寫著博客!今天第一天進公司開始興趣昂昂的走進公司!今天第一天上班公司經理就分配了工作但是框架都沒熟悉又和在CSDN樂知學院學到的SSH框架不一樣所以感慨道離開老師離開學校真的不習慣。
??JfreeChart折線圖 CSDN-李鵬飛