svelte-tiny-linked-charts 2.0.0 → 2.2.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.
package/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <https://unlicense.org>
package/README.md CHANGED
@@ -90,6 +90,7 @@ grow | false | Whether or not the bar should grow to fill out the full width of
90
90
  align | right | The side the bars should align to when they do not completely fill out the chart.
91
91
  gap | 1 | Gap between the bars in pixels.
92
92
  fill | #ff3e00 | Color of the bars, can be any valid CSS color.
93
+ fillArray | [] | Array of colors for each individual bar.
93
94
  fadeOpacity | 0.5 | The opacity the faded out bars should display in.
94
95
  hover | true | Boolean whether or not this chart can be hovered at all.
95
96
  transition | 0 | Transition the chart between different stats. Value is time in milliseconds.
@@ -103,8 +104,10 @@ scaleMax | 0 | Use this to overwrite the automatic scale set to the highest valu
103
104
  scaleMax | 0 | Use this to overwrite the default value floor of 0.
104
105
  type | bar | Can be set to "line" to display a line chart instead.
105
106
  lineColor | fill | Color of the line if used with type="line".
106
- tabindex | -1 | Sets the tabindex of each bar.
107
- preserveAspectRatio | false | Sets whether or not the SVG will preserve it's aspect ratio
107
+ preserveAspectRatio | false | Sets whether or not the SVG will preserve it's aspect ratio.
108
+ tabindex | -1 | Sets the tabindex of each bar. When a tabindex of 0 is given, each bar will contain a title that describes the bar's label and value.
109
+ title | "" | Title that describes the chart for screen readers.
110
+ description | "" | Description that describes the chart for screen readers.
108
111
  onclick | null | Function that executes on click and returns the key and index for the clicked data.
109
112
  onhover | null | Function that executes on hover of each bar.
110
113
  onblur | null | Function that executes when focus leaves the chart.
@@ -33,39 +33,42 @@
33
33
 
34
34
  /**
35
35
  * @typedef {Object} Props
36
- * @property {string} [uid]
37
- * @property {Record<string, number>} [data]
38
- * @property {string[]} [labels]
39
- * @property {number[]} [values]
40
- * @property {string} [linked]
41
- * @property {number} [height]
42
- * @property {number} [width]
43
- * @property {number} [barMinWidth]
44
- * @property {number} [barMinHeight]
45
- * @property {number} [hideBarBelow]
46
- * @property {boolean} [grow]
47
- * @property {"left" | "right"} [align]
48
- * @property {number} [gap]
49
- * @property {string} [fill]
50
- * @property {number} [fadeOpacity]
51
- * @property {boolean} [hover]
52
- * @property {number} [transition]
53
- * @property {boolean} [showValue]
54
- * @property {string} [valueDefault]
55
- * @property {string} [valuePrepend]
56
- * @property {string} [valueAppend]
57
- * @property {"static" | "floating"} [valuePosition]
58
- * @property {number} [valueUndefined]
59
- * @property {number} [scaleMax]
60
- * @property {number} [scaleMin]
61
- * @property {"bar" | "line"} [type]
62
- * @property {string} [lineColor]
63
- * @property {-1 | 0} [tabindex]
64
- * @property {boolean} [preserveAspectRatio]
65
- * @property {(args: OnClick) => void} [onclick]
66
- * @property {(args: OnValueUpdate) => void} [onvalueupdate]
67
- * @property {(args: OnHover) => void} [onhover]
68
- * @property {(args: OnBlur) => void} [onblur]
36
+ * @property {string} [uid] Unique ID to link this chart to a LinkedValue component with the same uid.
37
+ * @property {Record<string, number>} [data] Data that will be displayed in the chart supplied in key:value object. Key should be a string, value a number.
38
+ * @property {string[]} [labels] Labels supplied separately, to be used together with `values` property.
39
+ * @property {number[]} [values] Values supplied separately, to be used together with `labels` property.
40
+ * @property {string} [linked] Key to link this chart to other charts with the same key.
41
+ * @property {number} [height] Height of the chart in pixels.
42
+ * @property {number} [width] Width of the chart in pixels.
43
+ * @property {number} [barMinWidth] Width of the bars in the chart in pixels.
44
+ * @property {number} [barMinHeight] Minimum height of the bars in the chart in pixels.
45
+ * @property {number} [hideBarBelow] Bars below this value will be hidden, showing as 0 height.
46
+ * @property {boolean} [grow] Whether or not the bar should grow to fill out the full width of the chart.
47
+ * @property {"left" | "right"} [align] The side the bars should align to when they do not completely fill out the chart.
48
+ * @property {number} [gap] Gap between the bars in pixels.
49
+ * @property {string} [fill] Color of the bars, can be any valid CSS color.
50
+ * @property {Array<string | null>} [fillArray] Array of colors for each individual bar.
51
+ * @property {number} [fadeOpacity] The opacity the faded out bars should display in.
52
+ * @property {boolean} [hover] Boolean whether or not this chart can be hovered at all.
53
+ * @property {number} [transition] Transition the chart between different stats. Value is time in milliseconds.
54
+ * @property {boolean} [showValue] Boolean whether or not a value will be shown.
55
+ * @property {string} [valueDefault] Default value when not hovering.
56
+ * @property {string} [valuePrepend] String to prepend the value.
57
+ * @property {string} [valueAppend] String to append to the value.
58
+ * @property {number} [valueUndefined] For when the hovering value returns undefined.
59
+ * @property {"static" | "floating"} [valuePosition] Can be set to "floating" to follow the position of the hover.
60
+ * @property {number} [scaleMax] Use this to overwrite the automatic scale set to the highest value in your array.
61
+ * @property {number} [scaleMin] Use this to overwrite the default value floor of 0.
62
+ * @property {"bar" | "line"} [type] Can be set to "line" to display a line chart instead.
63
+ * @property {string} [lineColor] Color of the line if used with type="line".
64
+ * @property {boolean} [preserveAspectRatio] Sets whether or not the SVG will preserve it's aspect ratio.
65
+ * @property {-1 | 0} [tabindex] Sets the tabindex of each bar. When a tabindex of 0 is given, each bar will contain a title that describes the bar's label and value.
66
+ * @property {string} [title] Title that describes the chart for screen readers.
67
+ * @property {string} [description] Description that describes the chart for screen readers.
68
+ * @property {(args: OnClick) => void} [onclick] Function that executes on click and returns the key and index for the clicked data.
69
+ * @property {(args: OnValueUpdate) => void} [onvalueupdate] Function that executes when a value in the chart updates.
70
+ * @property {(args: OnHover) => void} [onhover] Function that executes on hover of each bar, also fires on tab focus.
71
+ * @property {(args: OnBlur) => void} [onblur] Function that executes when hover or focus leaves the chart.
69
72
  */
70
73
 
71
74
  /** @type {Props & { [key: string]: any }} */
@@ -84,6 +87,7 @@
84
87
  align = "right",
85
88
  gap = 1,
86
89
  fill = "#ff3e00",
90
+ fillArray = [],
87
91
  fadeOpacity = 0.5,
88
92
  hover = true,
89
93
  transition = 0,
@@ -97,8 +101,10 @@
97
101
  scaleMin = 0,
98
102
  type = "bar",
99
103
  lineColor = fill,
100
- tabindex = -1,
101
104
  preserveAspectRatio = false,
105
+ tabindex = -1,
106
+ title = "",
107
+ description = "",
102
108
  onclick = ({ key, index }) => null,
103
109
  onvalueupdate = ({ value, uid, linkedKey, valueElement }) => null,
104
110
  onhover = ({ uid, key, index, linkedKey, value, valueElement, eventElement }) => null,
@@ -219,8 +225,17 @@
219
225
  preserveAspectRatio={preserveAspectRatio ? "true" : "none"}
220
226
  onmouseleave={endHover}
221
227
  onblur={endHover}
228
+ aria-labelledby={title ? `${uid}-title` : null}
222
229
  {...rest}>
223
230
 
231
+ {#if title}
232
+ <title id="{uid}-title">{title}</title>
233
+ {/if}
234
+
235
+ {#if description}
236
+ <desc>{description}</desc>
237
+ {/if}
238
+
224
239
  <g transform="translate({alignmentOffset}, 0)">
225
240
  {#if type == "line"}
226
241
  <polyline points={polyline.join(" ")} stroke={lineColor} fill="transparent" />
@@ -231,14 +246,14 @@
231
246
  <rect
232
247
  style={transition ? `transition: all ${transition}ms` : null}
233
248
  opacity={hover && $hoveringKey[linkedKey] && $hoveringKey[linkedKey] != key ? fadeOpacity : 1}
234
- fill={fill}
249
+ fill={fillArray[i] || fill}
235
250
  width={barWidth}
236
251
  height={getHeight(value)}
237
252
  x={(gap + barWidth) * i}
238
253
  y={(height - getHeight(value))} />
239
254
  {:else if type == "line"}
240
255
  <circle
241
- fill={hover && $hoveringKey[linkedKey] !== null && $hoveringKey[linkedKey] == key ? fill : "transparent"}
256
+ fill={hover && $hoveringKey[linkedKey] !== null && $hoveringKey[linkedKey] == key ? (fillArray[i] || fill) : "transparent"}
242
257
  r={grow ? barMinWidth : barWidth / 2}
243
258
  cy={height - getHeight(value)}
244
259
  cx={((gap + barWidth) + (barWidth / (Object.keys(data).length))) * i} />
@@ -256,7 +271,11 @@
256
271
  height={height}
257
272
  fill="transparent"
258
273
  x={(gap + barWidth) * i}
259
- {tabindex} />
274
+ {tabindex}>
275
+ {#if tabindex !== -1}
276
+ <title>{key}: {value}</title>
277
+ {/if}
278
+ </rect>
260
279
  {/each}
261
280
  </g>
262
281
  </svg>
@@ -268,6 +287,7 @@
268
287
  <span bind:this={valueElement}>{$hoveringValue[uid] || valueUndefined}</span>
269
288
  {valueAppend}
270
289
  {:else}
290
+ <!-- eslint-disable-next-line svelte/no-at-html-tags -->
271
291
  {@html valueDefault}
272
292
  {/if}
273
293
  </div>
@@ -6,45 +6,153 @@ type LinkedChart = {
6
6
  }>): void;
7
7
  };
8
8
  declare const LinkedChart: import("svelte").Component<{
9
+ /**
10
+ * Unique ID to link this chart to a LinkedValue component with the same uid.
11
+ */
9
12
  uid?: string | undefined;
13
+ /**
14
+ * Data that will be displayed in the chart supplied in key:value object. Key should be a string, value a number.
15
+ */
10
16
  data?: Record<string, number> | undefined;
17
+ /**
18
+ * Labels supplied separately, to be used together with `values` property.
19
+ */
11
20
  labels?: string[] | undefined;
21
+ /**
22
+ * Values supplied separately, to be used together with `labels` property.
23
+ */
12
24
  values?: number[] | undefined;
25
+ /**
26
+ * Key to link this chart to other charts with the same key.
27
+ */
13
28
  linked?: string | undefined;
29
+ /**
30
+ * Height of the chart in pixels.
31
+ */
14
32
  height?: number | undefined;
33
+ /**
34
+ * Width of the chart in pixels.
35
+ */
15
36
  width?: number | undefined;
37
+ /**
38
+ * Width of the bars in the chart in pixels.
39
+ */
16
40
  barMinWidth?: number | undefined;
41
+ /**
42
+ * Minimum height of the bars in the chart in pixels.
43
+ */
17
44
  barMinHeight?: number | undefined;
45
+ /**
46
+ * Bars below this value will be hidden, showing as 0 height.
47
+ */
18
48
  hideBarBelow?: number | undefined;
49
+ /**
50
+ * Whether or not the bar should grow to fill out the full width of the chart.
51
+ */
19
52
  grow?: boolean | undefined;
53
+ /**
54
+ * The side the bars should align to when they do not completely fill out the chart.
55
+ */
20
56
  align?: "left" | "right" | undefined;
57
+ /**
58
+ * Gap between the bars in pixels.
59
+ */
21
60
  gap?: number | undefined;
61
+ /**
62
+ * Color of the bars, can be any valid CSS color.
63
+ */
22
64
  fill?: string | undefined;
65
+ /**
66
+ * Array of colors for each individual bar.
67
+ */
68
+ fillArray?: (string | null)[] | undefined;
69
+ /**
70
+ * The opacity the faded out bars should display in.
71
+ */
23
72
  fadeOpacity?: number | undefined;
73
+ /**
74
+ * Boolean whether or not this chart can be hovered at all.
75
+ */
24
76
  hover?: boolean | undefined;
77
+ /**
78
+ * Transition the chart between different stats. Value is time in milliseconds.
79
+ */
25
80
  transition?: number | undefined;
81
+ /**
82
+ * Boolean whether or not a value will be shown.
83
+ */
26
84
  showValue?: boolean | undefined;
85
+ /**
86
+ * Default value when not hovering.
87
+ */
27
88
  valueDefault?: string | undefined;
89
+ /**
90
+ * String to prepend the value.
91
+ */
28
92
  valuePrepend?: string | undefined;
93
+ /**
94
+ * String to append to the value.
95
+ */
29
96
  valueAppend?: string | undefined;
30
- valuePosition?: "static" | "floating" | undefined;
97
+ /**
98
+ * For when the hovering value returns undefined.
99
+ */
31
100
  valueUndefined?: number | undefined;
101
+ /**
102
+ * Can be set to "floating" to follow the position of the hover.
103
+ */
104
+ valuePosition?: "static" | "floating" | undefined;
105
+ /**
106
+ * Use this to overwrite the automatic scale set to the highest value in your array.
107
+ */
32
108
  scaleMax?: number | undefined;
109
+ /**
110
+ * Use this to overwrite the default value floor of 0.
111
+ */
33
112
  scaleMin?: number | undefined;
113
+ /**
114
+ * Can be set to "line" to display a line chart instead.
115
+ */
34
116
  type?: "bar" | "line" | undefined;
117
+ /**
118
+ * Color of the line if used with type="line".
119
+ */
35
120
  lineColor?: string | undefined;
36
- tabindex?: 0 | -1 | undefined;
121
+ /**
122
+ * Sets whether or not the SVG will preserve it's aspect ratio.
123
+ */
37
124
  preserveAspectRatio?: boolean | undefined;
125
+ /**
126
+ * Sets the tabindex of each bar. When a tabindex of 0 is given, each bar will contain a title that describes the bar's label and value.
127
+ */
128
+ tabindex?: 0 | -1 | undefined;
129
+ /**
130
+ * Title that describes the chart for screen readers.
131
+ */
132
+ title?: string | undefined;
133
+ /**
134
+ * Description that describes the chart for screen readers.
135
+ */
136
+ description?: string | undefined;
137
+ /**
138
+ * Function that executes on click and returns the key and index for the clicked data.
139
+ */
38
140
  onclick?: ((args: {
39
141
  key?: string | undefined;
40
142
  index?: number | undefined;
41
143
  }) => void) | undefined;
144
+ /**
145
+ * Function that executes when a value in the chart updates.
146
+ */
42
147
  onvalueupdate?: ((args: {
43
148
  value?: number | null | undefined;
44
149
  uid?: string | undefined;
45
150
  linkedKey?: string | undefined;
46
151
  valueElement?: HTMLElement | null | undefined;
47
152
  }) => void) | undefined;
153
+ /**
154
+ * Function that executes on hover of each bar, also fires on tab focus.
155
+ */
48
156
  onhover?: ((args: {
49
157
  /**
50
158
  * ,
@@ -72,6 +180,9 @@ declare const LinkedChart: import("svelte").Component<{
72
180
  valueElement: HTMLElement;
73
181
  eventElement: EventTarget | null;
74
182
  }) => void) | undefined;
183
+ /**
184
+ * Function that executes when hover or focus leaves the chart.
185
+ */
75
186
  onblur?: ((args: {
76
187
  /**
77
188
  * ,
@@ -91,45 +202,153 @@ declare const LinkedChart: import("svelte").Component<{
91
202
  [key: string]: any;
92
203
  }, {}, "data">;
93
204
  type Props = {
205
+ /**
206
+ * Unique ID to link this chart to a LinkedValue component with the same uid.
207
+ */
94
208
  uid?: string | undefined;
209
+ /**
210
+ * Data that will be displayed in the chart supplied in key:value object. Key should be a string, value a number.
211
+ */
95
212
  data?: Record<string, number> | undefined;
213
+ /**
214
+ * Labels supplied separately, to be used together with `values` property.
215
+ */
96
216
  labels?: string[] | undefined;
217
+ /**
218
+ * Values supplied separately, to be used together with `labels` property.
219
+ */
97
220
  values?: number[] | undefined;
221
+ /**
222
+ * Key to link this chart to other charts with the same key.
223
+ */
98
224
  linked?: string | undefined;
225
+ /**
226
+ * Height of the chart in pixels.
227
+ */
99
228
  height?: number | undefined;
229
+ /**
230
+ * Width of the chart in pixels.
231
+ */
100
232
  width?: number | undefined;
233
+ /**
234
+ * Width of the bars in the chart in pixels.
235
+ */
101
236
  barMinWidth?: number | undefined;
237
+ /**
238
+ * Minimum height of the bars in the chart in pixels.
239
+ */
102
240
  barMinHeight?: number | undefined;
241
+ /**
242
+ * Bars below this value will be hidden, showing as 0 height.
243
+ */
103
244
  hideBarBelow?: number | undefined;
245
+ /**
246
+ * Whether or not the bar should grow to fill out the full width of the chart.
247
+ */
104
248
  grow?: boolean | undefined;
249
+ /**
250
+ * The side the bars should align to when they do not completely fill out the chart.
251
+ */
105
252
  align?: "left" | "right" | undefined;
253
+ /**
254
+ * Gap between the bars in pixels.
255
+ */
106
256
  gap?: number | undefined;
257
+ /**
258
+ * Color of the bars, can be any valid CSS color.
259
+ */
107
260
  fill?: string | undefined;
261
+ /**
262
+ * Array of colors for each individual bar.
263
+ */
264
+ fillArray?: (string | null)[] | undefined;
265
+ /**
266
+ * The opacity the faded out bars should display in.
267
+ */
108
268
  fadeOpacity?: number | undefined;
269
+ /**
270
+ * Boolean whether or not this chart can be hovered at all.
271
+ */
109
272
  hover?: boolean | undefined;
273
+ /**
274
+ * Transition the chart between different stats. Value is time in milliseconds.
275
+ */
110
276
  transition?: number | undefined;
277
+ /**
278
+ * Boolean whether or not a value will be shown.
279
+ */
111
280
  showValue?: boolean | undefined;
281
+ /**
282
+ * Default value when not hovering.
283
+ */
112
284
  valueDefault?: string | undefined;
285
+ /**
286
+ * String to prepend the value.
287
+ */
113
288
  valuePrepend?: string | undefined;
289
+ /**
290
+ * String to append to the value.
291
+ */
114
292
  valueAppend?: string | undefined;
115
- valuePosition?: "static" | "floating" | undefined;
293
+ /**
294
+ * For when the hovering value returns undefined.
295
+ */
116
296
  valueUndefined?: number | undefined;
297
+ /**
298
+ * Can be set to "floating" to follow the position of the hover.
299
+ */
300
+ valuePosition?: "static" | "floating" | undefined;
301
+ /**
302
+ * Use this to overwrite the automatic scale set to the highest value in your array.
303
+ */
117
304
  scaleMax?: number | undefined;
305
+ /**
306
+ * Use this to overwrite the default value floor of 0.
307
+ */
118
308
  scaleMin?: number | undefined;
309
+ /**
310
+ * Can be set to "line" to display a line chart instead.
311
+ */
119
312
  type?: "bar" | "line" | undefined;
313
+ /**
314
+ * Color of the line if used with type="line".
315
+ */
120
316
  lineColor?: string | undefined;
121
- tabindex?: 0 | -1 | undefined;
317
+ /**
318
+ * Sets whether or not the SVG will preserve it's aspect ratio.
319
+ */
122
320
  preserveAspectRatio?: boolean | undefined;
321
+ /**
322
+ * Sets the tabindex of each bar. When a tabindex of 0 is given, each bar will contain a title that describes the bar's label and value.
323
+ */
324
+ tabindex?: 0 | -1 | undefined;
325
+ /**
326
+ * Title that describes the chart for screen readers.
327
+ */
328
+ title?: string | undefined;
329
+ /**
330
+ * Description that describes the chart for screen readers.
331
+ */
332
+ description?: string | undefined;
333
+ /**
334
+ * Function that executes on click and returns the key and index for the clicked data.
335
+ */
123
336
  onclick?: ((args: {
124
337
  key?: string | undefined;
125
338
  index?: number | undefined;
126
339
  }) => void) | undefined;
340
+ /**
341
+ * Function that executes when a value in the chart updates.
342
+ */
127
343
  onvalueupdate?: ((args: {
128
344
  value?: number | null | undefined;
129
345
  uid?: string | undefined;
130
346
  linkedKey?: string | undefined;
131
347
  valueElement?: HTMLElement | null | undefined;
132
348
  }) => void) | undefined;
349
+ /**
350
+ * Function that executes on hover of each bar, also fires on tab focus.
351
+ */
133
352
  onhover?: ((args: {
134
353
  /**
135
354
  * ,
@@ -157,6 +376,9 @@ type Props = {
157
376
  valueElement: HTMLElement;
158
377
  eventElement: EventTarget | null;
159
378
  }) => void) | undefined;
379
+ /**
380
+ * Function that executes when hover or focus leaves the chart.
381
+ */
160
382
  onblur?: ((args: {
161
383
  /**
162
384
  * ,
@@ -11,5 +11,6 @@
11
11
  {#if label}
12
12
  {transform(label)}
13
13
  {:else}
14
+ <!-- eslint-disable-next-line svelte/no-at-html-tags -->
14
15
  {@html empty}
15
16
  {/if}
@@ -16,5 +16,6 @@
16
16
  {#if uid in $hoveringValue && value !== null}
17
17
  {transform(value) || valueUndefined}
18
18
  {:else}
19
+ <!-- eslint-disable-next-line svelte/no-at-html-tags -->
19
20
  {@html empty}
20
21
  {/if}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-tiny-linked-charts",
3
- "version": "2.0.0",
3
+ "version": "2.2.0",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "vite build && npm run package",
@@ -35,8 +35,10 @@
35
35
  "@sveltejs/vite-plugin-svelte": "^4.0.0",
36
36
  "@sveltejs/package": "^2.3.7",
37
37
  "@testing-library/svelte": "^5.2.6",
38
- "eslint": "^9.0.0",
39
- "eslint-config-prettier": "^9.1.0",
38
+ "eslint": "^9.18.0",
39
+ "eslint-config-prettier": "^10.0.1",
40
+ "eslint-plugin-svelte": "^2.46.1",
41
+ "globals": "^15.14.0",
40
42
  "happy-dom": "^16.5.3",
41
43
  "prettier": "^3.1.0",
42
44
  "prettier-plugin-svelte": "^3.2.6",