# 選項
# 選項解析
選項從上到下解析,使用與上下文相關的路徑。
# 圖表層級選項
- options
- 覆寫 [
config.type
] - 預設值
# 資料集層級選項
如果未指定,dataset.type
預設為 config.type
。
- dataset
- options.datasets[
dataset.type
] - options
- 覆寫[
config.type
].datasets[dataset.type
] - 預設值.datasets[
dataset.type
] - 預設值
# 資料集動畫選項
- dataset.animation
- options.datasets[
dataset.type
].animation - options.animation
- 覆寫[
config.type
].datasets[dataset.type
].animation - 預設值.datasets[
dataset.type
].animation - 預設值.animation
# 資料集元素層級選項
每個範圍都會先以選項名稱中的 elementType
前綴查找,然後不使用前綴查找。例如,point
元素的 radius
會先使用 pointRadius
查找,如果找不到,則使用 radius
。
- dataset
- options.datasets[
dataset.type
] - options.datasets[
dataset.type
].elements[elementType
] - options.elements[
elementType
] - options
- 覆寫[
config.type
].datasets[dataset.type
] - 覆寫[
config.type
].datasets[dataset.type
].elements[elementType
] - 預設值.datasets[
dataset.type
] - 預設值.datasets[
dataset.type
].elements[elementType
] - 預設值.elements[
elementType
] - 預設值
# 刻度選項
- options.scales
- 覆寫[
config.type
].scales - 預設值.scales
- 預設值.scale
# 外掛程式選項
外掛程式可以提供 additionalOptionScopes
路徑陣列,以便在其中額外尋找其選項。對於根範圍,請使用空字串:''
。大多數核心外掛程式也會從根範圍取得選項。
- options.plugins[
plugin.id
] - (options.[
...plugin.additionalOptionScopes
]) - 覆寫[
config.type
].plugins[plugin.id
] - 預設值.plugins[
plugin.id
] - (預設值.[
...plugin.additionalOptionScopes
])
# 可腳本化選項
可腳本化的選項也接受一個函式,該函式會針對每個基礎資料值呼叫,並接收代表上下文資訊的唯一參數 context
(請參閱選項上下文)。解析器會作為第二個參數傳遞,可用於存取相同上下文中的其他選項。
注意
應在可腳本化的函式中驗證 context
參數,因為該函式可能會在不同的上下文中呼叫。type
欄位是此驗證的良好候選。
範例
color: function(context) {
const index = context.dataIndex;
const value = context.dataset.data[index];
return value < 0 ? 'red' : // draw negative values in red
index % 2 ? 'blue' : // else, alternate values in blue and green
'green';
},
borderColor: function(context, options) {
const color = options.color; // resolve the value of another scriptable option: 'red', 'blue' or 'green'
return Chart.helpers.color(color).lighten(0.2);
}
# 可索引選項
可索引的選項也接受一個陣列,其中每個項目對應於相同索引處的元素。請注意,如果項目少於資料,則會迴圈遍歷項目。在許多情況下,如果支援,使用函式更為合適。
範例
color: [
'red', // color for data at index 0
'blue', // color for data at index 1
'green', // color for data at index 2
'black', // color for data at index 3
//...
]
# 選項上下文
選項上下文用於在解析選項時提供上下文資訊,目前僅適用於可腳本化的選項。該物件會被保留,因此可用於儲存和傳遞呼叫之間的資訊。
有多個層級的上下文物件
圖表
dataset
資料
刻度
刻度標籤
pointLabel
(僅在徑向線性刻度中使用)
工具提示
每個層級都會繼承其父層,且父層中儲存的任何上下文資訊都可透過子層取得。
上下文物件包含下列屬性
# 圖表
chart
:關聯的圖表type
:'chart'
# 資料集
除了圖表之外
active
:如果元素處於活動狀態(已懸停),則為 truedataset
:索引datasetIndex
處的資料集datasetIndex
:目前資料集的索引index
:與datasetIndex
相同mode
:更新模式type
:'dataset'
# 資料
除了資料集之外
active
:如果元素處於活動狀態(已懸停),則為 truedataIndex
:目前資料的索引parsed
:給定dataIndex
和datasetIndex
的已剖析資料值raw
:給定dataIndex
和datasetIndex
的原始資料值element
:此資料的元素(點、弧、條等)index
:與dataIndex
相同type
:'data'
# 刻度
除了圖表之外
scale
:關聯的刻度type
:'scale'
# 刻度標籤
除了刻度之外
tick
:關聯的刻度標籤物件index
:刻度標籤索引type
:'tick'
# pointLabel
除了刻度之外
label
:關聯的標籤值index
:標籤索引type
:'pointLabel'
# 工具提示
除了圖表之外
tooltip
:工具提示物件tooltipItems
:工具提示正在顯示的項目