@mozaic-ds/chart 0.1.0-beta.4 → 0.1.0-beta.5

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.
@@ -1,192 +1,224 @@
1
1
  <template>
2
2
  <div class="container">
3
3
  <div class="main">
4
- <Line
5
- v-if="chartData"
6
- ref="cockpitLineChartRef"
7
- :data="chartData"
8
- :options="options"
9
- :plugins='htmlLegendPlugin'
10
- :chart-id='chartId'
11
- :cssClasses:='cssClasses'
12
- :styles='styles'
13
- :style='{ width, height, cursor:"pointer"}'
14
- />
15
- </div>
16
- <div ref="legendContainer" />
4
+ <Line
5
+ v-if="chartData"
6
+ ref="lineChartRef"
7
+ :id="chartId"
8
+ :data="chartData"
9
+ :options="options"
10
+ :plugins="htmlLegendPlugin"
11
+ :class="cssClasses"
12
+ :style="[{ width, height, cursor: 'pointer' }, styles]"
13
+ />
14
+ </div>
15
+ <div ref="legendContainer" />
17
16
  </div>
18
- </template>
19
-
20
- <script setup lang="ts">
21
-
22
- import type { Ref } from 'vue';
23
- import { computed, ref, PropType } from 'vue';
24
- import { Line } from 'vue-chartjs';
25
- import { LineData } from '../../types/LineChart';
26
- import { GenericTooltipService } from '../../services/GenericTooltipService';
27
- import type { Context } from '../../services/GenericTooltipService';
28
- import { TooltipChartType } from '../../types/TooltipChartType';
29
- import { formatTicks } from '../../services/FormatUtilities';
30
- import type { ChartItem } from '../../services/ChartsCommonLegend';
31
- import ChartDesign from '../../services/patterns/ChartDesign';
32
-
33
- import {
34
- createHtmlLegendItemText,
35
- createHtmlLegendListElement,
36
- createLegendElementWithSquareArea,
37
- getOrCreateLegendList,
38
- createLegendCheckbox,
39
- switchItemVisibility
40
- } from '../../services/ChartsCommonLegend';
41
-
42
- import {
43
- CategoryScale,
44
- Chart as ChartJS,
45
- Legend,
46
- LinearScale,
47
- LineElement,
48
- PointElement,
49
- Title,
50
- Tooltip,
51
- Plugin
52
- } from 'chart.js';
53
-
54
- ChartJS.register(
55
- Title,
56
- Tooltip,
57
- Legend,
58
- PointElement,
59
- LineElement,
60
- CategoryScale,
61
- LinearScale
62
- );
63
-
64
- const {
65
- colourSets,
66
- patternsStandardList
67
- } = ChartDesign();
17
+ </template>
68
18
 
19
+ <script setup lang="ts">
20
+ import type { Ref } from 'vue';
21
+ import { computed, ref, PropType } from 'vue';
22
+ import { Line } from 'vue-chartjs';
23
+ import { LineData } from '../../types/LineChart';
24
+ import { GenericTooltipService } from '../../services/GenericTooltipService';
25
+ import type { Context } from '../../services/GenericTooltipService';
26
+ import { TooltipChartType } from '../../types/TooltipChartType';
27
+ import { formatTicks } from '../../services/FormatUtilities';
28
+ import type { ChartItem } from '../../services/ChartsCommonLegend';
29
+ import ChartDesign from '../../services/patterns/ChartDesign';
69
30
 
70
- const legendContainer = ref(null);
71
- const selectMode = ref(false);
72
- const lineDataProps = defineProps({
73
- chartId: {
74
- type: String,
75
- default: 'radar-chart',
76
- },
77
- rawTitle: {
78
- type: String,
79
- default: 'sales',
80
- },
81
- width: {
82
- type: String,
83
- default: '200px',
84
- },
85
- height: {
86
- type: String,
87
- default: '400px',
88
- },
89
- // disable accessibility patterns
90
- disableAccessibility: {
91
- type: Boolean,
92
- default: false
93
- },
94
- // colourSet props is used to choose the colour set of the charts as defined in the Figma prototypes.
95
- // 7 colour sets are currently defined :
96
- // - Default 0 corresponds to the current one
97
- // - 1 to 6 corresponds to the "new" colour sets : https://www.figma.com/file/Hn6PyvnR385Ta0XN3KqOI9/04.-Dataviz---Documentation-(read-only)?type=design&node-id=1-69316&mode=design&t=sDytQ5BipsryWkuA-0
98
- // Remark : All the sets are defined in /src/services/patterns/ChartDesign.ts
99
- colourSet: {
100
- type: Number,
101
- default: 0
102
- },
103
- // 6 patterns exist and are not randomly given but follow the order defined in patternsStandardList /src/services/patterns/ChartDesign.ts
104
- // Additionally, a pattern has only one possible colour per colour set as defined in the Figma prototype.
105
- // In some use cases, the chart may need to show a different orders of these patterns, this can be changed using the props newPatternsOrder
106
- newPatternsOrder: {
107
- type: Array as PropType<number[]>,
108
- default: () => [0,1,2,3,4,5]
109
- },
110
- lines: {
111
- type: Array as PropType<LineData[]>,
112
- default: () => {},
113
- },
114
- labels: {
115
- type: Array as PropType<string[]>,
116
- default: () => []
117
- },
118
- cssClasses: {
119
- default: '',
120
- type: String,
121
- },
122
- styles: {
123
- type: Object as PropType<Partial<CSSStyleDeclaration>>,
124
- default: () => {},
125
- },
126
- plugins: {
127
- type: Array as PropType<Plugin<'line'>[]>,
128
- default: () => [],
129
- },
130
- })
131
-
132
- const patternsColors = computed(() => lineDataProps.newPatternsOrder.length !== 6
133
- ? colourSets[lineDataProps.colourSet]
134
- : lineDataProps.newPatternsOrder.map((id)=> {return colourSets[lineDataProps.colourSet][id]})
135
- )
31
+ import {
32
+ createHtmlLegendItemText,
33
+ createHtmlLegendListElement,
34
+ createLegendElementWithSquareArea,
35
+ getOrCreateLegendList,
36
+ createLegendCheckbox,
37
+ switchItemVisibility,
38
+ } from '../../services/ChartsCommonLegend';
39
+
40
+ import {
41
+ CategoryScale,
42
+ Chart as ChartJS,
43
+ Legend,
44
+ LinearScale,
45
+ LineElement,
46
+ PointElement,
47
+ Title,
48
+ Tooltip,
49
+ Plugin,
50
+ } from 'chart.js';
51
+
52
+ ChartJS.register(
53
+ Title,
54
+ Tooltip,
55
+ Legend,
56
+ PointElement,
57
+ LineElement,
58
+ CategoryScale,
59
+ LinearScale
60
+ );
61
+
62
+ const { colourSets, patternsStandardList } = ChartDesign();
63
+
64
+ const legendContainer = ref(null);
65
+ const selectMode = ref(false);
66
+ const lineDataProps = defineProps({
67
+ /**
68
+ * Value of the id attribute present on the <canvas> tag element the chart
69
+ */
70
+ chartId: {
71
+ type: String,
72
+ default: 'radar-chart',
73
+ },
74
+ /**
75
+ * Label of the second line in the Tooltip
76
+ */
77
+ rawTitle: {
78
+ type: String,
79
+ default: 'sales',
80
+ },
81
+ /**
82
+ * Value of the `width` css property used to define the width of the <canvas> element
83
+ */
84
+ width: {
85
+ type: String,
86
+ default: '200px',
87
+ },
88
+ /**
89
+ * Value of the `height` css property used to define the height of the <canvas> element
90
+ */
91
+ height: {
92
+ type: String,
93
+ default: '400px',
94
+ },
95
+ /**
96
+ * Disable accessibility patterns
97
+ */
98
+ disableAccessibility: {
99
+ type: Boolean,
100
+ default: false,
101
+ },
102
+ /**
103
+ * Used to choose the colour set of the charts as defined in the Figma prototypes.
104
+ * 7 colour sets are currently defined:
105
+ * - Default 0 corresponds to the current one
106
+ * - 1 to 6 corresponds to the "new" [colour sets](https://www.figma.com/file/Hn6PyvnR385Ta0XN3KqOI9/04.-Dataviz---Documentation-(read-only)?type=design&node-id=1-69316&mode=design&t=sDytQ5BipsryWkuA-0)
107
+ * Note: All the sets are defined in /src/services/patterns/ChartDesign.ts
108
+ */
109
+ colourSet: {
110
+ type: Number,
111
+ default: 0,
112
+ },
113
+ /**
114
+ * 6 patterns exist and are not randomly given but follow the order defined in [patternsStandardList](/src/services/patterns/ChartDesign.ts)
115
+ * Additionally, a pattern has only one possible colour per colour set as defined in the Figma prototype.
116
+ * In some use cases, the chart may need to show a different orders of these patterns, this can be changed using the props newPatternsOrder
117
+ */
118
+ newPatternsOrder: {
119
+ type: Array as PropType<number[]>,
120
+ default: () => [0, 1, 2, 3, 4, 5],
121
+ },
122
+ /**
123
+ * Line data _(can contain `label`, `data` and `unit` keys)_
124
+ */
125
+ lines: {
126
+ type: Array as PropType<LineData[]>,
127
+ default: () => {},
128
+ },
129
+ /**
130
+ * Labels used to label the index axis (default x axes). See [Data structures documentation](https://www.chartjs.org/docs/latest/general/data-structures.html)
131
+ */
132
+ labels: {
133
+ type: Array as PropType<string[]>,
134
+ default: () => [],
135
+ },
136
+ /**
137
+ * Add custom CSS classes to the <canvas> element
138
+ */
139
+ cssClasses: {
140
+ type: String,
141
+ default: undefined,
142
+ },
143
+ /**
144
+ * Add custom CSS styles to the <canvas> element
145
+ */
146
+ styles: {
147
+ type: Object as PropType<Partial<CSSStyleDeclaration>>,
148
+ default: () => {},
149
+ },
150
+ /**
151
+ * Value of the `plugins` key passed to the Chart config
152
+ */
153
+ plugins: {
154
+ type: Array as PropType<Plugin<'line'>[]>,
155
+ default: () => [],
156
+ },
157
+ });
136
158
 
137
- const patternsOrderedList = lineDataProps.newPatternsOrder.length !== 6
138
- ? patternsStandardList
139
- : lineDataProps.newPatternsOrder.map((id)=> {return patternsStandardList[id]})
159
+ const patternsColors = computed(() =>
160
+ lineDataProps.newPatternsOrder.length !== 6
161
+ ? colourSets[lineDataProps.colourSet]
162
+ : lineDataProps.newPatternsOrder.map((id) => {
163
+ return colourSets[lineDataProps.colourSet][id];
164
+ })
165
+ );
140
166
 
141
- const getTooltipData = (context: Context) => {
142
- const datasetIndex = context.tooltip.dataPoints[0].datasetIndex;
143
- const dataIndex = context.tooltip.dataPoints[0].dataIndex;
144
- if (!dataIndex || !datasetIndex) {
145
- return '';
146
- }
147
- const formattedValue = lineDataProps.lines[datasetIndex].data[dataIndex].toFixed(2);
148
- return lineDataProps.lines[datasetIndex].unit ? formattedValue +
149
- ' ' + lineDataProps.lines[datasetIndex].unit
150
- : formattedValue;
167
+ const patternsOrderedList =
168
+ lineDataProps.newPatternsOrder.length !== 6
169
+ ? patternsStandardList
170
+ : lineDataProps.newPatternsOrder.map((id) => {
171
+ return patternsStandardList[id];
172
+ });
173
+
174
+ const getTooltipData = (context: Context) => {
175
+ const datasetIndex = context.tooltip.dataPoints[0].datasetIndex as number;
176
+ const dataIndex = context.tooltip.dataPoints[0].dataIndex as number;
177
+ const formattedValue =
178
+ lineDataProps.lines[datasetIndex].data[dataIndex].toFixed(2);
179
+ return lineDataProps.lines[datasetIndex].unit
180
+ ? formattedValue + ' ' + lineDataProps.lines[datasetIndex].unit
181
+ : formattedValue;
182
+ };
183
+
184
+ const tooltipFirstLine = 'months';
185
+ const tooltipValueAttribute = computed(() => {
186
+ return lineDataProps.rawTitle;
187
+ });
188
+
189
+ const chartData = computed(() => {
190
+ return {
191
+ labels: lineDataProps.labels.map((label: string) => label),
192
+ datasets: [
193
+ {
194
+ type: 'line' as const,
195
+ borderColor: patternsColors.value[0],
196
+ pointStyle: 'rectRot',
197
+ pointBackgroundColor: '#FFFFFF',
198
+ pointRadius: 5,
199
+ label: lineDataProps.lines[0].label,
200
+ data: lineDataProps.lines[0].data,
201
+ borderWidth: 2,
202
+ },
203
+ {
204
+ type: 'line' as const,
205
+ borderColor: patternsColors.value[1],
206
+ pointStyle: 'circle',
207
+ pointBackgroundColor: '#FFFFFF',
208
+ pointRadius: 5,
209
+ label: lineDataProps.lines[1].label,
210
+ data: lineDataProps.lines[1].data,
211
+ borderWidth: 2,
212
+ },
213
+ ],
151
214
  };
152
-
153
- const tooltipFirstLine = 'months';
154
- const tooltipValueAttribute = computed(() => {
155
- return lineDataProps.rawTitle;
156
- });
157
-
158
- const chartData = computed(() => {
159
- return {
160
- labels: lineDataProps.labels.map((label: string) => label),
161
- datasets: [
162
- {
163
- type: 'line' as const,
164
- borderColor: patternsColors.value[0],
165
- pointStyle: 'rectRot',
166
- pointBackgroundColor: '#FFFFFF',
167
- pointRadius: 5,
168
- label: lineDataProps.lines[0].label,
169
- data: lineDataProps.lines[0].data,
170
- borderWidth: 2
171
- },
172
- {
173
- type: 'line' as const,
174
- borderColor: patternsColors.value[1],
175
- pointStyle: 'circle',
176
- pointBackgroundColor: '#FFFFFF',
177
- pointRadius: 5,
178
- label: lineDataProps.lines[1].label,
179
- data: lineDataProps.lines[1].data,
180
- borderWidth: 2
181
- }
182
- ]
183
- };
184
- });
185
-
186
- function getHtmlLegendPlugin(legendContainer: Ref, selectMode: Ref) {
187
- return [{
215
+ });
216
+
217
+ function getHtmlLegendPlugin(legendContainer: Ref, selectMode: Ref) {
218
+ return [
219
+ {
188
220
  id: 'htmlLegend',
189
- afterUpdate (chart: any) {
221
+ afterUpdate(chart: any) {
190
222
  const ul = getOrCreateLegendList(legendContainer, 'column');
191
223
  ul.style.display = 'flex';
192
224
  ul.style.flexDirection = 'row';
@@ -195,23 +227,28 @@ const patternsOrderedList = lineDataProps.newPatternsOrder.length !== 6
195
227
  }
196
228
  const items = chart.options.plugins.legend.labels.generateLabels(chart);
197
229
  items.forEach((item: ChartItem) => {
198
- const li = createHtmlLegendListElement(chart, selectMode, item.datasetIndex);
199
- let liContent: HTMLElement;
200
- if (!selectMode.value) {
201
- liContent = createLegendElementWithSquareArea(item);
202
- } else {
203
- liContent = createLegendElementWithCheckbox(chart, item);
204
- }
205
- li.style.marginRight = '10px';
206
- li.appendChild(liContent);
207
- li.appendChild(createHtmlLegendItemText(item));
208
- ul.appendChild(li);
230
+ const li = createHtmlLegendListElement(
231
+ chart,
232
+ selectMode,
233
+ item.datasetIndex
234
+ );
235
+ let liContent: HTMLElement;
236
+ if (!selectMode.value) {
237
+ liContent = createLegendElementWithSquareArea(item);
238
+ } else {
239
+ liContent = createLegendElementWithCheckbox(chart, item);
240
+ }
241
+ li.style.marginRight = '10px';
242
+ li.appendChild(liContent);
243
+ li.appendChild(createHtmlLegendItemText(item));
244
+ ul.appendChild(li);
209
245
  });
210
- }
211
- }];
212
- }
246
+ },
247
+ },
248
+ ];
249
+ }
213
250
 
214
- function createLegendElementWithCheckbox (chart: any, item: ChartItem) {
251
+ function createLegendElementWithCheckbox(chart: any, item: ChartItem) {
215
252
  const liContent = createLegendCheckbox(chart, item);
216
253
  liContent.onclick = (e: Event) => {
217
254
  switchItemVisibility(chart, item.datasetIndex, selectMode);
@@ -219,70 +256,69 @@ const patternsOrderedList = lineDataProps.newPatternsOrder.length !== 6
219
256
  };
220
257
  return liContent;
221
258
  }
222
-
223
- const htmlLegendPlugin = getHtmlLegendPlugin(legendContainer, selectMode);
224
-
225
- const options = computed(() => ({
226
- responsive: true,
227
- maintainAspectRatio: true,
228
- plugins: {
229
- legend: {
230
- display: false
259
+
260
+ const htmlLegendPlugin = getHtmlLegendPlugin(legendContainer, selectMode);
261
+
262
+ const options = computed(() => ({
263
+ responsive: true,
264
+ maintainAspectRatio: true,
265
+ plugins: {
266
+ legend: {
267
+ display: false,
268
+ },
269
+ tooltip: {
270
+ enabled: false,
271
+ external: function (context: Context) {
272
+ new GenericTooltipService().createTooltip(
273
+ context,
274
+ getTooltipData,
275
+ {
276
+ chartType: TooltipChartType.LINE_CHART,
277
+ firstLineLabel: tooltipFirstLine,
278
+ secondLineLabel: tooltipValueAttribute.value,
279
+ },
280
+ patternsColors.value,
281
+ patternsOrderedList,
282
+ lineDataProps.disableAccessibility
283
+ );
231
284
  },
232
- tooltip: {
233
- enabled: false,
234
- external: function (context: Context) {
235
- new GenericTooltipService().createTooltip(
236
- context,
237
- getTooltipData,
238
- {
239
- chartType: TooltipChartType.LINE_CHART,
240
- firstLineLabel: tooltipFirstLine,
241
- secondLineLabel: tooltipValueAttribute.value
242
- },
243
- patternsColors.value,
244
- patternsOrderedList,
245
- lineDataProps.disableAccessibility
246
- );
247
- }
248
- }
249
285
  },
250
- scales: {
251
- x: {
252
- offset: true,
286
+ },
287
+ scales: {
288
+ x: {
289
+ offset: true,
290
+ },
291
+ y: {
292
+ type: 'linear' as const,
293
+ display: true,
294
+ position: 'left' as const,
295
+ grid: {
296
+ drawOnChartArea: true,
253
297
  },
254
- y: {
255
- type: 'linear' as const,
256
- display: true,
257
- position: 'left' as const,
258
- grid: {
259
- drawOnChartArea: true
298
+ ticks: {
299
+ callback: function (val: number | string) {
300
+ const unit = lineDataProps.lines[0].unit;
301
+ return formatTicks(val as number, unit ? unit : null);
260
302
  },
261
- ticks: {
262
- callback: function (val: number | string) {
263
- const unit = lineDataProps.lines[0].unit;
264
- return formatTicks(val as number, unit ? unit : null);
265
- },
266
- maxTicksLimit: 8,
267
- }
268
- }
269
- }
270
- }));
271
- </script>
272
-
273
- <style scoped>
274
- .container {
275
- -moz-osx-font-smoothing: grayscale;
276
- -webkit-font-smoothing: antialiased;
277
- font-weight: 400;
278
- font-family: 'Roboto', sans-serif;
279
- }
280
- .main {
281
- display: flex;
282
- flex-direction: column;
283
- justify-content: center;
284
- align-items: center;
285
- margin-bottom: 20px;
286
- }
287
- </style>
288
-
303
+ maxTicksLimit: 8,
304
+ },
305
+ },
306
+ },
307
+ }));
308
+ </script>
309
+
310
+ <style scoped>
311
+ .container {
312
+ -moz-osx-font-smoothing: grayscale;
313
+ -webkit-font-smoothing: antialiased;
314
+ font-weight: 400;
315
+ font-family: 'Roboto', sans-serif;
316
+ }
317
+ .main {
318
+ display: flex;
319
+ flex-direction: column;
320
+ justify-content: center;
321
+ align-items: center;
322
+ margin-bottom: 20px;
323
+ }
324
+ </style>
@@ -15,7 +15,7 @@ export const Default = {
15
15
  args: {
16
16
  height: "400px",
17
17
  labels: ["Label 1", "Label 2", "Label 3", "Label 4", "Label 5"],
18
- areas: [
18
+ datasets: [
19
19
  {
20
20
  label: "currentYear",
21
21
  areaData: [
@@ -132,7 +132,7 @@ export const MultipleData = {
132
132
  "Label 8",
133
133
  "Label 9",
134
134
  ],
135
- areas: [
135
+ datasets: [
136
136
  {
137
137
  label: "currentYear",
138
138
  areaData: [