jqPlot 是一款相當完整的圖表繪製函式庫,折線圖、長條圖、圓餅圖樣樣都有,而且呼叫方式非常直覺。官網:https://www.jqplot.com/
$.jqplot('target', data, options);
預設的分類顏色序列。當分類數量超過顏色總數時,會從第一個顏色重新循環取用。
seriesColors: ["#4bb2c5", "#c5b47f", "#EAA228", "#579575", "#839557",
"#958c12", "#953579", "#4b5de4", "#d8b83f", "#ff5800", "#0085cc"]
設為 true 且有多個分類(折線圖則需多於一條線)時,每個分類在縱軸上的值會變成「前面所有分類的縱軸值總和 + 自己的值」,也就是堆疊效果。
title: '', // 簡寫:直接給標題文字
title: {
text: '', // 標題文字
show: true // 是否顯示標題
}
axesDefaults: {
show: false, // 是否顯示座標軸
min: null, // 最小刻度值
max: null, // 最大刻度值
pad: 1.2, // 刻度值增長因子
ticks: [], // 指定刻度上的顯示值
numberTicks: undefined, // 刻度間隔因子:間隔 = 區間長度 / (numberTicks - 1)
renderer: $.jqplot.LinearAxisRenderer, // 軸的渲染器
rendererOptions: {}, // 傳給渲染器的設定(線性圖不需要)
tickOptions: {
mark: 'outside', // 刻度顯示位置:'outside' 軸外 / 'inside' 軸內 / 'cross' 穿越
showMark: true, // 是否顯示刻度
showGridline: true, // 是否顯示與刻度同方向的網格線
markSize: 4, // 刻度線頂點距軸上點的像素距離;mark 為 'cross' 時此值×2
show: true, // 是否顯示刻度線、網格線與刻度值
showLabel: true, // 是否顯示刻度線與刻度值
formatString: '', // 刻度值格式,如 '%b %#d, %Y' → 'AUG 30, 2008'
fontSize: '10px', // 刻度值字級
fontFamily: 'Tahoma', // 刻度值字型
angle: 40, // 刻度值與座標軸夾角(順時針)
fontWeight: 'normal', // 字粗
fontStretch: 1 // 刻度值在軸外方向的伸縮度
},
showTicks: true, // 是否顯示刻度線與刻度值
showTickMarks: true, // 是否顯示刻度
useSeriesColor: true // 多軸時是否以不同顏色區分
}
可為 xaxis、yaxis、x2axis、y2axis 分別指定設定,可用屬性與 axesDefaults 完全相同。
seriesDefaults: {
show: true, // 是否渲染整個圖表區域
xaxis: 'xaxis', // 'xaxis' 或 'x2axis'
yaxis: 'yaxis', // 'yaxis' 或 'y2axis'
label: '', // 圖例中顯示的分類名稱
color: '', // 該分類的顏色
lineWidth: 2.5, // 折線寬度
shadow: true, // 是否顯示陰影
shadowAngle: 45, // 陰影角度
shadowOffset: 1.25,
shadowDepth: 3,
shadowAlpha: 0.1,
showLine: true, // 是否顯示折線
showMarker: true, // 是否強調顯示資料點
fill: false, // 是否填滿折線下方區域(需搭配 showLine: true)
fillAndStroke: false, // 填滿後是否在上方再畫一條線
fillColor: undefined, // 填滿區域顏色
fillAlpha: undefined, // 填滿區域透明度
renderer: $.jqplot.PieRenderer, // 用其他渲染器轉換圖表類型
rendererOptions: {}, // 傳給該渲染器的設定
markerRenderer: $.jqplot.MarkerRenderer,
markerOptions: {
show: true, // 是否顯示資料點
style: 'filledCircle', // 資料點樣式,另有 circle、diamond、square
// 及 filledDiamond、filledSquare
lineWidth: 2, // 資料點邊線寬度(過大會看起來像實心點)
size: 9, // 資料點大小
color: '#666666', // 資料點顏色
shadow: true,
shadowAngle: 45,
shadowOffset: 1,
shadowDepth: 3,
shadowAlpha: 0.07
},
isDragable: true // 是否允許拖動(需引入 dragable 包)
}
series: [
{ label: 'Traps Division' },
{ label: 'Decoy Division' },
{ label: 'Harmony Division' }
] // 可用屬性同 seriesDefaults
legend: {
show: false, // 是否顯示圖例框
location: 'ne', // 位置:nw, n, ne, e, se, s, sw, w
xoffset: 12, // 距圖表區上邊緣的距離(px)
yoffset: 12, // 距圖表區左邊緣的距離(px)
background: '', // 圖例框背景色
textColor: '' // 圖例框文字顏色
}
grid: {
drawGridLines: true, // 是否繪製網格線
gridLineColor: '#cccccc', // 網格線顏色
background: '#fffdf6', // 圖表區背景色
borderColor: '#999999', // 外框顏色
borderWidth: 2.0, // 外框寬度
shadow: true, // 外框陰影,用於營造立體感
shadowAngle: 45,
shadowOffset: 1.5,
shadowWidth: 3,
shadowDepth: 3,
shadowAlpha: 0.07,
renderer: $.jqplot.CanvasGridRenderer,
rendererOptions: {} // CanvasGridRenderer 無額外選項
}
seriesDefaults: {
rendererOptions: {
barPadding: 8, // 同一分類內兩根長條之間的距離(px)
barMargin: 10, // 不同分類的長條之間的距離(px)
barDirection: 'vertical', // 'vertical' 或 'horizontal'
barWidth: null, // 每根長條的寬度
shadowOffset: 2,
shadowDepth: 5,
shadowAlpha: 0.8
}
}
滑鼠移入圖表時的游標樣式,也常用來做區域放大(zoom)。此設定直接放在 options 下。
cursor: {
style: 'crosshair', // 游標的 CSS 樣式
show: true, // 是否顯示游標
showTooltip: true, // 是否顯示提示資訊
followMouse: false, // 提示框是否跟著滑鼠移動
tooltipLocation: 'se', // 提示框位置;followMouse 為 true 時相對於游標
tooltipOffset: 6, // 提示框與游標或座標軸的間距
showTooltipGridPosition: false, // 是否顯示游標距圖表左/上邊緣的像素位置
showTooltipUnitPosition: true, // 是否顯示游標所在位置的數值(注意與上一項的差異)
tooltipFormatString: '%.4P',
useAxesFormatters: true,
tooltipAxesGroups: [] // 只顯示指定軸組,如 [['xaxis','yaxis'], ['xaxis','y2axis']]
}
此設定透過 seriesDefaults 或 series 設定。
seriesDefaults: {
dragable: {
color: undefined, // 拖動時拖動線與資料點的顏色
constrainTo: 'none' // 限制方向:'x' 僅水平、'y' 僅垂直、'none' 不限制
}
}
滑鼠移到資料點上時放大並顯示提示框。直接放在 options 下。
highlighter: {
lineWidthAdjust: 2.5, // 放大後非實心資料點的線寬
sizeAdjust: 5, // 資料點放大的增量
showTooltip: true,
tooltipLocation: 'nw', // n, ne, e, se, s, sw, w, nw
fadeTooltip: true, // 提示框是否淡入淡出
tooltipFadeSpeed: 'fast', // 'slow'、'def'、'fast' 或毫秒數
tooltipOffset: 2, // 提示框距資料點的像素偏移
tooltipAxes: 'both', // 顯示哪個軸的值:'x'、'y' 或 'xy'
tooltipSeparator: ', ', // 多值之間的分隔符號
useAxesFormatters: true, // 是否沿用座標軸的格式
tooltipFormatString: '%.5P' // useAxesFormatters 為 false 時生效,支援 HTML
}
tooltipFormatString 除了格式化符號,也可以直接塞 HTML,例如:
'<b><i><span style="color:red;">hello</span></i></b> %.2f'
axesDefaults: {
base: 10, // 對數底數
tickDistribution: 'even' // 'even' 為均勻分布;'power' 依倍數遞增決定刻度
}
seriesDefaults: {
rendererOptions: {
diameter: undefined, // 圓餅直徑
padding: 20, // 圓餅距圖例或圖表邊緣的距離(等效於改直徑)
sliceMargin: 20, // 各扇區之間的間距
fill: true, // 是否填滿各扇區
shadow: true, // 扇區邊框陰影
shadowOffset: 2,
shadowDepth: 5,
shadowAlpha: 0.07
}
}
在資料點旁直接標註文字,非提示框性質。
seriesDefaults: {
pointLabels: {
location: 's', // 標籤相對於資料點的方位
ypadding: 2 // 縱軸方向的間距
}
}
seriesDefaults: {
trendline: {
show: true,
color: '#666666',
label: '',
type: 'linear', // 'linear' 直線 / 'exponential' 或 'exp' 指數
shadow: true,
lineWidth: 1.5,
shadowAngle: 45,
shadowOffset: 1.5,
shadowDepth: 3,
shadowAlpha: 0.07
}
}
第一次用不必急著背這些選項。先把 $.jqplot('div', [[1,3],[2,7],[3,9]]) 跑出一張折線圖,再依需求加上 axes 的 min/max/ticks 調整刻度,接著用 legend 與 seriesColors 修外觀,最後才依圖表類型掛上對應的 rendererOptions。全部細項可對照官方 API 文件。