# 选项

¥Options

# 选项解析

¥Option resolution

使用依赖于上下文的路由从上到下解析选项。

¥Options are resolved from top to bottom, using a context dependent route.

# 图表级别选项

¥Chart level options

  • options

  • overrides[config.type]

  • 默认值

    ¥defaults

# 数据集级别选项

¥Dataset level options

如果未指定,dataset.type 默认为 config.type

¥dataset.type defaults to config.type, if not specified.

  • dataset

  • options.datasets[dataset.type]

  • options

  • overrides [config.type].datasets[dataset.type]

  • defaults.datasets[dataset.type]

  • 默认值

    ¥defaults

# 数据集动画选项

¥Dataset animation options

  • dataset.animation

  • options.datasets[dataset.type].animation

  • options.animation

  • overrides [config.type].datasets[dataset.type].animation

  • defaults.datasets[dataset.type].animation

  • defaults.animation

# 数据集元素级别选项

¥Dataset element level options

首先在选项名称中使用 elementType 前缀查找每个作用域,然后不使用前缀。例如,使用 pointRadius 查找 point 元素的 radius,如果没有命中,则使用 radius

¥Each scope is looked up with elementType prefix in the option name first, then without the prefix. For example, radius for point element is looked up using pointRadius and if that does not hit, then radius.

  • dataset

  • options.datasets[dataset.type]

  • options.datasets[dataset.type].elements[elementType]

  • options.elements[elementType]

  • options

  • overrides [config.type].datasets[dataset.type]

  • overrides [config.type].datasets[dataset.type].elements[elementType]

  • defaults.datasets[dataset.type]

  • defaults.datasets[dataset.type].elements[elementType]

  • defaults.elements[elementType]

  • 默认值

    ¥defaults

# 标尺选项

¥Scale options

  • options.scales

  • overrides[config.type].scales

  • defaults.scales

  • defaults.scale

# 插件选项

¥Plugin options

插件可以提供 additionalOptionScopes 路径数组来额外查找其选项。对于根范围,使用空字符串:''。大多数核心插件也从根范围中获取选项。

¥A plugin can provide additionalOptionScopes array of paths to additionally look for its options in. For root scope, use empty string: ''. Most core plugins also take options from root scope.

  • options.plugins[plugin.id]

  • (选项。[...plugin.additionalOptionScopes])

    ¥(options.[...plugin.additionalOptionScopes])

  • overrides[config.type].plugins[plugin.id]

  • defaults.plugins[plugin.id]

  • (默认值。[...plugin.additionalOptionScopes])

    ¥(defaults.[...plugin.additionalOptionScopes])

# 脚本选项

¥Scriptable Options

脚本选项还接受一个函数,该函数为每个基础数据值调用,并采用表示上下文信息的唯一参数 context(请参阅 选项上下文)。解析器作为第二个参数传递,可用于访问同一上下文中的其他选项。

¥Scriptable options also accept a function which is called for each of the underlying data values and that takes the unique argument context representing contextual information (see option context). A resolver is passed as second parameter, that can be used to access other options in the same context.

注意

context 参数应该在可编写脚本的函数中进行验证,因为该函数可以在不同的上下文中调用。type 字段很适合此验证。

¥The context argument should be validated in the scriptable function, because the function can be invoked in different contexts. The type field is a good candidate for this validation.

示例:

¥Example:

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);
}

# 可转位选项

¥Indexable Options

可索引选项还接受一个数组,其中每个项目对应于同一索引处的元素。请注意,如果项目少于数据,则项目将被循环。在许多情况下,如果支持,使用 function 更合适。

¥Indexable options also accept an array in which each item corresponds to the element at the same index. Note that if there are less items than data, the items are looped over. In many cases, using a function is more appropriate if supported.

示例:

¥Example:

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
    //...
]

# 选项上下文

¥Option Context

选项上下文用于在解析选项时给出上下文信息,目前仅适用于 脚本选项。该对象被保留下来,因此它可用于在调用之间存储和传递信息。

¥The option context is used to give contextual information when resolving options and currently only applies to scriptable options. The object is preserved, so it can be used to store and pass information between calls.

有多个级别的上下文对象:

¥There are multiple levels of context objects:

  • chart

    • dataset

      • data
    • scale

      • tick

      • pointLabel(仅用于径向光栅尺)

        ¥pointLabel (only used in the radial linear scale)

    • tooltip

每个级别都继承其父级,并且存储在父级中的任何上下文信息都可以通过子级获得。

¥Each level inherits its parent(s) and any contextual information stored in the parent is available through the child.

上下文对象包含以下属性:

¥The context object contains the following properties:

# chart

  • chart:相关图表

    ¥chart: the associated chart

  • type'chart'

# dataset

除了 chart

¥In addition to chart

  • active:true 如果元素处于活动状态(悬停)

    ¥active: true if an element is active (hovered)

  • dataset:索引 datasetIndex 处的数据集

    ¥dataset: dataset at index datasetIndex

  • datasetIndex:当前数据集的索引

    ¥datasetIndex: index of the current dataset

  • index:同 datasetIndex

    ¥index: same as datasetIndex

  • mode:更新模式

    ¥mode: the update mode

  • type'dataset'

# data

除了 dataset

¥In addition to dataset

  • active:true 如果元素处于活动状态(悬停)

    ¥active: true if an element is active (hovered)

  • dataIndex:当前数据的索引

    ¥dataIndex: index of the current data

  • parsed:给定 dataIndexdatasetIndex 的解析数据值

    ¥parsed: the parsed data values for the given dataIndex and datasetIndex

  • raw:给定 dataIndexdatasetIndex 的原始数据值

    ¥raw: the raw data values for the given dataIndex and datasetIndex

  • element:此数据的元素(点、弧、条等)

    ¥element: the element (point, arc, bar, etc.) for this data

  • index:同 dataIndex

    ¥index: same as dataIndex

  • type'data'

# scale

除了 chart

¥In addition to chart

  • scale:相关规模

    ¥scale: the associated scale

  • type'scale'

# 刻度

¥tick

除了 scale

¥In addition to scale

  • tick:关联的刻度对象

    ¥tick: the associated tick object

  • index:滴答索引

    ¥index: tick index

  • type'tick'

# pointLabel

除了 scale

¥In addition to scale

  • label:关联的标签值

    ¥label: the associated label value

  • index:标签索引

    ¥index: label index

  • type'pointLabel'

# tooltip

除了 chart

¥In addition to chart

  • tooltip:工具提示对象

    ¥tooltip: the tooltip object

  • tooltipItems:工具提示显示的项目

    ¥tooltipItems: the items the tooltip is displaying