Skip to content

Commit 22e367c

Browse files
authored
Make indexable options looping (#7442)
* Make indexable options looping * Migration note
1 parent c527ec9 commit 22e367c

File tree

5 files changed

+60
-4
lines changed

5 files changed

+60
-4
lines changed

docs/docs/getting-started/v3-migration.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ Chart.js 3.0 introduces a number of breaking changes. Chart.js 2.0 was released
2525

2626
A number of changes were made to the configuration options passed to the `Chart` constructor. Those changes are documented below.
2727

28+
#### Generic changes
29+
30+
* Indexable options are now looping. `backgroundColor: ['red', 'green']` will result in alternating `'red'` / `'green'` if there are more than 2 data points.
31+
32+
#### Specific changes
33+
2834
* `hover.animationDuration` is now configured in `animation.active.duration`
2935
* `responsiveAnimationDuration` is now configured in `animation.resize.duration`
3036
* Polar area `startAngle` option is now consistent with `Radar`, 0 is at top and value is in degrees. Default is changed from `-½π` to `0`.

src/helpers/helpers.options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export function resolve(inputs, context, index, info) {
120120
cacheable = false;
121121
}
122122
if (index !== undefined && isArray(value)) {
123-
value = value[index];
123+
value = value[index % value.length];
124124
cacheable = false;
125125
}
126126
if (value !== undefined) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module.exports = {
2+
config: {
3+
type: 'bar',
4+
data: {
5+
labels: [0, 1, 2, 3, 4, 5],
6+
datasets: [
7+
{
8+
// option in dataset
9+
data: [0, 2, 3, 4, 5, 6],
10+
backgroundColor: [
11+
'#ff0000',
12+
'#00ff00',
13+
'#0000ff'
14+
]
15+
},
16+
{
17+
// option in element (fallback)
18+
data: [6, 5, 4, 3, 2, 1],
19+
}
20+
]
21+
},
22+
options: {
23+
legend: false,
24+
title: false,
25+
elements: {
26+
rectangle: {
27+
backgroundColor: [
28+
'#000000',
29+
'#888888'
30+
]
31+
}
32+
},
33+
scales: {
34+
x: {display: false},
35+
y: {display: false}
36+
}
37+
}
38+
},
39+
options: {
40+
canvas: {
41+
height: 256,
42+
width: 512
43+
}
44+
}
45+
};
6.47 KB
Loading

test/specs/helpers.options.tests.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,14 @@ describe('Chart.helpers.options', function() {
176176
});
177177
it ('should fallback if an indexable option value is undefined', function() {
178178
var input = [42, undefined, 'bar'];
179-
expect(resolve([input], undefined, 5)).toBe(undefined);
179+
expect(resolve([input], undefined, 1)).toBe(undefined);
180180
expect(resolve([input, 'foo'], undefined, 1)).toBe('foo');
181-
expect(resolve([input, 'foo'], undefined, 5)).toBe('foo');
181+
});
182+
it ('should loop if an indexable option index is out of bounds', function() {
183+
var input = [42, undefined, 'bar'];
184+
expect(resolve([input], undefined, 3)).toBe(42);
185+
expect(resolve([input, 'foo'], undefined, 4)).toBe('foo');
186+
expect(resolve([input, 'foo'], undefined, 5)).toBe('bar');
182187
});
183188
it ('should not handle indexable options if index is undefined', function() {
184189
var array = [42, 'foo', 'bar'];
@@ -211,7 +216,7 @@ describe('Chart.helpers.options', function() {
211216
};
212217
expect(resolve([input, 'foo'], {v: 42}, 0)).toBe(42);
213218
expect(resolve([input, 'foo'], {v: 42}, 1)).toBe('foo');
214-
expect(resolve([input, 'foo'], {v: 42}, 5)).toBe('foo');
219+
expect(resolve([input, 'foo'], {v: 42}, 5)).toBe('bar');
215220
expect(resolve([input, ['foo', 'bar']], {v: 42}, 1)).toBe('bar');
216221
});
217222
});

0 commit comments

Comments
 (0)