圖形分解
要想繪製出漂亮的圖表,就必須瞭解圖表的構成部分,將圖表進行分解成N個部分。
--------------------------------
JfreeChart 中文API
--------------------------------
要想繪製出漂亮的圖表,就必須瞭解圖表的構成部分,將圖表進行分解成N個部分。
然後再對每一個部分進行渲染,設置樣式:包括背景色、輪廓線條樣式和顏色、填充顏色、字體大小、樣式、顏色。
同時,需要確保在整個項目中,圖表的樣式風格整體統一。
統一,和諧才能打造漂亮、乾淨、專業的外觀.
--------------------------------
1
-圖表組成部門
整個圖表-JFreeChart
圖表繪製區域(圖表輪廓內部稱之爲繪製區域)-Plot
標題-Title
副標題-Subtitle
X座標軸-DomainAxis
Y座標軸-RangeAxis(ValueAxis)
X座標軸標題-DomainAxis.AxisLabel
Y座標軸標題-RangeAxis(ValueAxis).AxisLabel
X座標軸水平線DomainAxis.AxisLine
Y座標軸水平線RangeAxis(ValueAxis)AxisLine
X座標軸標尺值DomainAxis.TickLabel
Y座標軸標尺值RangeAxis(ValueAxis).TickLabel
X座標軸標尺豎線DomainAxis.TickMark
Y座標軸標尺水平線RangeAxis(ValueAxis).TickMark
垂直數據軸網格線:DomainGridlines
水平數據軸網格線:RangeGridlines
標註-Legend
繪製圖形的步驟:
1
-創建數據集合,然後根據數據創建JfreeChart
2
-chart.setTextAntiAlias(
false
);
//必須設置文本抗鋸齒爲false,防止亂碼
3
-對圖表進行渲染
4
-修改X-Y軸;標註,圖表樣式
5
-創建ChartPanel;chartPanel =
new
ChartPanel(chart);
--------------------------------
2
-主題樣式
JfreeChart主題樣式有StandardChartTheme進行統一管理,包括字體,繪製顏色,線條類型,座標軸樣式等!
private
static
Font FONT =
new
Font(
"宋體"
, Font.PLAIN,
12
);
// 設置中文主題樣式 解決亂碼
StandardChartTheme chartTheme =
new
StandardChartTheme(
"CN"
);
// 設置標題字體
chartTheme.setExtraLargeFont(FONT);
// 設置圖例的字體
chartTheme.setRegularFont(FONT);
// 設置軸向的字體
chartTheme.setLargeFont(FONT);
chartTheme.setSmallFont(FONT);
chartTheme.setTitlePaint(
new
Color(
51
,
51
,
51
));
//Paint 可以理解爲繪製顏色;標題字體顏色
chartTheme.setSubtitlePaint(
new
Color(
85
,
85
,
85
));
//副標題字體顏色
chartTheme.setLegendBackgroundPaint(Color.WHITE);
// 設置標註背景色
chartTheme.setLegendItemPaint(Color.BLACK);
//設置字體顏色
chartTheme.setChartBackgroundPaint(Color.WHITE);
//圖表背景色
// 繪製顏色繪製顏色.輪廓供應商
// paintSequence,outlinePaintSequence,strokeSequence,outlineStrokeSequence,shapeSequence
Paint[] OUTLINE_PAINT_SEQUENCE =
new
Paint[] { Color.WHITE };
//外邊框線條顏色
//繪製器顏色源-所有線條 餅圖 默認顏色都是來自DefaultDrawingSupplier
DefaultDrawingSupplier drawingSupplier =
new
DefaultDrawingSupplier(CHART_COLORS, CHART_COLORS, OUTLINE_PAINT_SEQUENCE,
DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE, DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE);
chartTheme.setDrawingSupplier(drawingSupplier);
//設置繪製顏色、線條、外邊框供應商
chartTheme.setPlotBackgroundPaint(Color.WHITE);
// 繪製區域背景色
chartTheme.setPlotOutlinePaint(Color.WHITE);
// 繪製區域外邊框
chartTheme.setLabelLinkPaint(
new
Color(
8
,
55
,
114
));
// 鏈接標籤顏色
chartTheme.setLabelLinkStyle(PieLabelLinkStyle.CUBIC_CURVE);
chartTheme.setAxisOffset(
new
RectangleInsets(
5
,
12
,
5
,
12
));
//設置X-Y座標軸偏移量
chartTheme.setDomainGridlinePaint(
new
Color(
192
,
208
,
224
));
// X座標軸垂直網格顏色
chartTheme.setRangeGridlinePaint(
new
Color(
192
,
192
,
192
));
// Y座標軸水平網格顏色
chartTheme.setBaselinePaint(Color.WHITE);
chartTheme.setCrosshairPaint(Color.BLUE);
// 不確定含義
chartTheme.setAxisLabelPaint(
new
Color(
51
,
51
,
51
));
// 座標軸標題文字顏色
chartTheme.setTickLabelPaint(
new
Color(
67
,
67
,
72
));
// 刻度數字顏色
chartTheme.setBarPainter(
new
StandardBarPainter());
// 設置柱狀圖渲染問哦基本渲染,不採用漸變
chartTheme.setXYBarPainter(
new
StandardXYBarPainter());
// XYBar 渲染,值使用顏色渲染,不採用漸變
chartTheme.setItemLabelPaint(Color.black);
chartTheme.setThermometerPaint(Color.white);
// 溫度計
ChartFactory.setChartTheme(chartTheme);
//設置主題樣式
--------------------------------
3
-API描述;對每一個部分進行樣式控制,只涉及重要API!
JFreeChart類:圖標對象,任何類型的圖標的最終表現形式都是在該對象上進行一些屬性的定製。
JFreeChart引擎本身提供了一個工廠類用於創建不同類型的圖表對象。
void
setAntiAlias(
boolean
flag) 設置字體抗鋸齒,必須關閉字體抗鋸齒,否則文字顯示非常模糊:用法chart.setTextAntiAlias(
false
);
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
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類中定義)
--------------------------------
X軸
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) 座標軸標尺是否顯示
--------------------------------
Y軸
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
)
--------------------------------
X 日期軸
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) 分類軸分類標籤的最大寬度
--------------------------------
繪製區域:
Plot類:圖標區域對象,基本上這個對象決定着圖表的樣式,創建該對象時需要Axis,Renderer及數據集對象的支持。
void
setBackgroundImage(Image image) 數據區的背景圖片
void
setBackgroundImageAlignment(
int
alignment) 數據區的背景圖片對齊方式(參數常量在org.jfree.ui.Align類中定義)
void
setBackgroundPaint(Paint paint) 數據區的背景圖片背景色
void
setBackgroundAlpha(
float
alpha) 數據區的背景透明度(
0.0
~
1.0
)
void
setForegroundAlpha(
float
alpha) 數據區的前景透明度(
0.0
~
1.0
)
void
setDataAreaRatio(
double
ratio) 數據區佔整個圖表區的百分比
void
setOutLinePaint(Paint paint) 數據區的邊界線條顏色
void
setOutLineStroke(Stroke stroke) 數據區的邊界線條筆觸
void
setNoDataMessage(String message) 沒有數據時顯示的消息
void
setNoDataMessageFont(Font font) 沒有數據時顯示的消息字體
void
setNoDataMessagePaint(Paint paint) 沒有數據時顯示的消息顏色
--------------------------------
CategoryPlot(Plot)類:
void
setDataset(CategoryDataset dataset) 數據區的
2
維數據表
void
setColumnRenderingOrder(SortOrder order) 數據分類的排序方式
void
setAxisOffset(Spacer offset) 座標軸到數據區的間距
void
setOrientation(PlotOrientation orientation) 數據區的方向(PlotOrientation.HORIZONTAL或PlotOrientation.VERTICAL)
void
setDomainAxis(CategoryAxis axis) 數據區的分類軸
void
setDomainAxisLocation(AxisLocation location) 分類軸的位置(參數常量在org.jfree.chart.axis.AxisLocation類中定義)
void
setDomainGridlinesVisible(
boolean
visible) 垂直方向:分類軸網格是否可見
void
setDomainGridlinePaint(Paint paint) 分類軸網格線條顏色
void
setDomainGridlineStroke(Stroke stroke) 分類軸網格線條筆觸
void
setRangeAxis(ValueAxis axis) 數據區的數據軸
void
setRangeAxisLocation(AxisLocation location) 數據軸的位置(參數常量在org.jfree.chart.axis.AxisLocation類中定義)
void
setRangeGridlinesVisible(
boolean
visible) 數據軸網格是否可見
void
setRangeGridlinePaint(Paint paint) 數據軸網格線條顏色
void
setRangeGridlineStroke(Stroke stroke) 數據軸網格線條筆觸
void
setRenderer(CategoryItemRenderer renderer) 數據區的表示者(詳見Renderer組)
void
addAnnotation(CategoryAnnotation annotation) 給數據區加一個註釋
void
addRangeMarker(Marker marker,Layer layer) 給數據區加一個數值範圍區域
--------------------------------
PiePlot(Plot)類:
void
setDataset(PieDataset dataset) 數據區的
1
維數據表
void
setIgnoreNullValues(
boolean
flag) 忽略無值的分類
void
setCircular(
boolean
flag) 餅圖是否一定是正圓
void
setStartAngle(
double
angle) 餅圖的初始角度
void
setDirection(Rotation direction) 餅圖的旋轉方向
void
setExplodePercent(
int
section,
double
percent) 抽取的那塊(
1
維數據表的分類下標)以及抽取出來的距離(
0.0
~
1.0
),3D餅圖無效
void
setLabelBackgroundPaint(Paint paint) 分類標籤的底色
void
setLabelFont(Font font) 分類標籤的字體
void
setLabelPaint(Paint paint) 分類標籤的字體顏色
void
setLabelLinkMargin(
double
margin) 分類標籤與圖的連接線邊距
void
setLabelLinkPaint(Paint paint) 分類標籤與圖的連接線顏色
void
setLabelLinkStroke(Stroke stroke) 分類標籤與圖的連接線筆觸
void
setLabelOutlinePaint(Paint paint) 分類標籤邊框顏色
void
setLabelOutlineStroke(Paint paint) 分類標籤邊框筆觸
void
setLabelShadowPaint(Paint paint) 分類標籤陰影顏色
void
setMaximumLabelWidth(
double
width) 分類標籤的最大長度(
0.0
~
1.0
)
void
setPieIndex(
int
index) 餅圖的索引(複合餅圖中用到)
void
setSectionOutlinePaint(
int
section,Paint paint) 指定分類餅的邊框顏色
void
setSectionOutlineStroke(
int
section,Stroke stroke) 指定分類餅的邊框筆觸
void
setSectionPaint(
int
section,Paint paint) 指定分類餅的顏色
void
setShadowPaint(Paint paint) 餅圖的陰影顏色
void
setShadowXOffset(
double
offset) 餅圖的陰影相對圖的水平偏移
void
setShadowYOffset(
double
offset) 餅圖的陰影相對圖的垂直偏移
void
setLabelGenerator(PieSectionLabelGenerator generator) 分類標籤的格式,設置成
null
則整個標籤包括連接線都不顯示
void
setToolTipGenerator(PieToolTipGenerator generator) MAP中鼠標移上的顯示格式
void
setURLGenerator(PieURLGenerator generator) MAP中鑽取鏈接格式
--------------------------------
PiePlot3D(PiePlot)類:
void
setDepthFactor(
double
factor) 3D餅圖的Z軸高度(
0.0
~
1.0
)
MultiplePiePlot(Plot)類:
void
setLimit(
double
limit) 每個餅圖之間的數據關聯(詳細比較複雜)
void
setPieChart(JFreeChart pieChart) 每個餅圖的顯示方式(見JFreeChart類個PiePlot類)
--------------------------------
渲染 Render
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之間的間隔(
0
-
1
)
void
setMaxBarWidth(
double
percent) 每個BAR的最大寬度(
0
-
1
)
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)類
--------------------------------
3
-獲得核心對象
//得到JFreeChart
JFreeChart chart = ChartFactory.createTimeSeriesChart(title,
""
,
"新增中長期貸款(億元)"
, lendingDataset);
//得到Plot
XYPlot xyplot = (XYPlot) chart.getPlot();
//得到渲染器
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
//得到X座標軸
plot.getDomainAxis();
//得到Y座標軸
ValueAxis axis = plot.getRangeAxis();
plot.getRangeAxis().setUpperMargin(
0.1
);
// 設置頂部Y座標軸間距,防止數據無法顯示
plot.getRangeAxis().setLowerMargin(
0.1
);
// 設置底部Y座標軸間距
// 得到標註
chart.getLegend();
chart.getLegend().setFrame(
new
BlockBorder(Color.WHITE));
//設置標註表框
--------------------------------
3
-JfreeChart注意問題事項
--------------------------------
(
1
)中文亂碼問題
解決:主題樣式只需要加載一次,放在靜態代碼塊即可!
public
class
ChartUtils {
private
static
String NO_DATA_MSG =
"數據加載失敗"
;
private
static
Font FONT =
new
Font(
"宋體"
, Font.PLAIN,
12
);
public
static
Color[] CHART_COLORS = {
new
Color(
31
,
129
,
188
),
new
Color(
241
,
92
,
128
),
new
Color(
124
,
181
,
236
),
new
Color(
102
,
172
,
204
),
new
Color(
102
,
102
,
0
),
new
Color(
204
,
153
,
102
),
new
Color(
0
,
153
,
255
),
new
Color(
204
,
255
,
255
),
new
Color(
51
,
153
,
153
),
new
Color(
255
,
204
,
102
),
new
Color(
102
,
102
,
0
),
new
Color(
204
,
204
,
204
),
new
Color(
204
,
255
,
255
),
new
Color(
255
,
204
,
204
),
new
Color(
255
,
255
,
204
),
new
Color(
255
,
153
,
204
),
new
Color(
51
,
0
,
0
),
new
Color(
0
,
51
,
102
),
new
Color(
0
,
153
,
102
),
new
Color(
153
,
102
,
153
),
new
Color(
102
,
153
,
204
),
new
Color(
153
,
204
,
153
),
new
Color(
204
,
204
,
153
),
new
Color(
255
,
255
,
153
),
new
Color(
255
,
204
,
153
),
new
Color(
255
,
153
,
204
),
new
Color(
204
,
153
,
153
),
new
Color(
204
,
204
,
255
),
new
Color(
204
,
255
,
204
),
new
Color(
153
,
204
,
153
),
new
Color(
255
,
204
,
102
) };
//顏色
static
{
setChartTheme();
}
public
ChartUtils() {
}
/**
* 中文主題樣式 解決亂碼
*/
public
static
void
setChartTheme() {
// 設置中文主題樣式 解決亂碼
StandardChartTheme chartTheme =
new
StandardChartTheme(
"CN"
);
// 設置標題字體
chartTheme.setExtraLargeFont(FONT);
// 設置圖例的字體
chartTheme.setRegularFont(FONT);
// 設置軸向的字體
chartTheme.setLargeFont(FONT);
chartTheme.setSmallFont(FONT);
chartTheme.setTitlePaint(
new
Color(
51
,
51
,
51
));
chartTheme.setSubtitlePaint(
new
Color(
85
,
85
,
85
));
chartTheme.setLegendBackgroundPaint(Color.WHITE);
// 設置標註
chartTheme.setLegendItemPaint(Color.BLACK);
//
chartTheme.setChartBackgroundPaint(Color.WHITE);
// 繪製顏色繪製顏色.輪廓供應商
// paintSequence,outlinePaintSequence,strokeSequence,outlineStrokeSequence,shapeSequence
Paint[] OUTLINE_PAINT_SEQUENCE =
new
Paint[] { Color.WHITE };
//繪製器顏色源
DefaultDrawingSupplier drawingSupplier =
new
DefaultDrawingSupplier(CHART_COLORS, CHART_COLORS, OUTLINE_PAINT_SEQUENCE,
DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE, DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE);
chartTheme.setDrawingSupplier(drawingSupplier);
chartTheme.setPlotBackgroundPaint(Color.WHITE);
// 繪製區域
chartTheme.setPlotOutlinePaint(Color.WHITE);
// 繪製區域外邊框
chartTheme.setLabelLinkPaint(
new
Color(
8
,
55
,
114
));
// 鏈接標籤顏色
chartTheme.setLabelLinkStyle(PieLabelLinkStyle.CUBIC_CURVE);
chartTheme.setAxisOffset(
new
RectangleInsets(
5
,
12
,
5
,
12
));
chartTheme.setDomainGridlinePaint(
new
Color(
192
,
208
,
224
));
// X座標軸垂直網格顏色
chartTheme.setRangeGridlinePaint(
new
Color(
192
,
192
,
192
));
// Y座標軸水平網格顏色
chartTheme.setBaselinePaint(Color.WHITE);
chartTheme.setCrosshairPaint(Color.BLUE);
// 不確定含義
chartTheme.setAxisLabelPaint(
new
Color(
51
,
51
,
51
));
// 座標軸標題文字顏色
chartTheme.setTickLabelPaint(
new
Color(
67
,
67
,
72
));
// 刻度數字
chartTheme.setBarPainter(
new
StandardBarPainter());
// 設置柱狀圖渲染
chartTheme.setXYBarPainter(
new
StandardXYBarPainter());
// XYBar 渲染
chartTheme.setItemLabelPaint(Color.black);
chartTheme.setThermometerPaint(Color.white);
// 溫度計
ChartFactory.setChartTheme(chartTheme);
}
}
--------------------------------
(
2
)中文字體模糊
解決:必須取消文本抗鋸齒,創建JFreeChart完畢後,調用setAntiAlias取消文本抗鋸齒
public
static
void
setAntiAlias(JFreeChart chart) {
chart.setTextAntiAlias(
false
);
}
調用:
JFreeChart chart = ChartFactory.createTimeSeriesChart(title,
""
,
""
, dataset);
ChartUtils.setAntiAlias(chart);
--------------------------------
(
3
)創建折線圖時,顯示數據,數據被遮蓋
解決:
plot.getRangeAxis().setUpperMargin(
0.1
);
// 設置頂部Y座標軸間距百分比,防止數據無法顯示
plot.getRangeAxis().setLowerMargin(
0.1
);
// 設置底部Y座標軸間距百分比
--------------------------------
(
4
)創建時間序列柱狀圖圖時,柱子顯示過細
解決:暫時無法解決
原因:柱子寬度由 Timeseries.add(
new
Day(date), dValue); add裏面的RegularTimePeriod決定。
意思就是:如果RegularTimePeriod爲
Day,那個這個柱子的寬度佔比爲
1
/(最大日期-最小日期 之間的天數)
Month,那個這個柱子的寬度佔比爲
1
/(最大日期-最小日期 之間的月數)
YEAR,那個這個柱子的寬度佔比爲
1
/(最大日期-最小日期 之間的年數)
所以,時間間隔越小,柱子寬度就越粗。
如果時間間隔過大的話,建議使用時間序列折線圖或者柱狀圖代替。
---------------------------------
(5)設置 X 座標刻度間隔單位(X座標軸數據太密集處理)
NumberAxis domainAxis = (NumberAxis)plot.getDomainAxis();//x軸設置 domainAxis.setTickUnit(new NumberTickUnit(100));/每100個刻度顯示一個刻度值