Make indexable options looping (#7442)

* Make indexable options looping
* Migration note
This commit is contained in:
Jukka Kurkela
2020-05-29 23:27:10 +03:00
committed by Evert Timberg
parent b71e4c3c7f
commit 2b57660aba
5 changed files with 60 additions and 4 deletions

View File

@@ -25,6 +25,12 @@ Chart.js 3.0 introduces a number of breaking changes. Chart.js 2.0 was released
A number of changes were made to the configuration options passed to the `Chart` constructor. Those changes are documented below.
#### Generic changes
* Indexable options are now looping. `backgroundColor: ['red', 'green']` will result in alternating `'red'` / `'green'` if there are more than 2 data points.
#### Specific changes
* `hover.animationDuration` is now configured in `animation.active.duration`
* `responsiveAnimationDuration` is now configured in `animation.resize.duration`
* Polar area `startAngle` option is now consistent with `Radar`, 0 is at top and value is in degrees. Default is changed from `-½π` to `0`.

View File

@@ -120,7 +120,7 @@ export function resolve(inputs, context, index, info) {
cacheable = false;
}
if (index !== undefined && isArray(value)) {
value = value[index];
value = value[index % value.length];
cacheable = false;
}
if (value !== undefined) {

View File

@@ -0,0 +1,45 @@
module.exports = {
config: {
type: 'bar',
data: {
labels: [0, 1, 2, 3, 4, 5],
datasets: [
{
// option in dataset
data: [0, 2, 3, 4, 5, 6],
backgroundColor: [
'#ff0000',
'#00ff00',
'#0000ff'
]
},
{
// option in element (fallback)
data: [6, 5, 4, 3, 2, 1],
}
]
},
options: {
legend: false,
title: false,
elements: {
rectangle: {
backgroundColor: [
'#000000',
'#888888'
]
}
},
scales: {
x: {display: false},
y: {display: false}
}
}
},
options: {
canvas: {
height: 256,
width: 512
}
}
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

View File

@@ -176,9 +176,14 @@ describe('Chart.helpers.options', function() {
});
it ('should fallback if an indexable option value is undefined', function() {
var input = [42, undefined, 'bar'];
expect(resolve([input], undefined, 5)).toBe(undefined);
expect(resolve([input], undefined, 1)).toBe(undefined);
expect(resolve([input, 'foo'], undefined, 1)).toBe('foo');
expect(resolve([input, 'foo'], undefined, 5)).toBe('foo');
});
it ('should loop if an indexable option index is out of bounds', function() {
var input = [42, undefined, 'bar'];
expect(resolve([input], undefined, 3)).toBe(42);
expect(resolve([input, 'foo'], undefined, 4)).toBe('foo');
expect(resolve([input, 'foo'], undefined, 5)).toBe('bar');
});
it ('should not handle indexable options if index is undefined', function() {
var array = [42, 'foo', 'bar'];
@@ -211,7 +216,7 @@ describe('Chart.helpers.options', function() {
};
expect(resolve([input, 'foo'], {v: 42}, 0)).toBe(42);
expect(resolve([input, 'foo'], {v: 42}, 1)).toBe('foo');
expect(resolve([input, 'foo'], {v: 42}, 5)).toBe('foo');
expect(resolve([input, 'foo'], {v: 42}, 5)).toBe('bar');
expect(resolve([input, ['foo', 'bar']], {v: 42}, 1)).toBe('bar');
});
});