@sit-onyx/chartjs-plugin 0.0.0

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.
Files changed (33) hide show
  1. package/LICENSE.txt +190 -0
  2. package/README.md +17 -0
  3. package/package.json +38 -0
  4. package/src/index.ts +2 -0
  5. package/src/plugin.ts +208 -0
  6. package/src/stories/BarChart/BarChart.ct.tsx +8 -0
  7. package/src/stories/BarChart/BarChart.stories.ts +12 -0
  8. package/src/stories/BarChart/BarChart.vue +33 -0
  9. package/src/stories/BubbleChart/BubbleChart.ct.tsx +8 -0
  10. package/src/stories/BubbleChart/BubbleChart.stories.ts +12 -0
  11. package/src/stories/BubbleChart/BubbleChart.vue +36 -0
  12. package/src/stories/DoughnutChart/DoughnutChart.ct.tsx +8 -0
  13. package/src/stories/DoughnutChart/DoughnutChart.stories.ts +12 -0
  14. package/src/stories/DoughnutChart/DoughnutChart.vue +24 -0
  15. package/src/stories/LineChart/LineChart.ct.tsx +8 -0
  16. package/src/stories/LineChart/LineChart.stories.ts +12 -0
  17. package/src/stories/LineChart/LineChart.vue +33 -0
  18. package/src/stories/PieChart/PieChart.ct.tsx +8 -0
  19. package/src/stories/PieChart/PieChart.stories.ts +12 -0
  20. package/src/stories/PieChart/PieChart.vue +24 -0
  21. package/src/stories/PolarAreaChart/PolarAreaChart.ct.tsx +8 -0
  22. package/src/stories/PolarAreaChart/PolarAreaChart.stories.ts +12 -0
  23. package/src/stories/PolarAreaChart/PolarAreaChart.vue +29 -0
  24. package/src/stories/RadarChart/RadarChart.ct.tsx +8 -0
  25. package/src/stories/RadarChart/RadarChart.stories.ts +12 -0
  26. package/src/stories/RadarChart/RadarChart.vue +29 -0
  27. package/src/stories/ScatterChart/ScatterChart.ct.tsx +8 -0
  28. package/src/stories/ScatterChart/ScatterChart.stories.ts +12 -0
  29. package/src/stories/ScatterChart/ScatterChart.vue +42 -0
  30. package/src/stories/data.ts +42 -0
  31. package/src/stories/playwright-utils.tsx +23 -0
  32. package/src/utils.spec.ts +9 -0
  33. package/src/utils.ts +49 -0
package/LICENSE.txt ADDED
@@ -0,0 +1,190 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ Copyright 2023 Schwarz IT KG
179
+
180
+ Licensed under the Apache License, Version 2.0 (the "License");
181
+ you may not use this file except in compliance with the License.
182
+ You may obtain a copy of the License at
183
+
184
+ http://www.apache.org/licenses/LICENSE-2.0
185
+
186
+ Unless required by applicable law or agreed to in writing, software
187
+ distributed under the License is distributed on an "AS IS" BASIS,
188
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189
+ See the License for the specific language governing permissions and
190
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,17 @@
1
+ <div align="center">
2
+ <picture>
3
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/SchwarzIT/onyx/main/.github/onyx-logo-light.svg">
4
+ <source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/SchwarzIT/onyx/main/.github/onyx-logo-dark.svg">
5
+ <img alt="onyx logo" src="https://raw.githubusercontent.com/SchwarzIT/onyx/main/.github/onyx-logo-dark.svg" width="160px">
6
+ </picture>
7
+ </div>
8
+
9
+ <br>
10
+
11
+ # @sit-onyx/chartjs-plugin
12
+
13
+ A Chart.js plugin for the onyx design system created by [Schwarz IT](https://it.schwarz).
14
+
15
+ ## Documentation
16
+
17
+ You can find our documentation [here](https://onyx.schwarz/development/packages/chartjs-plugin.html).
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@sit-onyx/chartjs-plugin",
3
+ "description": "A Chart.js plugin for the onyx design system created by Schwarz IT",
4
+ "version": "0.0.0",
5
+ "type": "module",
6
+ "author": "Schwarz IT KG",
7
+ "license": "Apache-2.0",
8
+ "files": [
9
+ "src"
10
+ ],
11
+ "types": "./src/index.ts",
12
+ "exports": {
13
+ ".": "./src/index.ts"
14
+ },
15
+ "homepage": "https://onyx.schwarz/development/packages/chartjs-plugin.html",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/SchwarzIT/onyx",
19
+ "directory": "packages/chartjs-plugin"
20
+ },
21
+ "bugs": {
22
+ "url": "https://github.com/SchwarzIT/onyx/issues"
23
+ },
24
+ "peerDependencies": {
25
+ "chart.js": ">= 4.0.0",
26
+ "sit-onyx": "^1.0.0-alpha.46"
27
+ },
28
+ "devDependencies": {
29
+ "vue-chartjs": "^5.3.0"
30
+ },
31
+ "scripts": {
32
+ "dev": "storybook dev -p 6006 --no-open",
33
+ "build": "vue-tsc --noEmit",
34
+ "test": "vitest",
35
+ "test:coverage": "vitest run --coverage",
36
+ "test:components": "playwright install && playwright test"
37
+ }
38
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./plugin";
2
+ export * from "./utils";
package/src/plugin.ts ADDED
@@ -0,0 +1,208 @@
1
+ import {
2
+ Chart,
3
+ DoughnutController,
4
+ PieController,
5
+ PolarAreaController,
6
+ type ChartDataset,
7
+ type ChartType,
8
+ type Color,
9
+ type Plugin,
10
+ type ScaleOptionsByType,
11
+ type ScaleTypeRegistry,
12
+ } from "chart.js";
13
+ import { getCSSVariableValue, hexToRgb } from "./utils";
14
+
15
+ /**
16
+ * Registers the onyx Chart.js plugin and updates the default styles (colors, fonts etc.) to match
17
+ * the onyx design system. Will disable Chart.js default "color" plugin to avoid conflicts.
18
+ *
19
+ * Styles will be updated accordingly if the application is switched between light/dark mode.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * import { registerOnyxPlugin } from "@sit-onyx/chartjs-plugin";
24
+ * import { Chart, registerables } from "chart.js";
25
+ *
26
+ * Chart.register(...registerables);
27
+ * registerOnyxPlugin(Chart);
28
+ * ```
29
+ */
30
+ export const registerOnyxPlugin = (chart: typeof Chart) => {
31
+ chart.register(plugin);
32
+ chart.defaults.plugins.colors.enabled = false;
33
+
34
+ //
35
+ // General default styles for all chart types
36
+ //
37
+ chart.defaults.font.family = getCSSVariableValue("--onyx-font-family");
38
+ chart.defaults.font.size = 13;
39
+
40
+ chart.defaults.borderColor = () => getCSSVariableValue("--onyx-color-base-neutral-300");
41
+ chart.defaults.color = () => getCSSVariableValue("--onyx-color-text-icons-neutral-medium");
42
+
43
+ chart.defaults.plugins.tooltip.backgroundColor = () => {
44
+ return getCSSVariableValue("--onyx-color-base-neutral-900");
45
+ };
46
+ chart.defaults.plugins.tooltip.titleColor = () => {
47
+ return getCSSVariableValue("--onyx-color-text-icons-neutral-inverted");
48
+ };
49
+ chart.defaults.plugins.tooltip.bodyColor = () => {
50
+ return getCSSVariableValue("--onyx-color-text-icons-neutral-inverted");
51
+ };
52
+
53
+ chart.defaults.plugins.title.color = createColorGetter("--onyx-color-text-icons-neutral-intense");
54
+ chart.defaults.plugins.title.font = {
55
+ weight: "bold",
56
+ size: 16,
57
+ };
58
+
59
+ chart.defaults.scales.radialLinear.ticks.backdropColor = () => {
60
+ return getCSSVariableValue("--onyx-color-base-background-tinted");
61
+ };
62
+
63
+ Object.entries(chart.defaults.scales).forEach(([key, scale]) => {
64
+ // exclude radialLinear scale because it does not support a title
65
+ if (key === "radialLinear") return;
66
+ const typedScale = scale as ScaleOptionsByType<
67
+ Exclude<keyof ScaleTypeRegistry, "radialLinear">
68
+ >;
69
+
70
+ typedScale.title = {
71
+ ...typedScale.title,
72
+ color: createColorGetter("--onyx-color-text-icons-neutral-soft"),
73
+ font: {
74
+ size: 16,
75
+ },
76
+ };
77
+ });
78
+ };
79
+
80
+ const plugin: Plugin<ChartType, undefined> = {
81
+ id: "onyx",
82
+ /**
83
+ * We use the "beforeUpdate" hook to apply styles to the individual datasets
84
+ * of the chart.
85
+ */
86
+ beforeLayout: (chart) => {
87
+ /** Color offset / index so every dataset gets a unique color. */
88
+ let colorOffset = 0;
89
+
90
+ chart.config.data.datasets.forEach((dataset, index) => {
91
+ const controller = chart.getDatasetMeta(index).controller;
92
+
93
+ if (controller instanceof DoughnutController || controller instanceof PolarAreaController) {
94
+ colorizeDataset(dataset, colorOffset, true);
95
+ colorOffset += dataset.data.length;
96
+ } else if (controller) {
97
+ colorizeDataset(dataset, colorOffset);
98
+ colorOffset++;
99
+ }
100
+ });
101
+
102
+ // due to some reason the doughnut and pie charts do not use the default font size
103
+ // for the scale titles so we need to manually set them here
104
+ const controller = chart.getDatasetMeta(0).controller;
105
+ if (controller instanceof DoughnutController || controller instanceof PieController) {
106
+ Object.values(chart.config.options?.scales ?? {}).forEach((scale) => {
107
+ if (!scale) return;
108
+ const typedScale = scale as ScaleOptionsByType<
109
+ Exclude<keyof ScaleTypeRegistry, "radialLinear">
110
+ >;
111
+
112
+ typedScale.title.font = {
113
+ ...typedScale.title.font,
114
+ size: 16,
115
+ };
116
+ });
117
+ }
118
+ },
119
+ /**
120
+ * Update chart whenever the light/dark mode changes so that the colors are updated
121
+ */
122
+ afterInit: (chart) => {
123
+ const darkModeObserver = new MutationObserver((mutations) => {
124
+ const oldClasses = mutations[0].oldValue ?? "";
125
+ const newClasses = (mutations[0].target as HTMLElement).className;
126
+
127
+ const oldTheme = oldClasses.includes("dark") ? "dark" : "light";
128
+ const newTheme = newClasses.includes("dark") ? "dark" : "light";
129
+
130
+ // update chart so the colors are updated to light/dark mode
131
+ if (oldTheme !== newTheme) chart.update();
132
+ });
133
+
134
+ const observerOptions: MutationObserverInit = {
135
+ attributeFilter: ["class"],
136
+ attributeOldValue: true,
137
+ };
138
+
139
+ // TODO: see https://github.com/SchwarzIT/onyx/issues/618
140
+ // change to media query once the above ticket is implemented
141
+ // for example implementation see: https://css-tricks.com/working-with-javascript-media-queries/#aa-listen-for-changes
142
+ darkModeObserver.observe(document.body, observerOptions);
143
+ darkModeObserver.observe(document.documentElement, observerOptions);
144
+ },
145
+ };
146
+
147
+ /**
148
+ * Some Chart.js color properties are actually typed incorrect
149
+ * because they support getters but are typed as static colors.
150
+ * This function will create a color getter which is type casted as static "Color".
151
+ */
152
+ const createColorGetter = (color: string) => {
153
+ const getter = () => getCSSVariableValue(color);
154
+ return getter as unknown as Color;
155
+ };
156
+
157
+ /**
158
+ * Colorizes the given dataset with colors from the onyx design system.
159
+ *
160
+ * @param dataset Dataset to colorize
161
+ * @param offset Offset for the colors. Useful if multiple datasets are used inside a chart which
162
+ * should have different colors.
163
+ * @param allData Set this to true for doughnut, pie and polar area chart type so every data has a separate color.
164
+ */
165
+ const colorizeDataset = (dataset: ChartDataset, offset = 0, allData = false) => {
166
+ const borderColorizer = () => {
167
+ if (dataset.borderColor) return dataset.borderColor;
168
+ if (allData) return dataset.data.map((_, index) => getBorderColor(offset + index));
169
+ return getBorderColor(offset);
170
+ };
171
+
172
+ const backgroundColorizer = () => {
173
+ if (dataset.backgroundColor) return dataset.backgroundColor;
174
+ if (allData) return dataset.data.map((_, index) => getBackgroundColor(offset + index));
175
+ return getBackgroundColor(offset);
176
+ };
177
+
178
+ dataset.borderColor = borderColorizer();
179
+ dataset.backgroundColor = backgroundColorizer();
180
+ };
181
+
182
+ /**
183
+ * Available colors to choose from for default dataset styles.
184
+ */
185
+ const COLORS = [
186
+ "--onyx-color-base-quantitatives-100",
187
+ "--onyx-color-base-quantitatives-200",
188
+ "--onyx-color-base-quantitatives-300",
189
+ "--onyx-color-base-quantitatives-400",
190
+ "--onyx-color-base-quantitatives-500",
191
+ "--onyx-color-base-quantitatives-600",
192
+ "--onyx-color-base-quantitatives-700",
193
+ "--onyx-color-base-quantitatives-800",
194
+ "--onyx-color-base-quantitatives-900",
195
+ ] as const;
196
+
197
+ /**
198
+ * Gets a border color.
199
+ */
200
+ const getBorderColor = (i: number) => getCSSVariableValue(COLORS[i % COLORS.length]);
201
+
202
+ /**
203
+ * Gets a background color (same as border color + 30% transparency).
204
+ */
205
+ const getBackgroundColor = (i: number) => {
206
+ const color = getBorderColor(i);
207
+ return `rgba(${hexToRgb(color)}, 0.3)`;
208
+ };
@@ -0,0 +1,8 @@
1
+ import { test } from "@playwright/experimental-ct-vue";
2
+ import { executeChartScreenshotTest } from "../playwright-utils";
3
+ import BarChart from "./BarChart.vue";
4
+
5
+ test("should render bar chart", async ({ mount, page }) => {
6
+ const component = await mount(BarChart);
7
+ await executeChartScreenshotTest(page, component, "bar");
8
+ });
@@ -0,0 +1,12 @@
1
+ import type { Meta, StoryObj } from "@storybook/vue3";
2
+ import BarChart from "./BarChart.vue";
3
+
4
+ const meta: Meta<typeof BarChart> = {
5
+ title: "examples/BarChart",
6
+ component: BarChart,
7
+ };
8
+
9
+ export default meta;
10
+ type Story = StoryObj<typeof BarChart>;
11
+
12
+ export const Default = { args: {} } satisfies Story;
@@ -0,0 +1,33 @@
1
+ <script lang="ts" setup>
2
+ import type { ChartData } from "chart.js";
3
+ import { Bar } from "vue-chartjs";
4
+ import { chartOptions } from "../data";
5
+
6
+ const labels = ["01.01.2024", "01.02.2024", "01.03.2024", "01.04.2024", "01.05.2024"];
7
+
8
+ const chartData: ChartData<"bar"> = {
9
+ labels,
10
+ datasets: [
11
+ {
12
+ label: "Dataset A",
13
+ data: [1, 8, 4, 6, 2],
14
+ borderWidth: 2,
15
+ },
16
+ {
17
+ label: "Dataset B",
18
+ data: [4, 2, 3, 1, 7],
19
+ borderWidth: 2,
20
+ },
21
+ ],
22
+ };
23
+ </script>
24
+
25
+ <template>
26
+ <Bar :data="chartData" :options="chartOptions" class="chart" />
27
+ </template>
28
+
29
+ <style lang="scss" scoped>
30
+ .chart {
31
+ height: 32rem;
32
+ }
33
+ </style>
@@ -0,0 +1,8 @@
1
+ import { test } from "@playwright/experimental-ct-vue";
2
+ import { executeChartScreenshotTest } from "../playwright-utils";
3
+ import BubbleChart from "./BubbleChart.vue";
4
+
5
+ test("should render bubble chart", async ({ mount, page }) => {
6
+ const component = await mount(BubbleChart);
7
+ await executeChartScreenshotTest(page, component, "bubble");
8
+ });
@@ -0,0 +1,12 @@
1
+ import type { Meta, StoryObj } from "@storybook/vue3";
2
+ import BubbleChart from "./BubbleChart.vue";
3
+
4
+ const meta: Meta<typeof BubbleChart> = {
5
+ title: "examples/BubbleChart",
6
+ component: BubbleChart,
7
+ };
8
+
9
+ export default meta;
10
+ type Story = StoryObj<typeof BubbleChart>;
11
+
12
+ export const Default = { args: {} } satisfies Story;
@@ -0,0 +1,36 @@
1
+ <script lang="ts" setup>
2
+ import type { ChartData } from "chart.js";
3
+ import { Bubble } from "vue-chartjs";
4
+ import { chartOptions } from "../data";
5
+
6
+ const chartData: ChartData<"bubble"> = {
7
+ datasets: [
8
+ {
9
+ label: "Dataset A",
10
+ data: [
11
+ { x: 20, y: 25, r: 5 },
12
+ { x: 40, y: 10, r: 10 },
13
+ { x: 30, y: 22, r: 30 },
14
+ ],
15
+ },
16
+ {
17
+ label: "Dataset B",
18
+ data: [
19
+ { x: 10, y: 30, r: 15 },
20
+ { x: 20, y: 20, r: 10 },
21
+ { x: 15, y: 8, r: 30 },
22
+ ],
23
+ },
24
+ ],
25
+ };
26
+ </script>
27
+
28
+ <template>
29
+ <Bubble :data="chartData" :options="chartOptions" class="chart" />
30
+ </template>
31
+
32
+ <style lang="scss" scoped>
33
+ .chart {
34
+ height: 32rem;
35
+ }
36
+ </style>
@@ -0,0 +1,8 @@
1
+ import { test } from "@playwright/experimental-ct-vue";
2
+ import { executeChartScreenshotTest } from "../playwright-utils";
3
+ import DoughnutChart from "./DoughnutChart.vue";
4
+
5
+ test("should render doughnut chart", async ({ mount, page }) => {
6
+ const component = await mount(DoughnutChart);
7
+ await executeChartScreenshotTest(page, component, "doughnut");
8
+ });
@@ -0,0 +1,12 @@
1
+ import type { Meta, StoryObj } from "@storybook/vue3";
2
+ import DoughnutChart from "./DoughnutChart.vue";
3
+
4
+ const meta: Meta<typeof DoughnutChart> = {
5
+ title: "examples/DoughnutChart",
6
+ component: DoughnutChart,
7
+ };
8
+
9
+ export default meta;
10
+ type Story = StoryObj<typeof DoughnutChart>;
11
+
12
+ export const Default = { args: {} } satisfies Story;
@@ -0,0 +1,24 @@
1
+ <script lang="ts" setup>
2
+ import type { ChartData } from "chart.js";
3
+ import { Doughnut } from "vue-chartjs";
4
+ import { chartOptions } from "../data";
5
+
6
+ const chartData: ChartData<"doughnut"> = {
7
+ labels: ["VueJs", "EmberJs", "ReactJs", "AngularJs"],
8
+ datasets: [
9
+ {
10
+ data: [40, 20, 80, 10],
11
+ },
12
+ ],
13
+ };
14
+ </script>
15
+
16
+ <template>
17
+ <Doughnut :data="chartData" :options="chartOptions" class="chart" />
18
+ </template>
19
+
20
+ <style lang="scss" scoped>
21
+ .chart {
22
+ height: 32rem;
23
+ }
24
+ </style>
@@ -0,0 +1,8 @@
1
+ import { test } from "@playwright/experimental-ct-vue";
2
+ import { executeChartScreenshotTest } from "../playwright-utils";
3
+ import LineChart from "./LineChart.vue";
4
+
5
+ test("should render line chart", async ({ mount, page }) => {
6
+ const component = await mount(LineChart);
7
+ await executeChartScreenshotTest(page, component, "line");
8
+ });
@@ -0,0 +1,12 @@
1
+ import type { Meta, StoryObj } from "@storybook/vue3";
2
+ import LineChart from "./LineChart.vue";
3
+
4
+ const meta: Meta<typeof LineChart> = {
5
+ title: "examples/LineChart",
6
+ component: LineChart,
7
+ };
8
+
9
+ export default meta;
10
+ type Story = StoryObj<typeof LineChart>;
11
+
12
+ export const Default = { args: {} } satisfies Story;
@@ -0,0 +1,33 @@
1
+ <script lang="ts" setup>
2
+ import type { ChartData } from "chart.js";
3
+ import { Line } from "vue-chartjs";
4
+ import { chartOptions } from "../data";
5
+
6
+ const labels = ["01.01.2024", "01.02.2024", "01.03.2024", "01.04.2024", "01.05.2024"];
7
+
8
+ const chartData: ChartData<"line"> = {
9
+ labels,
10
+ datasets: [
11
+ {
12
+ label: "Dataset A",
13
+ fill: true,
14
+ data: [1, 8, 4, 6, 2],
15
+ },
16
+ {
17
+ label: "Dataset B",
18
+ fill: true,
19
+ data: [4, 2, 3, 1, 7],
20
+ },
21
+ ],
22
+ };
23
+ </script>
24
+
25
+ <template>
26
+ <Line :data="chartData" :options="chartOptions" class="chart" />
27
+ </template>
28
+
29
+ <style lang="scss" scoped>
30
+ .chart {
31
+ height: 32rem;
32
+ }
33
+ </style>
@@ -0,0 +1,8 @@
1
+ import { test } from "@playwright/experimental-ct-vue";
2
+ import { executeChartScreenshotTest } from "../playwright-utils";
3
+ import PieChart from "./PieChart.vue";
4
+
5
+ test("should render pie chart", async ({ mount, page }) => {
6
+ const component = await mount(PieChart);
7
+ await executeChartScreenshotTest(page, component, "pie");
8
+ });
@@ -0,0 +1,12 @@
1
+ import type { Meta, StoryObj } from "@storybook/vue3";
2
+ import PieChart from "./PieChart.vue";
3
+
4
+ const meta: Meta<typeof PieChart> = {
5
+ title: "examples/PieChart",
6
+ component: PieChart,
7
+ };
8
+
9
+ export default meta;
10
+ type Story = StoryObj<typeof PieChart>;
11
+
12
+ export const Default = { args: {} } satisfies Story;
@@ -0,0 +1,24 @@
1
+ <script lang="ts" setup>
2
+ import type { ChartData } from "chart.js";
3
+ import { Pie } from "vue-chartjs";
4
+ import { chartOptions } from "../data";
5
+
6
+ const chartData: ChartData<"pie"> = {
7
+ labels: ["VueJs", "EmberJs", "ReactJs", "AngularJs"],
8
+ datasets: [
9
+ {
10
+ data: [40, 20, 80, 10],
11
+ },
12
+ ],
13
+ };
14
+ </script>
15
+
16
+ <template>
17
+ <Pie :data="chartData" :options="chartOptions" class="chart" />
18
+ </template>
19
+
20
+ <style lang="scss" scoped>
21
+ .chart {
22
+ height: 32rem;
23
+ }
24
+ </style>
@@ -0,0 +1,8 @@
1
+ import { test } from "@playwright/experimental-ct-vue";
2
+ import { executeChartScreenshotTest } from "../playwright-utils";
3
+ import PolarAreaChart from "./PolarAreaChart.vue";
4
+
5
+ test("should render polar area chart", async ({ mount, page }) => {
6
+ const component = await mount(PolarAreaChart);
7
+ await executeChartScreenshotTest(page, component, "polar-area");
8
+ });
@@ -0,0 +1,12 @@
1
+ import type { Meta, StoryObj } from "@storybook/vue3";
2
+ import PolarAreaChart from "./PolarAreaChart.vue";
3
+
4
+ const meta: Meta<typeof PolarAreaChart> = {
5
+ title: "examples/PolarAreaChart",
6
+ component: PolarAreaChart,
7
+ };
8
+
9
+ export default meta;
10
+ type Story = StoryObj<typeof PolarAreaChart>;
11
+
12
+ export const Default = { args: {} } satisfies Story;
@@ -0,0 +1,29 @@
1
+ <script lang="ts" setup>
2
+ import type { ChartData } from "chart.js";
3
+ import { PolarArea } from "vue-chartjs";
4
+ import { chartOptionsWithoutScales } from "../data";
5
+
6
+ const chartData: ChartData<"polarArea"> = {
7
+ labels: ["Eating", "Drinking", "Sleeping", "Designing", "Coding", "Cycling", "Running"],
8
+ datasets: [
9
+ {
10
+ label: "My First dataset",
11
+ data: [65, 59, 90, 81, 56, 55, 40],
12
+ },
13
+ {
14
+ label: "My Second dataset",
15
+ data: [28, 48, 40, 19, 96, 27, 100],
16
+ },
17
+ ],
18
+ };
19
+ </script>
20
+
21
+ <template>
22
+ <PolarArea :data="chartData" :options="chartOptionsWithoutScales" class="chart" />
23
+ </template>
24
+
25
+ <style lang="scss" scoped>
26
+ .chart {
27
+ height: 32rem;
28
+ }
29
+ </style>
@@ -0,0 +1,8 @@
1
+ import { test } from "@playwright/experimental-ct-vue";
2
+ import { executeChartScreenshotTest } from "../playwright-utils";
3
+ import RadarChart from "./RadarChart.vue";
4
+
5
+ test("should render radar chart", async ({ mount, page }) => {
6
+ const component = await mount(RadarChart);
7
+ await executeChartScreenshotTest(page, component, "radar");
8
+ });
@@ -0,0 +1,12 @@
1
+ import type { Meta, StoryObj } from "@storybook/vue3";
2
+ import RadarChart from "./RadarChart.vue";
3
+
4
+ const meta: Meta<typeof RadarChart> = {
5
+ title: "examples/RadarChart",
6
+ component: RadarChart,
7
+ };
8
+
9
+ export default meta;
10
+ type Story = StoryObj<typeof RadarChart>;
11
+
12
+ export const Default = { args: {} } satisfies Story;
@@ -0,0 +1,29 @@
1
+ <script lang="ts" setup>
2
+ import type { ChartData } from "chart.js";
3
+ import { Radar } from "vue-chartjs";
4
+ import { chartOptionsWithoutScales } from "../data";
5
+
6
+ const chartData: ChartData<"radar"> = {
7
+ labels: ["Eating", "Drinking", "Sleeping", "Designing", "Coding", "Cycling", "Running"],
8
+ datasets: [
9
+ {
10
+ label: "Dataset A",
11
+ data: [65, 59, 90, 81, 56, 55, 40],
12
+ },
13
+ {
14
+ label: "Dataset B",
15
+ data: [28, 48, 40, 19, 96, 27, 100],
16
+ },
17
+ ],
18
+ };
19
+ </script>
20
+
21
+ <template>
22
+ <Radar :data="chartData" :options="chartOptionsWithoutScales" class="chart" />
23
+ </template>
24
+
25
+ <style lang="scss" scoped>
26
+ .chart {
27
+ height: 32rem;
28
+ }
29
+ </style>
@@ -0,0 +1,8 @@
1
+ import { test } from "@playwright/experimental-ct-vue";
2
+ import { executeChartScreenshotTest } from "../playwright-utils";
3
+ import ScatterChart from "./ScatterChart.vue";
4
+
5
+ test("should render scatter chart", async ({ mount, page }) => {
6
+ const component = await mount(ScatterChart);
7
+ await executeChartScreenshotTest(page, component, "scatter");
8
+ });
@@ -0,0 +1,12 @@
1
+ import type { Meta, StoryObj } from "@storybook/vue3";
2
+ import ScatterChart from "./ScatterChart.vue";
3
+
4
+ const meta: Meta<typeof ScatterChart> = {
5
+ title: "examples/ScatterChart",
6
+ component: ScatterChart,
7
+ };
8
+
9
+ export default meta;
10
+ type Story = StoryObj<typeof ScatterChart>;
11
+
12
+ export const Default = { args: {} } satisfies Story;
@@ -0,0 +1,42 @@
1
+ <script lang="ts" setup>
2
+ import type { ChartData } from "chart.js";
3
+ import { Scatter } from "vue-chartjs";
4
+ import { chartOptions } from "../data";
5
+
6
+ const chartData: ChartData<"scatter"> = {
7
+ datasets: [
8
+ {
9
+ label: "Dataset A",
10
+ fill: false,
11
+ data: [
12
+ { x: -2, y: 4 },
13
+ { x: -1, y: 1 },
14
+ { x: 0, y: 0 },
15
+ { x: 1, y: 1 },
16
+ { x: 2, y: 4 },
17
+ ],
18
+ },
19
+ {
20
+ label: "Dataset B",
21
+ fill: false,
22
+ data: [
23
+ { x: -2, y: -4 },
24
+ { x: -1, y: -1 },
25
+ { x: 0, y: 1 },
26
+ { x: 1, y: -1 },
27
+ { x: 2, y: -4 },
28
+ ],
29
+ },
30
+ ],
31
+ };
32
+ </script>
33
+
34
+ <template>
35
+ <Scatter :data="chartData" :options="chartOptions" class="chart" />
36
+ </template>
37
+
38
+ <style lang="scss" scoped>
39
+ .chart {
40
+ height: 32rem;
41
+ }
42
+ </style>
@@ -0,0 +1,42 @@
1
+ import type { ChartOptions } from "chart.js";
2
+
3
+ /**
4
+ * Shared chart options for all example charts (without scales for polar area and radar chart).
5
+ */
6
+ export const chartOptionsWithoutScales = {
7
+ responsive: true,
8
+ maintainAspectRatio: false,
9
+ hover: {
10
+ intersect: false,
11
+ },
12
+ plugins: {
13
+ title: {
14
+ display: true,
15
+ text: "Example title",
16
+ },
17
+ tooltip: {
18
+ intersect: false,
19
+ },
20
+ },
21
+ } satisfies ChartOptions;
22
+
23
+ /**
24
+ * Shared chart options for all example charts.
25
+ */
26
+ export const chartOptions = {
27
+ ...chartOptionsWithoutScales,
28
+ scales: {
29
+ x: {
30
+ title: {
31
+ display: true,
32
+ text: "x scale label",
33
+ },
34
+ },
35
+ y: {
36
+ title: {
37
+ display: true,
38
+ text: "y scale label",
39
+ },
40
+ },
41
+ },
42
+ } satisfies ChartOptions;
@@ -0,0 +1,23 @@
1
+ import { expect, type MountResult } from "@playwright/experimental-ct-vue";
2
+ import type { Page } from "@playwright/test";
3
+
4
+ export const switchTheme = (page: Page, theme: string) =>
5
+ page.evaluate((theme) => document.documentElement.classList.add(theme), theme);
6
+
7
+ export const executeChartScreenshotTest = async (
8
+ page: Page,
9
+ component: MountResult<unknown>,
10
+ screenshotName: string,
11
+ ) => {
12
+ // hover so that the tooltip is shown for the screenshot
13
+ await component.hover();
14
+
15
+ // ASSERT
16
+ await expect(component).toHaveScreenshot(`${screenshotName}-light.png`);
17
+
18
+ // ACT
19
+ await switchTheme(page, "dark");
20
+
21
+ // ASSERT
22
+ await expect(component).toHaveScreenshot(`${screenshotName}-dark.png`);
23
+ };
@@ -0,0 +1,9 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { hexToRgb } from "./utils";
3
+
4
+ describe("utils.ts", () => {
5
+ test("should convert hex to rgb", () => {
6
+ const rgb = hexToRgb("#0c2238");
7
+ expect(rgb).toBe("12, 34, 56");
8
+ });
9
+ });
package/src/utils.ts ADDED
@@ -0,0 +1,49 @@
1
+ import type { OnyxColor } from "sit-onyx/types";
2
+
3
+ /**
4
+ * Utility for easily styling a Chart.js dataset with a specific onyx color.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * const chartData: ChartData<"line"> = {
9
+ * labels: ["A", "B", "C"],
10
+ * datasets: [
11
+ * {
12
+ * label: "Dataset A",
13
+ * data: [1, 2, 3],
14
+ * ...getDatasetColors("primary"),
15
+ * },
16
+ * ],
17
+ * };
18
+ * ```
19
+ */
20
+ export const getDatasetColors = (
21
+ color: OnyxColor | `quantitatives-${100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900}`,
22
+ ) => {
23
+ const cssVariable = `--onyx-color-base-${color.startsWith("quantitatives-") ? color : `${color}-500`}`;
24
+ const baseColor = getCSSVariableValue(cssVariable);
25
+
26
+ return {
27
+ borderColor: baseColor,
28
+ backgroundColor: `rgba(${hexToRgb(baseColor)}, 0.3)`,
29
+ };
30
+ };
31
+
32
+ /**
33
+ * Gets the current value of the given CSS variable.
34
+ * @param CSS variable name, e.g. `--onyx-color-base-primary-500`
35
+ */
36
+ export const getCSSVariableValue = (variableName: string) => {
37
+ return getComputedStyle(document.body).getPropertyValue(variableName);
38
+ };
39
+
40
+ /**
41
+ * Converts the given HEX color to rgb.
42
+ */
43
+ export const hexToRgb = (hex: string) => {
44
+ const number = parseInt(hex.replace("#", ""), 16);
45
+ const r = (number >> 16) & 255;
46
+ const g = (number >> 8) & 255;
47
+ const b = number & 255;
48
+ return `${r}, ${g}, ${b}`;
49
+ };