JFreeChart API

jfreechart目前最高版本爲1.0.13版(http://www.jfree.org/jfreechart/index.html)。可以繪製
pie charts 餅圖,bar charts 柱狀圖,line and area charts曲線圖,scatter plots and bubble charts 散列圖,time series 時序圖,Area Charts區域圖,Difference Chart差異圖,Step Chart步驟圖,Multiple Axis Charts 混合圖,Gantt charts甘特圖,combination charts 複合圖
JFreeChart核心類庫介紹:
jfreechart主要由兩個大的包組成:org.jfree.chart,org.jfree.data。其中前者主要與圖形
本身有關,後者與圖形顯示的數據有關。

核心類主要有:
org.jfree.chart.JFreeChart:圖表對象,任何類型的圖表的最終表現形式都是在該對象進行一些屬性的定製。JFreeChart 引擎本身提供了一個工廠類用於創建不同類型的圖表對象
org.jfree.data.category.XXXDataSet:數據集對象,用於提供顯示圖表所用的數據。根據不同類型的圖表對應着很多類型的數據集對象類
org.jfree.chart.plot.XXXPlot:圖表區域對象,基本上這個對象決定着什麼樣式的圖表,創建該對象的時候需要Axis、 Renderer以及數據集對象的支持
org.jfree.chart.axis.XXXAxis:用於處理圖表的兩個軸:縱軸和橫軸
org.jfree.chart.render.XXXRender:負責如何顯示一個圖表對象
org.jfree.chart.urls.XXXURLGenerator:用於生成Web圖表中每個項目的鼠標點擊鏈接
XXXXXToolTipGenerator:用於生成圖象的幫助提示,不同類型圖表對應不同類型的工具提示類
對於常用的餅圖闔柱狀圖,比較簡單而且網上有很多的文章介紹,在這裏就不再一一複述了,
(可以參考這篇文章http://www-128.ibm.com/developerworks/cn/java/l-jfreechart /index.html?ca=dwcn-isc&me=ccid)
主要說明下另一種常見的報表,時序圖,首先聲明一個曲線數據集合對象和曲線對象


TimePeriodValuesCollection timeseriescollection = new TimePeriodValuesCollection();
//聲明具體是曲線對象,(可根據實際情況在同一張圖中顯示多條曲線進行數據比對,根據實際應用情況當超過4條曲線時,就會有些亂。)
TimePeriodValues timeperiod1 = new TimePeriodValues("服務器A在線用戶數量");
TimePeriodValues timeperiod2 = new TimePeriodValues("服務器B在線用戶數量");
我在使用TimeSeriesCollection tsc = new TimeSeriesCollection();
TimeSeries ts = new TimeSeries();
在生成數據集時(ts.add(new Day(day, month, year),10)))只能生成最小單位爲天的橫軸所以改用了TimePeriodValuesCollection
//根據當前時間取得橫軸座標,時間間隔爲1小時
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
//這裏改爲根據自己程序得到的需要顯示的時間點和對應的數據的集合;
List objectList1 = dao.getList1();
List objectList2 = dao.getList2();
//使用循環,把x軸,y軸的值賦給timeseries1
for (int i =0;i<objecthash1.size();i++) {
    int hour = objecthash1[i].getHours();
    int count = objecthash1[i].getCount();
 //將每一對數據(時間,數值)添加到數據集合1(曲線對象1)中
    timeseries1.add(new Hour(hour, day, month, year),count);
}
for (int i =0;i<objecthash2.size();i++) {
    int hour = objecthash2[i].getHours();
    int count = objecthash2[i].getCount();
 //將每一對數據(時間,數值)添加到數據集合2(曲線對象2)中
    timeseries2.add(new Hour(hour, day, month, year),count);
}
//將曲線對象添加到曲線數據集合對象中
timeseriescollection.addSeries(timeseries1);
timeseriescollection.addSeries(timeseries2);
//繪製報表
String title = "日在線用戶統計"; //報表標題
String domain = "時間";    //x軸
String range = "用戶在線數量";   //y軸
//創建時間序列圖對象
JFreeChart chart = ChartFactory.createTimeSeriesChart(
title,  //報表標題
domain, //報表橫軸標籤
range,  //報表縱軸標籤
timeseriescollection, //數據集合
true,  //是否顯示圖例,在這裏如果爲true則會在圖表的下方顯示各條數據曲線的名稱和顏色
false, // 是否生成工具
false   // 是否生成URL鏈接);
//將報表保存爲jpg文件
ChartUtilities.saveChartAsJPEG(file, //文件保存物理路徑包括路徑和文件名
100,   //圖片質量
chart, //圖表對象
1024,  //圖像寬度
768,   //圖像高度
null); //顯示信息
//將報表直接在頁面輸出
ChartUtilities.writeChartAsJPEG(res.getOutputStream(),100,chart,1024,768,null);

String title="月在線用戶統計";  //標題
String domain="時間(天)";//x軸
String range="用戶在線數量";//y軸
TimePeriodValuesCollection   timeseriescollection   =   new   TimePeriodValuesCollection();
TimePeriodValues timeseries = new TimePeriodValues( "用戶數量");
 timeseries.add(new Minute(0, 1, 1, 1, 2006), 100);
 timeseries.add(new Minute(10, 1, 1, 1, 2006), 500);
 timeseries.add(new Minute(20, 1, 1, 1, 2006), 300);
 timeseries.add(new Minute(30, 1, 1, 1, 2006), 800);
JFreeChart chart =ChartFactory.createTimeSeriesChart(title,domain,range,timeseriescollection,true,false,false);
當我們生成了一個報表對象時,可能需要根據實際情況來決定報表的橫軸和縱軸的數值間隔,顯示方式等。
可以用XYPlot xyplot = (XYPlot)chart.getPlot();來得到所有數據點的集合。(其它形狀圖表得到的數據集對象根據實際情況造型)
得到數據點集合後,我們就可以設置各條曲線的顏色,和座標軸的距離,x軸、y軸的顯示方式等等屬性
xyplot.setBackgroundPaint(Color.lightGray); //設定圖表數據顯示部分背景色
xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D)); //設定座標軸與圖表數據顯示部分距離
xyplot.setDomainGridlinePaint(Color.white); //網格線縱向顏色
xyplot.setRangeGridlinePaint(Color.white); //網格線橫向顏色

數據點的調整
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)xyplot.getRenderer();
xylineandshaperenderer.setDefaultShapesVisible(true);  //數據點可見
xylineandshaperenderer.setSeriesFillPaint(0, Color.red);  //設置第一條曲線數據點填充爲紅色,如果一個圖表有多條曲線可分別設置
xylineandshaperenderer.setUseFillPaint(true);    //應用

使用xyplot.getRangeAxis()得到縱軸,xyplot.getDomainAxis()得到橫軸,得到後可以根據實際情況造型爲自己所需要的類型。
我的圖表縱軸爲數值類型,橫軸爲時間類型,使用如下方式
NumberAxis numAxis = (NumberAxis)xyplot.getRangeAxis();
DateAxis  dateaxis =   (DateAxis)xyplot.getDomainAxis();
//設置y顯示方式
numAxis.setAutoTickUnitSelection(false);//數據軸的數據標籤是否自動確定
double  rangetick = 0.1D;
numAxis.setTickUnit(new NumberTickUnit(rangetick));  //y軸單位間隔爲0.1
//設置x軸顯示方式
dateaxis.setAutoTickUnitSelection(false);//數據軸的數據標籤是否自動確定
dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY,1));//x軸單位間隔爲1天
我們還可以是將數據格式化以後顯示,比如y軸顯示百分比(10%~100%),x軸顯示爲×月×日
NumberFormat nf =NumberFormat.getPercentInstance();
numAxis.setNumberFormatOverride(nf);//設置y軸以百分比方式顯示
SimpleDateFormat format = new SimpleDateFormat("MM月dd");
dateaxis.setDateFormatOverride(format);//設置x軸數據單位以×月×日方式顯示
時序圖中還有一個很重要的方法
timeseriescollection.setDomainIsPointsInTime(true); //x軸上的刻度點代表的是時間點而不是時間段
最開始我沒有設置這個屬性,結果畫出來的圖,老是差半格不能在這個刻度的時候準確顯示,往後移了半格,就是因爲JFreeChart默認這個刻度是
一個時間段,它把這個刻度和下個刻度的中間點認爲是顯示數據點最佳位置。

其他一些關於AXIS類的方法:

Axis類:
void setVisible(boolean flag)座標軸是否可見
void setAxisLinePaint(Paint paint)座標軸線條顏色(3D軸無效)
void setAxisLineStroke(Stroke stroke)座標軸線條筆觸(3D軸無效)
void setAxisLineVisible(boolean visible)座標軸線條是否可見(3D軸無效)
void setFixedDimension(double dimension)(用於複合表中對多座標軸的設置)
void setLabel(String label)座標軸標題
void setLabelFont(Font font)座標軸標題字體
void setLabelPaint(Paint paint)座標軸標題顏色
void setLabelAngle(double angle)`座標軸標題旋轉角度(縱座標可以旋轉)
void setTickLabelFont(Font font)座標軸標尺值字體
void setTickLabelPaint(Paint paint)座標軸標尺值顏色
void setTickLabelsVisible(boolean flag)座標軸標尺值是否顯示
void setTickMarkPaint(Paint paint)座標軸標尺顏色
void setTickMarkStroke(Stroke stroke)座標軸標尺筆觸
void setTickMarksVisible(boolean flag)座標軸標尺是否顯示

ValueAxis(Axis)類:
void setAutoRange(boolean auto)自動設置數據軸數據範圍
void setAutoRangeMinimumSize(double size)自動設置數據軸數據範圍時數據範圍的最小跨度
void setAutoTickUnitSelection(boolean flag)數據軸的數據標籤是否自動確定(默認爲true)
void setFixedAutoRange(double length)數據軸固定數據範圍(設置100的話就是顯示MAXVALUE到MAXVALUE-100那段數據範圍)
void setInverted(boolean flag)數據軸是否反向(默認爲false)
void setLowerMargin(double margin)數據軸下(左)邊距
void setUpperMargin(double margin)數據軸上(右)邊距
void setLowerBound(double min)數據軸上的顯示最小值
void setUpperBound(double max)數據軸上的顯示最大值
void setPositiveArrowVisible(boolean visible)是否顯示正向箭頭(3D軸無效)
void setNegativeArrowVisible(boolean visible)是否顯示反向箭頭(3D軸無效)
void setVerticalTickLabels(boolean flag)數據軸數據標籤是否旋轉到垂直
void setStandardTickUnits(TickUnitSource source)數據軸的數據標籤(可以只顯示整數標籤,需要將AutoTickUnitSelection設false)

NumberAxis(ValueAxis)類:
void setAutoRangeIncludesZero(boolean flag)是否強制在自動選擇的數據範圍中包含0
void setAutoRangeStickyZero(boolean flag)是否強制在整個數據軸中包含0,即使0不在數據範圍中
void setNumberFormatOverride(NumberFormat formatter)數據軸數據標籤的顯示格式
void setTickUnit(NumberTickUnit unit)數據軸的數據標籤(需要將AutoTickUnitSelection設false)

DateAxis(ValueAxis)類:
void setMaximumDate(Date maximumDate)日期軸上的最小日期
void setMinimumDate(Date minimumDate)日期軸上的最大日期
void setRange(Date lower,Date upper)日期軸範圍
void setDateFormatOverride(DateFormat formatter)日期軸日期標籤的顯示格式
void setTickUnit(DateTickUnit unit)日期軸的日期標籤(需要將AutoTickUnitSelection設false)
void setTickMarkPosition(DateTickMarkPosition position)日期標籤位置(參數常量在org.jfree.chart.axis.DateTickMarkPosition類中定義)

CategoryAxis(Axis)類:
void setCategoryMargin(double margin)分類軸邊距
void setLowerMargin(double margin)分類軸下(左)邊距
void setUpperMargin(double margin)分類軸上(右)邊距
void setVerticalCategoryLabels(boolean flag)分類軸標題是否旋轉到垂直
void setMaxCategoryLabelWidthRatio(float ratio)分類軸分類標籤的最大寬度
jfreechart 設置技巧

1.橫座標內容豎立
      XYPlot xyplot = jfreechart.getXYPlot();
        DateAxis dateaxis = (DateAxis)xyplot.getDomainAxis();
        dateaxis.setTickUnit(new DateTickUnit(1, 1, new SimpleDateFormat("MMM-yyyy")));
        dateaxis.setVerticalTickLabels(true);

2.設置最大座標範圍
 1)ValueAxis axis = xyplot.getRangeAxis() ;
       axis.setRange(0,100) ;
       xyplot.setRangeAxis(axis);

  2)numberaxis1.setUpperBound(6500D);//最大值
     numberaxis1.setLowerBound(5500D);//最小值
2.設置時間軸的間隔時間
    dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY,1));//設置時間間隔爲一天
學JFreeChart不得不看的中文API

JFreeChart類:
void setAntiAlias(boolean flag)字體模糊邊界
void setBackgroundImage(Image image)背景圖片
void setBackgroundImageAlignment(int alignment)背景圖片對齊方式(參數常量在org.jfree.ui.Align類中定義)
void setBackgroundImageAlpha(float alpha)背景圖片透明度(0.0~1.0)
void setBackgroundPaint(Paint paint)背景色
void setBorderPaint(Paint paint)邊界線條顏色
void setBorderStroke(Stroke stroke)邊界線條筆觸
void setBorderVisible(boolean visible)邊界線條是否可見

-----------------------------------------------------------------------------------------------------------

TextTitle類:
void setFont(Font font)標題字體
void setPaint(Paint paint)標題字體顏色
void setText(String text)標題內容

-----------------------------------------------------------------------------------------------------------

StandardLegend(Legend)類:
void setBackgroundPaint(Paint paint)圖示背景色
void setTitle(String title)圖示標題內容
void setTitleFont(Font font)圖示標題字體
void setBoundingBoxArcWidth(int arcWidth)圖示邊界圓角寬
void setBoundingBoxArcHeight(int arcHeight)圖示邊界圓角高
void setOutlinePaint(Paint paint)圖示邊界線條顏色
void setOutlineStroke(Stroke stroke)圖示邊界線條筆觸
void setDisplaySeriesLines(boolean flag)圖示項是否顯示橫線(折線圖有效)
void setDisplaySeriesShapes(boolean flag)圖示項是否顯示形狀(折線圖有效)
void setItemFont(Font font)圖示項字體
void setItemPaint(Paint paint)圖示項字體顏色
void setAnchor(int anchor)圖示在圖表中的顯示位置(參數常量在Legend類中定義)

Axis類:
void setVisible(boolean flag)座標軸是否可見
void setAxisLinePaint(Paint paint)座標軸線條顏色(3D軸無效)
void setAxisLineStroke(Stroke stroke)座標軸線條筆觸(3D軸無效)
void setAxisLineVisible(boolean visible)座標軸線條是否可見(3D軸無效)
void setFixedDimension(double dimension)(用於複合表中對多座標軸的設置)
void setLabel(String label)座標軸標題
void setLabelFont(Font font)座標軸標題字體
void setLabelPaint(Paint paint)座標軸標題顏色
void setLabelAngle(double angle)`座標軸標題旋轉角度(縱座標可以旋轉)
void setTickLabelFont(Font font)座標軸標尺值字體
void setTickLabelPaint(Paint paint)座標軸標尺值顏色
void setTickLabelsVisible(boolean flag)座標軸標尺值是否顯示
void setTickMarkPaint(Paint paint)座標軸標尺顏色
void setTickMarkStroke(Stroke stroke)座標軸標尺筆觸
void setTickMarksVisible(boolean flag)座標軸標尺是否顯示

ValueAxis(Axis)類:
void setAutoRange(boolean auto)自動設置數據軸數據範圍
void setAutoRangeMinimumSize(double size)自動設置數據軸數據範圍時數據範圍的最小跨度
void setAutoTickUnitSelection(boolean flag)數據軸的數據標籤是否自動確定(默認爲true)
void setFixedAutoRange(double length)數據軸固定數據範圍(設置100的話就是顯示MAXVALUE到MAXVALUE-100那段數據範圍)
void setInverted(boolean flag)數據軸是否反向(默認爲false)
void setLowerMargin(double margin)數據軸下(左)邊距
void setUpperMargin(double margin)數據軸上(右)邊距
void setLowerBound(double min)數據軸上的顯示最小值
void setUpperBound(double max)數據軸上的顯示最大值
void setPositiveArrowVisible(boolean visible)是否顯示正向箭頭(3D軸無效)
void setNegativeArrowVisible(boolean visible)是否顯示反向箭頭(3D軸無效)
void setVerticalTickLabels(boolean flag)數據軸數據標籤是否旋轉到垂直
void setStandardTickUnits(TickUnitSource source)數據軸的數據標籤(可以只顯示整數標籤,需要將AutoTickUnitSelection設false)

NumberAxis(ValueAxis)類:
void setAutoRangeIncludesZero(boolean flag)是否強制在自動選擇的數據範圍中包含0
void setAutoRangeStickyZero(boolean flag)是否強制在整個數據軸中包含0,即使0不在數據範圍中
void setNumberFormatOverride(NumberFormat formatter)數據軸數據標籤的顯示格式
void setTickUnit(NumberTickUnit unit)數據軸的數據標籤(需要將AutoTickUnitSelection設false)

DateAxis(ValueAxis)類:
void setMaximumDate(Date maximumDate)日期軸上的最小日期
void setMinimumDate(Date minimumDate)日期軸上的最大日期
void setRange(Date lower,Date upper)日期軸範圍
void setDateFormatOverride(DateFormat formatter)日期軸日期標籤的顯示格式
void setTickUnit(DateTickUnit unit)日期軸的日期標籤(需要將AutoTickUnitSelection設false)
void setTickMarkPosition(DateTickMarkPosition position)日期標籤位置(參數常量在org.jfree.chart.axis.DateTickMarkPosition類中定義)

CategoryAxis(Axis)類:
void setCategoryMargin(double margin)分類軸邊距
void setLowerMargin(double margin)分類軸下(左)邊距
void setUpperMargin(double margin)分類軸上(右)邊距
void setVerticalCategoryLabels(boolean flag)分類軸標題是否旋轉到垂直
void setMaxCategoryLabelWidthRatio(float ratio)分類軸分類標籤的最大寬度

AbstractRenderer類:
void setItemLabelAnchorOffset(double offset)數據標籤的與數據點的偏移
void setItemLabelsVisible(boolean visible)數據標籤是否可見
void setItemLabelFont(Font font)數據標籤的字體
void setItemLabelPaint(Paint paint)數據標籤的字體顏色
void setItemLabelPosition(ItemLabelPosition position)數據標籤位置
void setPositiveItemLabelPosition(ItemLabelPosition position)正數標籤位置
void setNegativeItemLabelPosition(ItemLabelPosition position)負數標籤位置
void setOutLinePaint(Paint paint)圖形邊框的線條顏色
void setOutLineStroke(Stroke stroke)圖形邊框的線條筆觸
void setPaint(Paint paint)所有分類圖形的顏色
void setShape(Shape shape)所有分類圖形的形狀(如折線圖的點)
void setStroke(Stroke stroke)所有分類圖形的筆觸(如折線圖的線)
void setSeriesItemLabelsVisible(int series,boolean visible)指定分類的數據標籤是否可見
void setSeriesItemLabelFont(int series,Font font)指定分類的數據標籤的字體
void setSeriesItemLabelPaint(int series,Paint paint)指定分類的數據標籤的字體顏色
void setSeriesItemLabelPosition(int series,ItemLabelPosition position)數據標籤位置
void setSeriesPositiveItemLabelPosition(int series,ItemLabelPosition position)正數標籤位置
void setSeriesNegativeItemLabelPosition(int series,ItemLabelPosition position)負數標籤位置
void setSeriesOutLinePaint(int series,Paint paint)指定分類的圖形邊框的線條顏色
void setSeriesOutLineStroke(int series,Stroke stroke)指定分類的圖形邊框的線條筆觸
void setSeriesPaint(int series,Paint paint)指定分類圖形的顏色
void setSeriesShape(int series,Shape shape)指定分類圖形的形狀(如折線圖的點)
void setSeriesStroke(int series,Stroke stroke)指定分類圖形的筆觸(如折線圖的線)

AbstractCategoryItemRenderer(AbstractRenderer)類:
void setLabelGenerator(CategoryLabelGenerator generator)數據標籤的格式
void setToolTipGenerator(CategoryToolTipGenerator generator)MAP中鼠標移上的顯示格式
void setItemURLGenerator(CategoryURLGenerator generator)MAP中鑽取鏈接格式
void setSeriesLabelGenerator(int series,CategoryLabelGenerator generator)指定分類的數據標籤的格式
void setSeriesToolTipGenerator(int series,CategoryToolTipGenerator generator)指定分類的MAP中鼠標移上的顯示格式
void setSeriesItemURLGenerator(int series,CategoryURLGenerator generator)指定分類的MAP中鑽取鏈接格式

BarRenderer(AbstractCategoryItemRenderer)類:
void setDrawBarOutline(boolean draw)是否畫圖形邊框
void setItemMargin(double percent)每個BAR之間的間隔
void setMaxBarWidth(double percent)每個BAR的最大寬度
void setMinimumBarLength(double min)最短的BAR長度,避免數值太小而顯示不出
void setPositiveItemLabelPositionFallback(ItemLabelPosition position)無法在BAR中顯示的正數標籤位置
void setNegativeItemLabelPositionFallback(ItemLabelPosition position)無法在BAR中顯示的負數標籤位置

BarRenderer3D(BarRenderer)類:
void setWallPaint(Paint paint)3D座標軸的牆體顏色

StackedBarRenderer(BarRenderer)類:
沒有特殊的設置

StackedBarRenderer3D(BarRenderer3D)類:
沒有特殊的設置

GroupedStackedBarRenderer(StackedBarRenderer)類:
void setSeriesToGroupMap(KeyToGroupMap map)將分類自由的映射成若干個組(KeyToGroupMap.mapKeyToGroup(series,group))

LayeredBarRenderer(BarRenderer)類:
void setSeriesBarWidth(int series,double width)設定每個分類的寬度(注意設置不要使某分類被覆蓋)

WaterfallBarRenderer(BarRenderer)類:
void setFirstBarPaint(Paint paint)第一個柱圖的顏色
void setLastBarPaint(Paint paint)最後一個柱圖的顏色
void setPositiveBarPaint(Paint paint)正值柱圖的顏色
void setNegativeBarPaint(Paint paint)負值柱圖的顏色

IntervalBarRenderer(BarRenderer)類:
需要傳IntervalCategoryDataset作爲數據源

GanttBarRenderer(IntervalBarRenderer)類:
void setCompletePaint(Paint paint)完成進度顏色
void setIncompletePaint(Paint paint)未完成進度顏色
void setStartPercent(double percent)設置進度條在整條中的起始位置(0.0~1.0)
void setEndPercent(double percent)設置進度條在整條中的結束位置(0.0~1.0)

StatisticBarRenderer(BarRenderer)類:
需要傳StatisticCategoryDataset作爲數據源

LineAndShapeRenderer(AbstractCategoryItemRenderer)類:
void setDrawLines(boolean draw)是否折線的數據點之間用線連
void setDrawShapes(boolean draw)是否折線的數據點根據分類使用不同的形狀
void setShapesFilled(boolean filled)所有分類是否填充數據點圖形
void setSeriesShapesFilled(int series,boolean filled)指定分類是否填充數據點圖形
void setUseFillPaintForShapeOutline(boolean use)指定是否填充數據點的Paint也被用於畫數據點形狀的邊框

LevelRenderer(AbstractCategoryItemRenderer)類:
void setItemMargin(double percent)每個分類之間的間隔
void setMaxItemWidth(double percent)每個分類的最大寬度

CategoryStepRenderer(AbstractCategoryItemRenderer)類:
void setStagger(boolean shouldStagger)不同分類的圖是否交錯

MinMaxCategoryRenderer(AbstractCategoryItemRenderer)類:
void setDrawLines(boolean drawLines)是否在每個分類線間畫連接線
void setGroupPaint(Paint groupPaint)一組圖形連接線的顏色
void setGroupStroke(Stroke groupStroke)一組圖形連接線的筆觸
void setMaxIcon(Icon maxIcon)最大值的ICON
void setMinIcon(Icon minIcon)最小值的ICON
void setObjectIcon(Icon objectIcon)所有值的ICON

AreaRender(AbstractCategoryItemRenderer)類:
沒有特殊的設置

StackedAreaRender(AreaRender)類:
沒有特殊的設置


關鍵就是用好Renderer這個類了,再貼個例子:
    String sFont = "宋體";
    chart.setBorderVisible(true);
    chart.setBorderPaint(new Color(0xFF,0x66,0x00));
    chart.setBackgroundPaint(new Color(0xFF,0xF3,0xDE));
    chart.getTitle().setPaint(Color.red);
    chart.getTitle().setFont(new Font(sFont,Font.BOLD,14));

    //設置Plot,不顯示所有網格
    ((CategoryPlot)chart.getPlot()).setOutlinePaint(null);
    ((CategoryPlot)chart.getPlot()).setDomainGridlinesVisible(false);
    ((CategoryPlot)chart.getPlot()).setRangeGridlinesVisible(false);

    //設置橫軸字體,設定橫軸軸線不可見,隱藏縱軸
    ((CategoryPlot)chart.getPlot()).getDomainAxis().setTickLabelFont(new Font(sFont,Font.PLAIN,12));
    ((CategoryPlot)chart.getPlot()).getDomainAxis().setAxisLineVisible(false);
    ((CategoryPlot)chart.getPlot()).getRangeAxis().setVisible(false);

    //採用BarRenderer作爲表示器
    BarRenderer renderer = new BarRenderer();
    renderer.setPaint(new GradientPaint(0.0f,0.0f,Color.orange,0.0f,0.0f,Color.yellow));
    renderer.setOutlinePaint(Color.orange);
    renderer.setDrawBarOutline(true);

    //在條中央顯示投票數值
    renderer.setItemLabelAnchorOffset(-20.0f);
    renderer.setLabelGenerator(new StandardCategoryLabelGenerator("{2}",new DecimalFormat()));
    renderer.setPositiveItemLabelPosition(new ItemLabelPosition());
    renderer.setItemLabelsVisible(true);

我的需求是這樣,需要做一個網站訪問流量的變化趨勢圖,橫座標是時間,縱座標是訪問量即可。

剛開始我用linechart(就是折線圖,可以帶點,不帶點)來做的,因爲沒有經驗覺得linechart夠用了,結果上線用了一個多月,隨着時間的延長,我發線linechart圖不夠好了,因爲時間長了之後linechart圖並不能顯示出來所有的數據點,只能顯示出來35個左右的點位。假如我以天爲時間單位,我要得到兩個月,即60天的趨勢圖,那麼它是顯示不全的,數據點位會丟失,而且label也會變成“……”。

private static DefaultCategoryDataset createDefaultDataset(){
  String series1 = "First";
  String series2 = "Second";
  String series3 = "Third";

  // column keys...
  String type1 = "Type 1";
  String type2 = "Type 2";
  String type3 = "Type 3";
  String type4 = "Type 4";
  String type5 = "Type 5";
  String type6 = "Type 6";
  String type7 = "Type 7";
  String type8 = "Type 8";

  // create the dataset...
  DefaultCategoryDataset dataset = new DefaultCategoryDataset();

  dataset.addValue(1.0, series1, type1);
  dataset.addValue(4.0, series1, type2);
  dataset.addValue(3.0, series1, type3);
  dataset.addValue(5.0, series1, type4);
  dataset.addValue(5.0, series1, type5);
  dataset.addValue(7.0, series1, type6);
  dataset.addValue(7.0, series1, type7);
  dataset.addValue(8.0, series1, type8);

  dataset.addValue(5.0, series2, type1);
  dataset.addValue(7.0, series2, type2);
  dataset.addValue(6.0, series2, type3);
  dataset.addValue(8.0, series2, type4);
  dataset.addValue(4.0, series2, type5);
  dataset.addValue(4.0, series2, type6);
  dataset.addValue(2.0, series2, type7);
  dataset.addValue(1.0, series2, type8);

  dataset.addValue(4.0, series3, type1);
  dataset.addValue(3.0, series3, type2);
  dataset.addValue(2.0, series3, type3);
  dataset.addValue(3.0, series3, type4);
  dataset.addValue(6.0, series3, type5);
  dataset.addValue(3.0, series3, type6);
  dataset.addValue(4.0, series3, type7);
  dataset.addValue(3.0, series3, type8);
  return dataset;
 }
 private static XYDataset createDataset()
     {
         XYSeries xyseries = new XYSeries("allyes"); //先產生XYSeries 對象
         xyseries.add(20070801, 220000);
         xyseries.add(20070802, 210000);
         xyseries.add(20070803, 250000D);
         xyseries.add(20070804, 450000D);
         xyseries.add(20070805, 270000D);
         xyseries.add(20070806, 280000D);
         xyseries.add(20070807, 290000D);
         xyseries.add(20070808, 500000D);
       
         XYSeries xyseries1 = new XYSeries("direct");
         xyseries1.add(20070801, 230000D);
         xyseries1.add(20070802, 240000D);
         xyseries1.add(20070803, 250000D);
         xyseries1.add(20070804, 260000D);
         xyseries1.add(20070805, 270000D);
         xyseries1.add(20070806, 480000D);
         xyseries1.add(20070807, 290000D);
         xyseries1.add(20070808, 250000D);
       
         XYSeries xyseries2 = new XYSeries("iplus");
         xyseries2.add(20070801, 240000D);
         xyseries2.add(20070802, 280000D);
         xyseries2.add(20070803, 270000D);
         xyseries2.add(20070804, 290000D);
         xyseries2.add(20070805, 230000D);
         xyseries2.add(20070806, 310000D);
         xyseries2.add(20070807, 400000D);
         xyseries2.add(20070808, 220000D);
       
         XYSeriesCollection xyseriescollection = new XYSeriesCollection(); //再用XYSeriesCollection添加入XYSeries 對象
         xyseriescollection.addSeries(xyseries);
         xyseriescollection.addSeries(xyseries1);
         xyseriescollection.addSeries(xyseries2);
         return xyseriescollection;
     }

 

JFreeChart jfreechart = ChartFactory.createXYLineChart(
    des, x, y, dataset,//這裏的dateset可以是適用於linechart的所有dataset集合,可以是 DefaultCategoryDataset,XYDataset等等。前面給出了兩個得到dataset集合的方法。
    PlotOrientation.VERTICAL, true, true, false);
  //外框背景色
  jfreechart.setBackgroundPaint(Color.WHITE);
 
  CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot(); // 獲得 plot
 
  categoryplot.setBackgroundPaint(Color.BLACK); // 設定圖表數據顯示部分背景色
  categoryplot.setOutlinePaint(Color.RED);//設置數據區的邊界線條顏色
  categoryplot.setRangeGridlinePaint(Color.BLACK);
  categoryplot.setDomainAxisLocation(AxisLocation.TOP_OR_LEFT);
  categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D)); // 設定座標軸與圖表數據顯示部分距離
  categoryplot.setDomainGridlinePaint(Color.white); // 網格線顏色
  categoryplot.setRangeGridlinePaint(Color.white);
 
  // 獲得 renderer 注意這裏是XYLineAndShapeRenderer !!
  LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot
  .getRenderer();
  lineandshaperenderer.setShapesVisible(false); // series 點(即數據點)可見
  //設置線條寬度
  //lineandshaperenderer.setSeriesStroke(0,new BasicStroke(5));
  lineandshaperenderer.setStroke(new BasicStroke(4));

  lineandshaperenderer.setSeriesStroke(0, new BasicStroke(2.0F, 1, 1,
    1.0F, new float[] { 10F, 6F }, 0.0F));
  // 定義series爲"First"的(即series1)點之間的連線 ,這裏是虛線,默認是直線
  lineandshaperenderer.setSeriesStroke(1, new BasicStroke(2.0F, 1, 1,
    1.0F, new float[] { 6F, 6F }, 0.0F));
  // 定義series爲"Second"的(即series2)點之間的連線
  lineandshaperenderer.setSeriesStroke(2, new BasicStroke(2.0F, 1, 1,
    1.0F, new float[] { 2.0F, 6F }, 0.0F));
  // 定義series爲"Third"的(即series3)點之間的連線
 
  lineandshaperenderer.setShapesFilled(true); // 數據點被填充即不是空心點

lineandshaperenderer.setStroke(new BasicStroke(4));//這個是設置線條的粗細

這些問題困擾着我,找了一些文檔也沒有解決。最後我改爲了時間序列圖TimeSeriesChart,它也是折線圖,有橫軸和縱軸,它的好處是,橫軸的範圍很大,不會丟失數據點位,而且可以按你想要的時間單位來區分間隔,很好用。如果是要做跟時間相關的,比如說一個時間對應一個數據值那就最好用這個 chart。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章