mirror of
https://github.com/chartjs/Chart.js.git
synced 2026-03-06 08:24:05 +01:00
Make indexable options looping (#7442)
* Make indexable options looping * Migration note
This commit is contained in:
committed by
Evert Timberg
parent
b71e4c3c7f
commit
2b57660aba
@@ -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`.
|
||||
|
||||
@@ -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) {
|
||||
|
||||
45
test/fixtures/controller.bar/backgroundColor/loopable.js
vendored
Normal file
45
test/fixtures/controller.bar/backgroundColor/loopable.js
vendored
Normal 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
|
||||
}
|
||||
}
|
||||
};
|
||||
BIN
test/fixtures/controller.bar/backgroundColor/loopable.png
vendored
Normal file
BIN
test/fixtures/controller.bar/backgroundColor/loopable.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.5 KiB |
@@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user