@sit-onyx/chartjs-plugin 1.0.0 → 1.0.1-dev-20261020154757

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/LICENSE.txt CHANGED
@@ -175,7 +175,7 @@
175
175
 
176
176
  END OF TERMS AND CONDITIONS
177
177
 
178
- Copyright 2023 Schwarz IT KG
178
+ Copyright 2023 Schwarz Digits IT KG
179
179
 
180
180
  Licensed under the Apache License, Version 2.0 (the "License");
181
181
  you may not use this file except in compliance with the License.
package/README.md CHANGED
@@ -1,12 +1,12 @@
1
1
  <div align="center" style="text-align: center">
2
- <img alt="onyx logo" src="https://raw.githubusercontent.com/SchwarzIT/onyx/main/.github/onyx-logo.svg" height="96px">
2
+ <img alt="Onyx logo" src="https://raw.githubusercontent.com/SchwarzIT/onyx/main/packages/assets/src/onyx-brand/signet.svg" height="96px">
3
3
  </div>
4
4
 
5
5
  <br>
6
6
 
7
7
  # @sit-onyx/chartjs-plugin
8
8
 
9
- A Chart.js plugin for the onyx design system created by [Schwarz IT](https://it.schwarz).
9
+ A Chart.js plugin for the onyx design system created by [Schwarz Digits](https://schwarz-digits.de).
10
10
 
11
11
  ## Documentation
12
12
 
package/package.json CHANGED
@@ -1,44 +1,45 @@
1
1
  {
2
2
  "name": "@sit-onyx/chartjs-plugin",
3
- "description": "A Chart.js plugin for the onyx design system created by Schwarz IT",
4
- "version": "1.0.0",
5
- "type": "module",
6
- "author": "Schwarz IT KG",
3
+ "version": "1.0.1-dev-20261020154757",
4
+ "description": "A Chart.js plugin for the onyx design system created by Schwarz Digits",
5
+ "homepage": "https://onyx.schwarz/development/packages/chartjs-plugin.html",
6
+ "bugs": {
7
+ "url": "https://github.com/SchwarzIT/onyx/issues"
8
+ },
7
9
  "license": "Apache-2.0",
8
- "engines": {
9
- "node": ">=20"
10
+ "author": "Schwarz Digits IT KG",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/SchwarzIT/onyx",
14
+ "directory": "packages/chartjs-plugin"
10
15
  },
11
16
  "files": [
12
17
  "src"
13
18
  ],
19
+ "type": "module",
14
20
  "types": "./src/index.ts",
15
21
  "exports": {
16
22
  ".": "./src/index.ts",
17
23
  "./examples/*": "./src/stories/*"
18
24
  },
19
- "homepage": "https://onyx.schwarz/development/packages/chartjs-plugin.html",
20
- "repository": {
21
- "type": "git",
22
- "url": "https://github.com/SchwarzIT/onyx",
23
- "directory": "packages/chartjs-plugin"
24
- },
25
- "bugs": {
26
- "url": "https://github.com/SchwarzIT/onyx/issues"
25
+ "devDependencies": {
26
+ "@playwright/experimental-ct-vue": "1.62.1",
27
+ "@playwright/test": "1.62.1",
28
+ "@sit-onyx/shared": "^0.1.0",
29
+ "@sit-onyx/storybook-utils": "^1.1.4",
30
+ "@vue/compiler-dom": "3.5.44",
31
+ "chart.js": "4.5.1",
32
+ "sit-onyx": "^1.21.1-dev-20261020154757",
33
+ "vite": "8.3.4",
34
+ "vue": "3.5.44",
35
+ "vue-chartjs": "^5.3.4"
27
36
  },
28
37
  "peerDependencies": {
29
38
  "chart.js": ">= 4.0.0",
30
- "sit-onyx": "^1.0.0"
39
+ "sit-onyx": "^1.21.1-dev-20261020154757"
31
40
  },
32
- "devDependencies": {
33
- "@playwright/experimental-ct-vue": "1.55.0",
34
- "@playwright/test": "1.55.0",
35
- "@vue/compiler-dom": "3.5.21",
36
- "chart.js": "4.5.0",
37
- "vue": "3.5.21",
38
- "vue-chartjs": "^5.3.2",
39
- "@sit-onyx/shared": "^0.1.0",
40
- "@sit-onyx/storybook-utils": "^1.0.0",
41
- "sit-onyx": "^1.0.0"
41
+ "engines": {
42
+ "node": ">=20"
42
43
  },
43
44
  "scripts": {
44
45
  "dev": "storybook dev -p 6006 --no-open",
package/src/plugin.ts CHANGED
@@ -19,13 +19,13 @@ import { QUANTITATIVE_COLOR_STEPS, getCSSVariableValue, hexToRgb } from "./utils
19
19
  * Styles will be updated accordingly if the application is switched between light/dark mode.
20
20
  *
21
21
  * @example
22
- * ```ts
23
- * import { registerOnyxPlugin } from "@sit-onyx/chartjs-plugin";
24
- * import { Chart, registerables } from "chart.js";
22
+ * ```ts
23
+ * import { registerOnyxPlugin } from "@sit-onyx/chartjs-plugin";
24
+ * import { Chart, registerables } from "chart.js";
25
25
  *
26
- * Chart.register(...registerables);
27
- * registerOnyxPlugin(Chart);
28
- * ```
26
+ * Chart.register(...registerables);
27
+ * registerOnyxPlugin(Chart);
28
+ * ```;
29
29
  */
30
30
  export const registerOnyxPlugin = (chart: typeof Chart) => {
31
31
  //
@@ -49,7 +49,7 @@ export const registerOnyxPlugin = (chart: typeof Chart) => {
49
49
  Object.defineProperty(chart.defaults.font, "family", {
50
50
  configurable: true,
51
51
  enumerable: true,
52
- get: () => getCSSVariableValue("--onyx-font-family"),
52
+ get: () => getCSSVariableValue("--onyx-font-family-paragraph"),
53
53
  });
54
54
 
55
55
  chart.defaults.font.size = 13;
@@ -109,7 +109,9 @@ const plugin: Plugin<ChartType, undefined> = {
109
109
  * of the chart.
110
110
  */
111
111
  beforeLayout: (chart) => {
112
- /** Color offset / index so every dataset gets a unique color. */
112
+ /**
113
+ * Color offset / index so every dataset gets a unique color.
114
+ */
113
115
  let colorOffset = 0;
114
116
 
115
117
  chart.config.data.datasets.forEach((dataset, index) => {
@@ -185,8 +187,9 @@ const createColorGetter = (color: string) => {
185
187
  *
186
188
  * @param dataset Dataset to colorize
187
189
  * @param offset Offset for the colors. Useful if multiple datasets are used inside a chart which
188
- * should have different colors.
189
- * @param allData Set this to true for doughnut, pie and polar area chart type so every data has a separate color.
190
+ * should have different colors.
191
+ * @param allData Set this to true for doughnut, pie and polar area chart type so every data has a
192
+ * separate color.
190
193
  */
191
194
  const colorizeDataset = (dataset: ChartDataset, offset = 0, allData = false) => {
192
195
  const borderColorizer = () => {
package/src/utils.ts CHANGED
@@ -10,18 +10,18 @@ export type QuantitativeColorStep = (typeof QUANTITATIVE_COLOR_STEPS)[number];
10
10
  * Will be updated automatically when switching between light/dark mode.
11
11
  *
12
12
  * @example
13
- * ```ts
14
- * const chartData: ChartData<"line"> = {
15
- * labels: ["A", "B", "C"],
16
- * datasets: [
17
- * {
18
- * label: "Dataset A",
19
- * data: [1, 2, 3],
20
- * ...getDatasetColors("primary"),
21
- * },
22
- * ],
23
- * };
24
- * ```
13
+ * ```ts
14
+ * const chartData: ChartData<"line"> = {
15
+ * labels: ["A", "B", "C"],
16
+ * datasets: [
17
+ * {
18
+ * label: "Dataset A",
19
+ * data: [1, 2, 3],
20
+ * ...getDatasetColors("primary"),
21
+ * },
22
+ * ],
23
+ * };
24
+ * ```;
25
25
  */
26
26
  export const getDatasetColors = (color: OnyxColor | `quantitatives-${QuantitativeColorStep}`) => {
27
27
  const cssVariable = `--onyx-color-base-${color.startsWith("quantitatives-") ? color : `${color}-500`}`;
@@ -37,19 +37,29 @@ export const getDatasetColors = (color: OnyxColor | `quantitatives-${Quantitativ
37
37
 
38
38
  /**
39
39
  * Gets the current value of the given CSS variable.
40
- * @param CSS variable name, e.g. `--onyx-color-base-primary-500`
40
+ *
41
+ * @param CSS Variable name, e.g. `--onyx-color-base-primary-500`
41
42
  */
42
43
 
43
- export const getCSSVariableValue = (variableName: string): string => {
44
- const isDark = getComputedStyle(document.body).colorScheme === "dark";
45
- const computedValue = getComputedStyle(document.body).getPropertyValue(variableName).trim();
46
- const lightDarkMatch = /^light-dark\(([^,]+),\s*([^)]+)\)$/.exec(computedValue);
44
+ export const getCSSVariableValue = (variableName: string, el = document.body): string => {
45
+ const rawValue = getComputedStyle(el).getPropertyValue(variableName).trim();
47
46
 
48
- if (lightDarkMatch) {
49
- return lightDarkMatch[isDark ? 2 : 1]!.trim();
50
- } else {
51
- return computedValue;
47
+ // if the CSS variable is no "color" (e.g. font family or numeric value), return the raw value
48
+ // otherwise we resolve the actual color below
49
+ if (!CSS.supports("color", rawValue)) {
50
+ return rawValue;
52
51
  }
52
+
53
+ const element = el.ownerDocument.createElement("div");
54
+ element.style.display = "none";
55
+ element.style.color = `var(${variableName})`;
56
+ // color scheme is inherited from the parent, so we don't need to handle it manually
57
+ el.appendChild(element);
58
+
59
+ const resolvedColor = getComputedStyle(element).color;
60
+ element.remove();
61
+
62
+ return rgbToHex(resolvedColor);
53
63
  };
54
64
 
55
65
  /**
@@ -62,3 +72,39 @@ export const hexToRgb = (hex: string) => {
62
72
  const b = number & 255;
63
73
  return `${r}, ${g}, ${b}`;
64
74
  };
75
+
76
+ /**
77
+ * Converts an rgb() or rgba() string (comma- or space-separated) to hex.
78
+ *
79
+ * @param {string} rgbValue - e.g. "rgb(255, 0, 128)" or "rgba(0, 0, 0, 0.5)"
80
+ * @returns {string} - e.g. "#ff0080" or "#00000080"
81
+ */
82
+ export function rgbToHex(rgbValue: string) {
83
+ // Extract all numeric components (supports standard numbers and percentages)
84
+ const values = rgbValue.match(/[\d.]+%?/g);
85
+ if (!values || values.length < 3) return rgbValue;
86
+
87
+ const toHex = (val: string, isAlpha = false) => {
88
+ let num = val.endsWith("%") ? (parseFloat(val) / 100) * (isAlpha ? 1 : 255) : parseFloat(val);
89
+
90
+ if (isAlpha) num = Math.round(num * 255);
91
+ else num = Math.round(num);
92
+
93
+ return Math.min(255, Math.max(0, num)).toString(16).padStart(2, "0");
94
+ };
95
+
96
+ const r = toHex(values[0]);
97
+ const g = toHex(values[1]!);
98
+ const b = toHex(values[2]!);
99
+
100
+ // Include 2-digit alpha only if an alpha channel < 1 is present
101
+ let a = "";
102
+ if (values[3] !== undefined) {
103
+ const alphaVal = values[3].endsWith("%") ? parseFloat(values[3]) / 100 : parseFloat(values[3]);
104
+ if (alphaVal < 1) {
105
+ a = toHex(values[3], true);
106
+ }
107
+ }
108
+
109
+ return `#${r}${g}${b}${a}`;
110
+ }