Skip to content

Commit ca612bb

Browse files
committed
test: disable colors plugin for tests
1 parent 29a89bc commit ca612bb

File tree

10 files changed

+86
-4
lines changed

10 files changed

+86
-4
lines changed

docs/general/colors.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const data = {
4747
};
4848
```
4949

50-
However, setting colors for each dataset might require additional work that you'd rather not do. In that case, consider using the following plugins with pre-defined or generated palettes.
50+
However, setting colors for each dataset might require additional work that you'd rather not do. In that case, consider using the following plugins with pre-defined or generated palettes.
5151

5252
### Default color series
5353

@@ -67,6 +67,22 @@ import { Colors } from 'chart.js';
6767
Chart.register(Colors);
6868
```
6969

70+
:::tip Note
71+
72+
If you are using UMD version of Chart.js, this plugin will be enabled by default. You can disable it by setting option `enabled` to `false`:
73+
74+
```js
75+
const options = {
76+
plugins: {
77+
colors: {
78+
enabled: false
79+
}
80+
}
81+
};
82+
```
83+
84+
:::
85+
7086
### Advanced color series
7187

7288
If a limited-size series of colors is not enough, you can use [`chartjs-plugin-autocolors`](https://github.com/kurkle/chartjs-plugin-autocolors) that generates an unlimited number of distinct colors for every dataset or for every data point in every dataset:

src/plugins/plugin.colors.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import type {Chart, ChartDataset} from '../types';
22

3+
export interface ColorsPluginOptions {
4+
enabled?: boolean;
5+
}
6+
37
type DatasetColorizer = (dataset: ChartDataset, i: number) => void;
48

59
interface ColorsDescriptor {
@@ -68,7 +72,15 @@ function containsColorsDefinitions(
6872
export default {
6973
id: 'colors',
7074

71-
beforeLayout(chart: Chart) {
75+
defaults: {
76+
enabled: true,
77+
},
78+
79+
beforeLayout(chart: Chart, _args, options: ColorsPluginOptions) {
80+
if (!options.enabled) {
81+
return;
82+
}
83+
7284
const {
7385
type,
7486
options: {elements},

test/fixtures/plugin.colors/bar.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ module.exports = {
1111
data: [10, 2, 3, null, 10, 5]
1212
}
1313
]
14+
},
15+
options: {
16+
plugins: {
17+
colors: {
18+
enabled: true
19+
}
20+
}
1421
}
1522
}
1623
};

test/fixtures/plugin.colors/bubble.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ module.exports = {
77
}, {
88
data: [{x: 18, y: 38, r: 25}]
99
}]
10+
},
11+
options: {
12+
plugins: {
13+
colors: {
14+
enabled: true
15+
}
16+
}
1017
}
1118
}
1219
};

test/fixtures/plugin.colors/doughnut.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ module.exports = {
1010
data: [5, 1, 6, 2, null, 9]
1111
}
1212
]
13+
},
14+
options: {
15+
plugins: {
16+
colors: {
17+
enabled: true
18+
}
19+
}
1320
}
1421
}
1522
};

test/fixtures/plugin.colors/line.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ module.exports = {
1111
data: [10, 2, 3, null, 10, 5]
1212
}
1313
]
14+
},
15+
options: {
16+
plugins: {
17+
colors: {
18+
enabled: true
19+
}
20+
}
1421
}
1522
}
1623
};

test/fixtures/plugin.colors/polarArea.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ module.exports = {
88
data: [0, 2, 4, null, 6, 8]
99
}
1010
]
11+
},
12+
options: {
13+
plugins: {
14+
colors: {
15+
enabled: true
16+
}
17+
}
1118
}
1219
}
1320
};

test/fixtures/plugin.colors/radar.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ module.exports = {
1111
data: [4, -5, -10, null, 10, 5]
1212
}
1313
]
14+
},
15+
options: {
16+
plugins: {
17+
colors: {
18+
enabled: true
19+
}
20+
}
1421
}
1522
}
1623
};

test/fixtures/plugin.colors/scatter.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ module.exports = {
1212
pointRadius: 20,
1313
label: 'dataset2'
1414
}],
15+
},
16+
options: {
17+
plugins: {
18+
colors: {
19+
enabled: true
20+
}
21+
}
1522
}
1623
}
1724
};

test/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ jasmine.triggerMouseEvent = triggerMouseEvent;
2222
// more stable test results.
2323
window.moment.tz.setDefault('Etc/UTC');
2424

25-
beforeEach(function() {
25+
beforeAll(() => {
26+
// Disable colors plugin for tests.
27+
window.Chart.defaults.plugins.colors.enabled = false;
28+
});
29+
30+
beforeEach(() => {
2631
addMatchers();
2732
});
2833

29-
afterEach(function() {
34+
afterEach(() => {
3035
releaseCharts();
3136
});

0 commit comments

Comments
 (0)