@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.
@@ -2,17 +2,18 @@
2
2
  <div class="container">
3
3
  <div class="main">
4
4
  <Doughnut
5
- v-if="doughnutChartData"
6
- ref='doughnutRef'
7
- :data='doughnutChartData as ChartData<"doughnut", number[], unknown>'
8
- :options='options'
9
- :plugins='htmlLegendPlugin'
10
- :cssClasses:="cssClasses"
11
- :max-values="maxValues"
12
- :style='{width, height, cursor:"pointer"}'
13
- />
14
- </div>
15
- <div ref="legendContainer" />
5
+ v-if="doughnutChartData"
6
+ ref="doughnutRef"
7
+ :id="chartId"
8
+ :data="doughnutChartData"
9
+ :options="options"
10
+ :plugins="htmlLegendPlugin"
11
+ :max-values="maxValues"
12
+ :class="cssClasses"
13
+ :style="[{ width, height, cursor: 'pointer' }, styles]"
14
+ />
15
+ </div>
16
+ <div ref="legendContainer" />
16
17
  </div>
17
18
  </template>
18
19
 
@@ -35,19 +36,11 @@ import {
35
36
  Tooltip,
36
37
  Legend,
37
38
  ArcElement,
38
- Plugin
39
+ Plugin,
39
40
  } from 'chart.js';
40
- ChartJS.register(
41
- Title,
42
- Tooltip,
43
- Legend,
44
- ArcElement
45
- );
41
+ ChartJS.register(Title, Tooltip, Legend, ArcElement);
46
42
 
47
- const {
48
- colourSets,
49
- patternsStandardList
50
- } = ChartDesign();
43
+ const { colourSets, patternsStandardList } = ChartDesign();
51
44
 
52
45
  const {
53
46
  onHoverIndex,
@@ -57,117 +50,157 @@ const {
57
50
  getOnHoverOptions,
58
51
  groupDataAfterNthValue,
59
52
  getDoughnutLabels,
60
- getBorderColor
53
+ getBorderColor,
61
54
  } = doughnutChartFunction();
62
55
  const selectMode = ref(false);
63
56
  const legendContainer = ref<HTMLElement | null>(null);
64
57
 
65
58
  const doughnutDataProps = defineProps({
59
+ /**
60
+ * Value of the id attribute present on the <canvas> element containing the chart
61
+ */
66
62
  chartId: {
67
63
  type: String,
68
64
  default: 'doughnut-chart',
69
65
  },
66
+ /**
67
+ * Data to be passed to Chart
68
+ */
70
69
  data: {
71
70
  type: Array as PropType<DoughnutData[]>,
72
- default: () => []
71
+ default: () => [],
73
72
  },
73
+ /**
74
+ * Labels used to label the index axis (default x axes). See [Data structures documentation](https://www.chartjs.org/docs/latest/general/data-structures.html)
75
+ */
74
76
  labels: {
75
77
  type: Array as PropType<string[]>,
76
- default: () => []
78
+ default: () => [],
77
79
  },
80
+ /**
81
+ * Add custom CSS classes to the <canvas> element
82
+ */
78
83
  cssClasses: {
79
- default: '',
80
84
  type: String,
85
+ default: undefined,
81
86
  },
82
- // disable accessibility patterns
87
+ /**
88
+ * Disable accessibility patterns
89
+ */
83
90
  disableAccessibility: {
84
91
  type: Boolean,
85
- default: false
92
+ default: false,
86
93
  },
87
- // colourSet props is used to choose the colour set of the charts as defined in the Figma prototypes.
88
- // 7 colour sets are currently defined :
89
- // - Default 0 corresponds to the current one
90
- // - 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
91
- // Remark : All the sets are defined in /src/services/patterns/ChartDesign.ts
94
+ /**
95
+ * Used to choose the colour set of the charts as defined in the Figma prototypes.
96
+ * 7 colour sets are currently defined:
97
+ * - Default 0 corresponds to the current one
98
+ * - 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)
99
+ * Note: All the sets are defined in /src/services/patterns/ChartDesign.ts
100
+ */
92
101
  colourSet: {
93
- default:'0',
94
- type: Number
102
+ type: Number,
103
+ default: 0,
95
104
  },
96
- // 6 patterns exist and are not randomly given but follow the order defined in patternsStandardList /src/services/patterns/ChartDesign.ts
97
- // Additionally, a pattern has only one possible colour per colour set as defined in the Figma prototype.
98
- // In some use cases, the chart may need to show a different orders of these patterns, this can be changed using the props newPatternsOrder
105
+ /**
106
+ * 6 patterns exist and are not randomly given but follow the order defined in [patternsStandardList](/src/services/patterns/ChartDesign.ts)
107
+ * Additionally, a pattern has only one possible colour per colour set as defined in the Figma prototype.
108
+ * In some use cases, the chart may need to show a different orders of these patterns, this can be changed using the props newPatternsOrder
109
+ */
99
110
  newPatternsOrder: {
100
- type: Array as PropType<number[]>,
101
- default: () => [0,1,2,3,4,5]
102
- },
111
+ type: Array as PropType<number[]>,
112
+ default: () => [0, 1, 2, 3, 4, 5],
113
+ },
114
+ /**
115
+ * Value of the `width` css property used to define the width of the <canvas> element
116
+ */
103
117
  width: {
104
- type: String,
105
- default: '400px',
118
+ type: String,
119
+ default: '400px',
106
120
  },
121
+ /**
122
+ * Value of the `height` css property used to define the height of the <canvas> element
123
+ */
107
124
  height: {
108
125
  type: String,
109
126
  default: '400px',
110
127
  },
128
+ /**
129
+ * Maximum number of data to be displayed in the Chart
130
+ */
111
131
  maxValues: {
112
132
  type: Number,
113
133
  default: 5,
114
134
  },
135
+ /**
136
+ * Add custom CSS styles to the <canvas> element
137
+ */
115
138
  styles: {
116
139
  type: Object as PropType<Partial<CSSStyleDeclaration>>,
117
- default: () => {
118
- },
140
+ default: () => {},
119
141
  },
142
+ /**
143
+ * Value of the `plugins` key passed to the Chart config
144
+ */
120
145
  plugins: {
121
146
  type: Array as PropType<Plugin<'doughnut'>[]>,
122
147
  default: () => [],
123
148
  },
124
- })
149
+ });
125
150
 
126
- const patternsColors = computed( () => {
151
+ const patternsColors = computed(() => {
127
152
  return doughnutDataProps.newPatternsOrder.length !== 6
128
- ? colourSets[doughnutDataProps.colourSet]
129
- : doughnutDataProps.newPatternsOrder.map((id) => {
130
- return colourSets[doughnutDataProps.colourSet][id]
131
- })
153
+ ? colourSets[doughnutDataProps.colourSet]
154
+ : doughnutDataProps.newPatternsOrder.map((id) => {
155
+ return colourSets[doughnutDataProps.colourSet][id];
156
+ });
132
157
  });
133
158
 
134
159
  const patternsOrderedList = computed(() => {
135
- return doughnutDataProps.newPatternsOrder.length !== 6
136
- ? patternsStandardList
137
- : doughnutDataProps.newPatternsOrder.map((id) => {
138
- return patternsStandardList[id]
139
- })
140
- }
141
- );
160
+ return doughnutDataProps.newPatternsOrder.length !== 6
161
+ ? patternsStandardList
162
+ : doughnutDataProps.newPatternsOrder.map((id) => {
163
+ return patternsStandardList[id];
164
+ });
165
+ });
142
166
 
143
- const doughnutChartData = computed(() => {
167
+ const doughnutChartData = computed<any>(() => {
144
168
  return {
145
- labels: getDoughnutLabels(doughnutDataProps.labels,
146
- doughnutDataProps.data, doughnutDataProps.maxValues),
169
+ labels: getDoughnutLabels(
170
+ doughnutDataProps.labels,
171
+ doughnutDataProps.data,
172
+ doughnutDataProps.maxValues
173
+ ),
147
174
  datasets: [
148
175
  {
149
- data: groupDataAfterNthValue(doughnutDataProps.data, doughnutDataProps.maxValues).map((x: GenericData) => x.rate),
150
- backgroundColor: getBackgroundColor(patternsColors.value, patternsOrderedList.value, doughnutDataProps.disableAccessibility),
151
- borderColor: getBorderColor(patternsColors.value)
152
- }
153
- ]
154
- }
176
+ data: groupDataAfterNthValue(
177
+ doughnutDataProps.data,
178
+ doughnutDataProps.maxValues
179
+ ).map((x: GenericData) => x.rate),
180
+ backgroundColor: getBackgroundColor(
181
+ patternsColors.value,
182
+ patternsOrderedList.value,
183
+ doughnutDataProps.disableAccessibility
184
+ ),
185
+ borderColor: getBorderColor(patternsColors.value),
186
+ },
187
+ ],
188
+ };
155
189
  });
156
190
 
157
191
  const getTooltipData = (context: Context) => {
158
- const dataIndex = context.tooltip.dataPoints[0].dataIndex;
159
- if (!dataIndex) {
160
- return '';
161
- }
162
- const tooltipData =
163
- groupDataAfterNthValue(doughnutDataProps.data, doughnutDataProps.maxValues)[dataIndex];
192
+ const dataIndex = context.tooltip.dataPoints[0].dataIndex as number;
193
+ const tooltipData = groupDataAfterNthValue(
194
+ doughnutDataProps.data,
195
+ doughnutDataProps.maxValues
196
+ )[dataIndex];
164
197
  const rate = formatWithThousandsSeprators(tooltipData.rate);
165
198
  const value = formatWithThousandsSeprators(tooltipData.value);
166
199
  const unit = tooltipData.unit || '';
167
200
  return `${value}${unit} (${rate})%`;
168
201
  };
169
202
 
170
- function getSpacing () {
203
+ function getSpacing() {
171
204
  return doughnutDataProps.labels.length <= 1 ? 0 : 12;
172
205
  }
173
206
 
@@ -185,57 +218,80 @@ const options = computed(() => {
185
218
  align: 'start' as const,
186
219
  labels: {
187
220
  pointStyle: 'rectRounded',
188
- usePointStyle: true
189
- }
221
+ usePointStyle: true,
222
+ },
190
223
  },
191
224
  title: {
192
- display: false
225
+ display: false,
193
226
  },
194
227
  tooltip: {
195
228
  enabled: false,
196
229
  external: function (context: Context) {
197
230
  new GenericTooltipService().createTooltip(
198
- context,
199
- getTooltipData,
200
- {
201
- chartType: TooltipChartType.DOUGHNUT
202
- },
203
- colors,
204
- patterns,
205
- disablePattern.value
231
+ context,
232
+ getTooltipData,
233
+ {
234
+ chartType: TooltipChartType.DOUGHNUT,
235
+ },
236
+ colors,
237
+ patterns,
238
+ disablePattern.value
206
239
  );
207
- }
208
- }
240
+ },
241
+ },
209
242
  },
210
243
  radius: '90%',
211
244
  cutout: '70%',
212
245
  borderWidth: 3,
213
246
  spacing: getSpacing(),
214
- hoverOffset: 4
215
- }
247
+ hoverOffset: 4,
248
+ };
216
249
  });
217
250
 
218
251
  const doughnutDataAndLabels = {
219
252
  data: doughnutDataProps.data,
220
- labels: doughnutDataProps.labels
221
- }
222
- const disablePattern = computed(() => {return doughnutDataProps.disableAccessibility})
253
+ labels: doughnutDataProps.labels,
254
+ };
255
+ const disablePattern = computed(() => {
256
+ return doughnutDataProps.disableAccessibility;
257
+ });
223
258
 
224
- const htmlLegendPlugin = computed(() => privateGetHtmlLegendPlugin(legendContainer, selectMode, disablePattern, patternsColors, patternsOrderedList, doughnutDataProps.maxValues, doughnutDataAndLabels));
259
+ const htmlLegendPlugin = computed(() =>
260
+ privateGetHtmlLegendPlugin(
261
+ legendContainer,
262
+ selectMode,
263
+ disablePattern,
264
+ patternsColors,
265
+ patternsOrderedList,
266
+ doughnutDataProps.maxValues,
267
+ doughnutDataAndLabels
268
+ )
269
+ );
225
270
 
226
- onMounted(() =>{
227
- getBackgroundColor(patternsColors.value, patternsOrderedList.value, doughnutDataProps.disableAccessibility);
271
+ onMounted(() => {
272
+ getBackgroundColor(
273
+ patternsColors.value,
274
+ patternsOrderedList.value,
275
+ doughnutDataProps.disableAccessibility
276
+ );
228
277
  });
229
278
  watch(onHoverIndex, (newValue, oldValue) => {
230
279
  if (newValue !== oldValue) {
231
- getBackgroundColor(patternsColors.value, patternsOrderedList.value, doughnutDataProps.disableAccessibility);
280
+ getBackgroundColor(
281
+ patternsColors.value,
282
+ patternsOrderedList.value,
283
+ doughnutDataProps.disableAccessibility
284
+ );
232
285
  }
233
286
  });
234
287
 
235
288
  watch(disablePattern, () => {
236
- getBackgroundColor(patternsColors.value, patternsOrderedList.value, doughnutDataProps.disableAccessibility)
289
+ getBackgroundColor(
290
+ patternsColors.value,
291
+ patternsOrderedList.value,
292
+ doughnutDataProps.disableAccessibility
293
+ );
237
294
  });
238
-
239
295
  </script>
240
296
 
241
297
  <style scoped>
@@ -252,4 +308,4 @@ watch(disablePattern, () => {
252
308
  justify-content: center;
253
309
  align-items: center;
254
310
  }
255
- </style>
311
+ </style>