Options

Scriptable Options

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).

Example:

  1. color: function(context) {
  2. var index = context.dataIndex;
  3. var value = context.dataset.data[index];
  4. return value < 0 ? 'red' : // draw negative values in red
  5. index % 2 ? 'blue' : // else, alternate values in blue and green
  6. 'green';
  7. }

Indexable Options

Indexable options also accept an array in which each item corresponds to the element at the same index. Note that this method requires to provide as many items as data, so, in most cases, using a function is more appropriated if supported.

Example:

  1. color: [
  2. 'red', // color for data at index 0
  3. 'blue', // color for data at index 1
  4. 'green', // color for data at index 2
  5. 'black', // color for data at index 3
  6. //...
  7. ]

Option Context

The option context is used to give contextual information when resolving options and currently only applies to scriptable options.

The context object contains the following properties:

  • chart: the associated chart
  • dataIndex: index of the current data
  • dataset: dataset at index datasetIndex
  • datasetIndex: index of the current dataset
  • hover: true if hovered

Important: since the context can represent different types of entities (dataset, data, etc.), some properties may be undefined so be sure to test any context property before using it.