@sit-onyx/chartjs-plugin 1.0.0-alpha.7 → 1.0.0-alpha.70
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/plugin.ts +37 -31
- package/src/utils.ts +12 -6
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sit-onyx/chartjs-plugin",
|
|
3
3
|
"description": "A Chart.js plugin for the onyx design system created by Schwarz IT",
|
|
4
|
-
"version": "1.0.0-alpha.
|
|
4
|
+
"version": "1.0.0-alpha.70",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "Schwarz IT KG",
|
|
7
7
|
"license": "Apache-2.0",
|
|
@@ -24,10 +24,10 @@
|
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
26
|
"chart.js": ">= 4.0.0",
|
|
27
|
-
"sit-onyx": "^1.0.0-alpha.
|
|
27
|
+
"sit-onyx": "^1.0.0-alpha.114"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"vue-chartjs": "^5.3.
|
|
30
|
+
"vue-chartjs": "^5.3.1"
|
|
31
31
|
},
|
|
32
32
|
"scripts": {
|
|
33
33
|
"dev": "storybook dev -p 6006 --no-open",
|
package/src/plugin.ts
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
type ScaleOptionsByType,
|
|
11
11
|
type ScaleTypeRegistry,
|
|
12
12
|
} from "chart.js";
|
|
13
|
-
import { getCSSVariableValue, hexToRgb } from "./utils";
|
|
13
|
+
import { QUANTITATIVE_COLOR_STEPS, getCSSVariableValue, hexToRgb } from "./utils";
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Registers the onyx Chart.js plugin and updates the default styles (colors, fonts etc.) to match
|
|
@@ -34,8 +34,14 @@ export const registerOnyxPlugin = (chart: typeof Chart) => {
|
|
|
34
34
|
// or a getter defined with "Object.defineProperty".
|
|
35
35
|
// Otherwise if called directly, this is not compatible with server side rendering.
|
|
36
36
|
//
|
|
37
|
+
// Also if setting any plugin options here (e.g. tooltip), make sure to check that they
|
|
38
|
+
// are defined/register to prevent "can not set/read of undefined" errors
|
|
39
|
+
//
|
|
37
40
|
chart.register(plugin);
|
|
38
|
-
|
|
41
|
+
|
|
42
|
+
if (chart.defaults.plugins.colors) {
|
|
43
|
+
chart.defaults.plugins.colors.enabled = false;
|
|
44
|
+
}
|
|
39
45
|
|
|
40
46
|
//
|
|
41
47
|
// General default styles for all chart types
|
|
@@ -51,25 +57,33 @@ export const registerOnyxPlugin = (chart: typeof Chart) => {
|
|
|
51
57
|
chart.defaults.borderColor = () => getCSSVariableValue("--onyx-color-base-neutral-300");
|
|
52
58
|
chart.defaults.color = () => getCSSVariableValue("--onyx-color-text-icons-neutral-medium");
|
|
53
59
|
|
|
54
|
-
chart.defaults.plugins.tooltip
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
60
|
+
if (chart.defaults.plugins.tooltip) {
|
|
61
|
+
chart.defaults.plugins.tooltip.backgroundColor = () => {
|
|
62
|
+
return getCSSVariableValue("--onyx-color-base-neutral-900");
|
|
63
|
+
};
|
|
64
|
+
chart.defaults.plugins.tooltip.titleColor = () => {
|
|
65
|
+
return getCSSVariableValue("--onyx-color-text-icons-neutral-inverted");
|
|
66
|
+
};
|
|
67
|
+
chart.defaults.plugins.tooltip.bodyColor = () => {
|
|
68
|
+
return getCSSVariableValue("--onyx-color-text-icons-neutral-inverted");
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (chart.defaults.plugins.title) {
|
|
73
|
+
chart.defaults.plugins.title.color = createColorGetter(
|
|
74
|
+
"--onyx-color-text-icons-neutral-intense",
|
|
75
|
+
);
|
|
76
|
+
chart.defaults.plugins.title.font = {
|
|
77
|
+
weight: "bold",
|
|
78
|
+
size: 16,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
69
81
|
|
|
70
|
-
chart.defaults.scales.radialLinear
|
|
71
|
-
|
|
72
|
-
|
|
82
|
+
if (chart.defaults.scales.radialLinear) {
|
|
83
|
+
chart.defaults.scales.radialLinear.ticks.backdropColor = () => {
|
|
84
|
+
return getCSSVariableValue("--onyx-color-base-background-tinted");
|
|
85
|
+
};
|
|
86
|
+
}
|
|
73
87
|
|
|
74
88
|
Object.entries(chart.defaults.scales).forEach(([key, scale]) => {
|
|
75
89
|
// exclude radialLinear scale because it does not support a title
|
|
@@ -193,17 +207,9 @@ const colorizeDataset = (dataset: ChartDataset, offset = 0, allData = false) =>
|
|
|
193
207
|
/**
|
|
194
208
|
* Available colors to choose from for default dataset styles.
|
|
195
209
|
*/
|
|
196
|
-
const COLORS =
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
"--onyx-color-base-quantitatives-300",
|
|
200
|
-
"--onyx-color-base-quantitatives-400",
|
|
201
|
-
"--onyx-color-base-quantitatives-500",
|
|
202
|
-
"--onyx-color-base-quantitatives-600",
|
|
203
|
-
"--onyx-color-base-quantitatives-700",
|
|
204
|
-
"--onyx-color-base-quantitatives-800",
|
|
205
|
-
"--onyx-color-base-quantitatives-900",
|
|
206
|
-
] as const;
|
|
210
|
+
const COLORS = QUANTITATIVE_COLOR_STEPS.map(
|
|
211
|
+
(step) => `--onyx-color-base-quantitatives-${step}` as const,
|
|
212
|
+
);
|
|
207
213
|
|
|
208
214
|
/**
|
|
209
215
|
* Gets a border color.
|
package/src/utils.ts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import type { OnyxColor } from "sit-onyx/types";
|
|
2
2
|
|
|
3
|
+
export const QUANTITATIVE_COLOR_STEPS = [
|
|
4
|
+
100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200,
|
|
5
|
+
] as const;
|
|
6
|
+
export type QuantitativeColorStep = (typeof QUANTITATIVE_COLOR_STEPS)[number];
|
|
7
|
+
|
|
3
8
|
/**
|
|
4
9
|
* Utility for easily styling a Chart.js dataset with a specific onyx color.
|
|
10
|
+
* Will be updated automatically when switching between light/dark mode.
|
|
5
11
|
*
|
|
6
12
|
* @example
|
|
7
13
|
* ```ts
|
|
@@ -17,15 +23,15 @@ import type { OnyxColor } from "sit-onyx/types";
|
|
|
17
23
|
* };
|
|
18
24
|
* ```
|
|
19
25
|
*/
|
|
20
|
-
export const getDatasetColors = (
|
|
21
|
-
color: OnyxColor | `quantitatives-${100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900}`,
|
|
22
|
-
) => {
|
|
26
|
+
export const getDatasetColors = (color: OnyxColor | `quantitatives-${QuantitativeColorStep}`) => {
|
|
23
27
|
const cssVariable = `--onyx-color-base-${color.startsWith("quantitatives-") ? color : `${color}-500`}`;
|
|
24
|
-
|
|
28
|
+
// we use arrow functions here so the value is updated / re-evaluated when
|
|
29
|
+
// switched between light and dark mode
|
|
30
|
+
const borderColor = () => getCSSVariableValue(cssVariable);
|
|
25
31
|
|
|
26
32
|
return {
|
|
27
|
-
borderColor
|
|
28
|
-
backgroundColor: `rgba(${hexToRgb(
|
|
33
|
+
borderColor,
|
|
34
|
+
backgroundColor: () => `rgba(${hexToRgb(borderColor())}, 0.3)`,
|
|
29
35
|
};
|
|
30
36
|
};
|
|
31
37
|
|