@sit-onyx/chartjs-plugin 1.0.0-alpha.6 → 1.0.0-alpha.60

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 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.6",
4
+ "version": "1.0.0-alpha.60",
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.52"
27
+ "sit-onyx": "^1.0.0-alpha.105"
28
28
  },
29
29
  "devDependencies": {
30
- "vue-chartjs": "^5.3.0"
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
@@ -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
- chart.defaults.plugins.colors.enabled = false;
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.backgroundColor = () => {
55
- return getCSSVariableValue("--onyx-color-base-neutral-900");
56
- };
57
- chart.defaults.plugins.tooltip.titleColor = () => {
58
- return getCSSVariableValue("--onyx-color-text-icons-neutral-inverted");
59
- };
60
- chart.defaults.plugins.tooltip.bodyColor = () => {
61
- return getCSSVariableValue("--onyx-color-text-icons-neutral-inverted");
62
- };
63
-
64
- chart.defaults.plugins.title.color = createColorGetter("--onyx-color-text-icons-neutral-intense");
65
- chart.defaults.plugins.title.font = {
66
- weight: "bold",
67
- size: 16,
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.ticks.backdropColor = () => {
71
- return getCSSVariableValue("--onyx-color-base-background-tinted");
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
package/src/utils.ts CHANGED
@@ -2,6 +2,7 @@ import type { OnyxColor } from "sit-onyx/types";
2
2
 
3
3
  /**
4
4
  * Utility for easily styling a Chart.js dataset with a specific onyx color.
5
+ * Will be updated automatically when switching between light/dark mode.
5
6
  *
6
7
  * @example
7
8
  * ```ts
@@ -21,11 +22,13 @@ export const getDatasetColors = (
21
22
  color: OnyxColor | `quantitatives-${100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900}`,
22
23
  ) => {
23
24
  const cssVariable = `--onyx-color-base-${color.startsWith("quantitatives-") ? color : `${color}-500`}`;
24
- const baseColor = getCSSVariableValue(cssVariable);
25
+ // we use arrow functions here so the value is updated / re-evaluated when
26
+ // switched between light and dark mode
27
+ const borderColor = () => getCSSVariableValue(cssVariable);
25
28
 
26
29
  return {
27
- borderColor: baseColor,
28
- backgroundColor: `rgba(${hexToRgb(baseColor)}, 0.3)`,
30
+ borderColor,
31
+ backgroundColor: () => `rgba(${hexToRgb(borderColor())}, 0.3)`,
29
32
  };
30
33
  };
31
34