svelte-tiny-linked-charts 1.6.2 → 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
@@ -20,6 +20,8 @@ yarn add svelte-tiny-linked-charts --dev
20
20
  npm install svelte-tiny-linked-charts --save-dev
21
21
  ```
22
22
 
23
+ If you are using Svelte 4, use version ^1.0.0. Version 2 is reserved for Svelte 5.
24
+
23
25
  Include the chart in your app.
24
26
  ```js
25
27
  import { LinkedChart, LinkedLabel, LinkedValue } from "svelte-tiny-linked-charts"
@@ -88,6 +90,7 @@ grow | false | Whether or not the bar should grow to fill out the full width of
88
90
  align | right | The side the bars should align to when they do not completely fill out the chart.
89
91
  gap | 1 | Gap between the bars in pixels.
90
92
  fill | #ff3e00 | Color of the bars, can be any valid CSS color.
93
+ fillArray | [] | Array of colors for each individual bar.
91
94
  fadeOpacity | 0.5 | The opacity the faded out bars should display in.
92
95
  hover | true | Boolean whether or not this chart can be hovered at all.
93
96
  transition | 0 | Transition the chart between different stats. Value is time in milliseconds.
@@ -101,10 +104,13 @@ scaleMax | 0 | Use this to overwrite the automatic scale set to the highest valu
101
104
  scaleMax | 0 | Use this to overwrite the default value floor of 0.
102
105
  type | bar | Can be set to "line" to display a line chart instead.
103
106
  lineColor | fill | Color of the line if used with type="line".
104
- tabindex | -1 | Sets the tabindex of each bar.
105
- preserveAspectRatio | false | Sets whether or not the SVG will preserve it's aspect ratio
106
- dispatchEvents | false | Boolean whether or not to dispatch events on certain actions.
107
- clickHandler | null | Function that executes on click and returns the key and index for the clicked data.
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.
111
+ onclick | null | Function that executes on click and returns the key and index for the clicked data.
112
+ onhover | null | Function that executes on hover of each bar.
113
+ onblur | null | Function that executes when focus leaves the chart.
108
114
 
109
115
  `<LinkedLabel />` component.
110
116
  Property | Default | Description
@@ -1,63 +1,162 @@
1
1
  <script>
2
- import { createEventDispatcher, tick } from "svelte"
2
+ import { tick } from "svelte"
3
3
  import { hoveringKey, hoveringValue } from "./stores/tinyLinkedCharts.js"
4
4
 
5
- export let uid = (Math.random() + 1).toString(36).substring(7)
6
- export let data = {}
7
- export let labels = []
8
- export let values = []
9
- export let linked = ""
10
- export let height = 40
11
- export let width = 150
12
- export let barMinWidth = 4
13
- export let barMinHeight = 0
14
- export let hideBarBelow = 0
15
- export let grow = false
16
- export let align = "right"
17
- export let gap = 1
18
- export let fill = "#ff3e00"
19
- export let fadeOpacity = 0.5
20
- export let hover = true
21
- export let transition = 0
22
- export let showValue = false
23
- export let valueDefault = "&nbsp;"
24
- export let valuePrepend = ""
25
- export let valueAppend = ""
26
- export let valuePosition = "static"
27
- export let valueUndefined = 0
28
- export let scaleMax = 0
29
- export let scaleMin = 0
30
- export let type = "bar"
31
- export let lineColor = fill
32
- export let tabindex = -1
33
- export let dispatchEvents = false
34
- export let preserveAspectRatio = false
35
- export let clickHandler = (key, i) => null
36
-
37
- const dispatch = createEventDispatcher()
38
-
39
- let valuePositionOffset = 0
40
- let polyline = []
41
- let valueElement
42
-
43
- $: dataLength = Object.keys(data).length
44
- $: barWidth = grow ? getBarWidth(dataLength) : parseInt(barMinWidth)
45
- $: highestValue = getHighestValue(data)
46
- $: alignmentOffset = dataLength ? getAlignment() : 0
47
- $: linkedKey = linked || (Math.random() + 1).toString(36).substring(7)
48
- $: $hoveringValue[uid] = $hoveringKey[linkedKey] ? data[$hoveringKey[linkedKey]] : null
49
- $: if (labels.length && values.length) data = Object.fromEntries(labels.map((_, i) => [labels[i], values[i]]))
50
- $: if (valuePosition == "floating") valuePositionOffset = (parseInt(gap) + barWidth) * Object.keys(data).indexOf($hoveringKey[linkedKey]) + alignmentOffset
51
- $: if (type == "line") polyline = getPolyLinePoints(data)
52
- $: if (dispatchEvents) dispatch('value-update', { value: $hoveringValue[uid], uid, linkedKey, valueElement })
53
- $: if (tabindex > 0) console.warn("Tabindex should not be higher than 0")
5
+ /** @typedef {Object} OnValueUpdate
6
+ * @property {number | null} [value]
7
+ * @property {string} [uid]
8
+ * @property {string} [linkedKey]
9
+ * @property {HTMLElement | null} [valueElement]
10
+ */
54
11
 
12
+ /** @typedef {Object} OnClick
13
+ * @property {string} [key]
14
+ * @property {number} [index]
15
+ */
16
+
17
+ /** @typedef {Object} OnHover
18
+ * @property {string} uid,
19
+ * @property {string} key,
20
+ * @property {number} index,
21
+ * @property {string} linkedKey,
22
+ * @property {number | null} value,
23
+ * @property {HTMLElement} valueElement,
24
+ * @property {EventTarget | null} eventElement
25
+ */
26
+
27
+ /** @typedef {Object} OnBlur
28
+ * @property {string} uid,
29
+ * @property {string} linkedKey,
30
+ * @property {HTMLElement} valueElement,
31
+ * @property {EventTarget | null} eventElement
32
+ */
33
+
34
+ /**
35
+ * @typedef {Object} Props
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.
72
+ */
73
+
74
+ /** @type {Props & { [key: string]: any }} */
75
+ let {
76
+ uid = (Math.random() + 1).toString(36).substring(7),
77
+ data = $bindable({}),
78
+ labels = [],
79
+ values = [],
80
+ linked = "",
81
+ height = 40,
82
+ width = 150,
83
+ barMinWidth = 4,
84
+ barMinHeight = 0,
85
+ hideBarBelow = 0,
86
+ grow = false,
87
+ align = "right",
88
+ gap = 1,
89
+ fill = "#ff3e00",
90
+ fillArray = [],
91
+ fadeOpacity = 0.5,
92
+ hover = true,
93
+ transition = 0,
94
+ showValue = false,
95
+ valueDefault = "&nbsp;",
96
+ valuePrepend = "",
97
+ valueAppend = "",
98
+ valuePosition = "static",
99
+ valueUndefined = 0,
100
+ scaleMax = 0,
101
+ scaleMin = 0,
102
+ type = "bar",
103
+ lineColor = fill,
104
+ preserveAspectRatio = false,
105
+ tabindex = -1,
106
+ title = "",
107
+ description = "",
108
+ onclick = ({ key, index }) => null,
109
+ onvalueupdate = ({ value, uid, linkedKey, valueElement }) => null,
110
+ onhover = ({ uid, key, index, linkedKey, value, valueElement, eventElement }) => null,
111
+ onblur = ({ uid, linkedKey, valueElement, eventElement }) => null,
112
+ ...rest
113
+ } = $props()
114
+
115
+ let valuePositionOffset = $state(0)
116
+ /** @type {number[][]} */
117
+ let polyline = $state([])
118
+ let valueElement = $state()
119
+ let dataLength = $derived(Object.keys(data).length)
120
+ let barWidth = $derived(dataLength && grow ? getBarWidth() : barMinWidth)
121
+ let highestValue = $derived(data ? getHighestValue() : 0)
122
+ let alignmentOffset = $derived(dataLength ? getAlignment() : 0)
123
+ let linkedKey = $derived(linked || (Math.random() + 1).toString(36).substring(7))
124
+
125
+ $effect(() => {
126
+ if (labels.length && values.length) data = Object.fromEntries(labels.map((_, i) => [labels[i], values[i]]))
127
+ })
128
+
129
+ $effect(() => {
130
+ $hoveringValue[uid] = $hoveringKey[linkedKey] ? data[$hoveringKey[linkedKey]] : null
131
+ })
132
+
133
+ $effect(() => {
134
+ if (valuePosition === "floating") valuePositionOffset = (gap + barWidth) * Object.keys(data).indexOf($hoveringKey[linkedKey] || "") + alignmentOffset
135
+ })
136
+
137
+ $effect(() => {
138
+ if (type == "line") polyline = getPolyLinePoints(data)
139
+ })
140
+
141
+ $effect(() => {
142
+ onvalueupdate({ value: $hoveringValue[uid], uid, linkedKey, valueElement })
143
+ })
144
+
145
+ $effect(() => {
146
+ if (tabindex > 0) console.warn("tabindex should not be higher than 0")
147
+ })
148
+
149
+ /** @returns {number} */
55
150
  function getHighestValue() {
56
151
  if (scaleMax) return scaleMax
57
152
  if (dataLength) return Math.max(...Object.values(data))
58
153
  return 0
59
154
  }
60
155
 
156
+ /**
157
+ * @param {number} value
158
+ * @returns {number}
159
+ */
61
160
  function getHeight(value) {
62
161
  if (value < hideBarBelow || value < scaleMin) return 0
63
162
 
@@ -65,56 +164,77 @@
65
164
  const minValue = scaleMin || 0
66
165
 
67
166
  const scaledValue = (value - minValue) / (maxValue - minValue)
68
- return Math.max(Math.ceil(scaledValue * parseInt(height)), barMinHeight)
167
+ return Math.max(Math.ceil(scaledValue * height), barMinHeight)
69
168
  }
70
169
 
170
+ /** @returns {number} */
71
171
  function getBarWidth() {
72
- return Math.max((parseInt(width) - (dataLength * parseInt(gap))) / dataLength, parseInt(barMinWidth))
172
+ return Math.max((width - (dataLength * gap)) / dataLength, barMinWidth)
73
173
  }
74
174
 
175
+ /** @returns {number} */
75
176
  function getAlignment() {
76
177
  if (align == "left") return 0
77
- return (parseInt(gap) + parseInt(width)) - ((parseInt(gap) + barWidth) * dataLength)
178
+ return (gap + width) - ((gap + barWidth) * dataLength)
78
179
  }
79
180
 
80
- function getPolyLinePoints() {
181
+ /** @param {Record<string, number>} data */
182
+ function getPolyLinePoints(data) {
81
183
  let points = []
82
184
 
83
185
  for (let i = 0; i < Object.keys(data).length; i++) {
84
- points.push([i * ((barWidth + parseInt(gap)) + (barWidth / (Object.keys(data).length))), height - getHeight(Object.values(data)[i])])
186
+ points.push([i * ((barWidth + gap) + (barWidth / (Object.keys(data).length))), height - getHeight(Object.values(data)[i])])
85
187
  }
86
188
 
87
189
  return points
88
190
  }
89
191
 
90
- async function startHover(key, index) {
192
+ /**
193
+ * @param {MouseEvent | FocusEvent | TouchEvent} event
194
+ * @param {string} key
195
+ * @param {number} index
196
+ */
197
+ async function startHover(event, key, index) {
91
198
  if (!hover) return
92
199
  $hoveringKey[linkedKey] = key
93
200
 
94
201
  await tick()
95
202
 
96
- if (dispatchEvents) dispatch('hover', { uid, key, index, linkedKey, value: $hoveringValue[uid], valueElement, eventElement: event.target })
203
+ onhover({ uid, key, index, linkedKey, value: $hoveringValue[uid], valueElement, eventElement: event.target })
97
204
  }
98
205
 
99
- async function endHover() {
206
+ /**
207
+ * @param {FocusEvent} event
208
+ */
209
+ async function endHover(event) {
100
210
  if (!hover) return
101
211
  $hoveringKey[linkedKey] = null
212
+ $hoveringValue[uid] = null
102
213
 
103
214
  await tick()
104
215
 
105
- if (dispatchEvents) dispatch('blur', { uid, linkedKey, valueElement, eventElement: event.target })
216
+ onblur({ uid, linkedKey, valueElement, eventElement: event.target })
106
217
  }
107
218
  </script>
108
219
 
109
- <!-- svelte-ignore a11y-no-static-element-interactions -->
220
+ <!-- svelte-ignore a11y_no_static_element_interactions -->
110
221
  <svg
111
222
  {width}
112
223
  height={type == "line" ? height + barWidth / 2 : height}
113
224
  viewBox="0 0 {width } {height}"
114
225
  preserveAspectRatio={preserveAspectRatio ? "true" : "none"}
115
- {...$$restProps}
116
- on:mouseleave={endHover}
117
- on:blur={endHover}>
226
+ onmouseleave={endHover}
227
+ onblur={endHover}
228
+ aria-labelledby={title ? `${uid}-title` : null}
229
+ {...rest}>
230
+
231
+ {#if title}
232
+ <title id="{uid}-title">{title}</title>
233
+ {/if}
234
+
235
+ {#if description}
236
+ <desc>{description}</desc>
237
+ {/if}
118
238
 
119
239
  <g transform="translate({alignmentOffset}, 0)">
120
240
  {#if type == "line"}
@@ -126,32 +246,36 @@
126
246
  <rect
127
247
  style={transition ? `transition: all ${transition}ms` : null}
128
248
  opacity={hover && $hoveringKey[linkedKey] && $hoveringKey[linkedKey] != key ? fadeOpacity : 1}
129
- fill={fill}
249
+ fill={fillArray[i] || fill}
130
250
  width={barWidth}
131
- height={type == "line" ? height : getHeight(value)}
132
- x={(parseInt(gap) + barWidth) * i}
251
+ height={getHeight(value)}
252
+ x={(gap + barWidth) * i}
133
253
  y={(height - getHeight(value))} />
134
254
  {:else if type == "line"}
135
255
  <circle
136
- fill={hover && $hoveringKey[linkedKey] !== null && $hoveringKey[linkedKey] == key ? fill : "transparent"}
137
- r={grow ? parseInt(barMinWidth) : barWidth / 2}
256
+ fill={hover && $hoveringKey[linkedKey] !== null && $hoveringKey[linkedKey] == key ? (fillArray[i] || fill) : "transparent"}
257
+ r={grow ? barMinWidth : barWidth / 2}
138
258
  cy={height - getHeight(value)}
139
- cx={((parseInt(gap) + barWidth) + (barWidth / (Object.keys(data).length))) * i} />
259
+ cx={((gap + barWidth) + (barWidth / (Object.keys(data).length))) * i} />
140
260
  {/if}
141
261
 
142
- <!-- svelte-ignore a11y-no-noninteractive-tabindex -->
143
- <!-- svelte-ignore a11y-no-static-element-interactions -->
262
+ <!-- svelte-ignore a11y_no_noninteractive_tabindex -->
263
+ <!-- svelte-ignore a11y_no_static_element_interactions -->
144
264
  <rect
145
- on:mouseover={() => startHover(key, i)}
146
- on:focus={() => startHover(key, i)}
147
- on:touchstart={() => startHover(key, i)}
148
- on:click={() => clickHandler(key, i)}
149
- on:keypress={() => clickHandler(key, i)}
265
+ onmouseover={event => startHover(event, key, i)}
266
+ onfocus={event => startHover(event, key, i)}
267
+ ontouchstart={event => startHover(event, key, i)}
268
+ onclick={() => onclick({ key, index: i })}
269
+ onkeypress={() => onclick({ key, index: i })}
150
270
  width={barWidth}
151
271
  height={height}
152
272
  fill="transparent"
153
- x={(parseInt(gap) + barWidth) * i}
154
- {tabindex} />
273
+ x={(gap + barWidth) * i}
274
+ {tabindex}>
275
+ {#if tabindex !== -1}
276
+ <title>{key}: {value}</title>
277
+ {/if}
278
+ </rect>
155
279
  {/each}
156
280
  </g>
157
281
  </svg>
@@ -163,6 +287,7 @@
163
287
  <span bind:this={valueElement}>{$hoveringValue[uid] || valueUndefined}</span>
164
288
  {valueAppend}
165
289
  {:else}
290
+ <!-- eslint-disable-next-line svelte/no-at-html-tags -->
166
291
  {@html valueDefault}
167
292
  {/if}
168
293
  </div>
@@ -1,93 +1,397 @@
1
- /** @typedef {typeof __propDef.props} LinkedChartProps */
2
- /** @typedef {typeof __propDef.events} LinkedChartEvents */
3
- /** @typedef {typeof __propDef.slots} LinkedChartSlots */
4
- export default class LinkedChart extends SvelteComponent<{
5
- [x: string]: any;
6
- hover?: boolean;
7
- uid?: string;
8
- data?: {};
9
- labels?: any[];
10
- values?: any[];
11
- linked?: string;
12
- height?: number;
13
- width?: number;
14
- barMinWidth?: number;
15
- barMinHeight?: number;
16
- hideBarBelow?: number;
17
- grow?: boolean;
18
- align?: string;
19
- gap?: number;
20
- fill?: string;
21
- fadeOpacity?: number;
22
- transition?: number;
23
- showValue?: boolean;
24
- valueDefault?: string;
25
- valuePrepend?: string;
26
- valueAppend?: string;
27
- valuePosition?: string;
28
- valueUndefined?: number;
29
- scaleMax?: number;
30
- scaleMin?: number;
31
- type?: string;
32
- lineColor?: string;
33
- tabindex?: number;
34
- dispatchEvents?: boolean;
35
- preserveAspectRatio?: boolean;
36
- clickHandler?: (key: any, i: any) => any;
37
- }, {
38
- 'value-update': CustomEvent<any>;
39
- hover: CustomEvent<any>;
40
- blur: CustomEvent<any>;
1
+ export default LinkedChart;
2
+ type LinkedChart = {
3
+ $on?(type: string, callback: (e: any) => void): () => void;
4
+ $set?(props: Partial<Props & {
5
+ [key: string]: any;
6
+ }>): void;
7
+ };
8
+ declare const LinkedChart: import("svelte").Component<{
9
+ /**
10
+ * Unique ID to link this chart to a LinkedValue component with the same uid.
11
+ */
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
+ */
16
+ data?: Record<string, number> | undefined;
17
+ /**
18
+ * Labels supplied separately, to be used together with `values` property.
19
+ */
20
+ labels?: string[] | undefined;
21
+ /**
22
+ * Values supplied separately, to be used together with `labels` property.
23
+ */
24
+ values?: number[] | undefined;
25
+ /**
26
+ * Key to link this chart to other charts with the same key.
27
+ */
28
+ linked?: string | undefined;
29
+ /**
30
+ * Height of the chart in pixels.
31
+ */
32
+ height?: number | undefined;
33
+ /**
34
+ * Width of the chart in pixels.
35
+ */
36
+ width?: number | undefined;
37
+ /**
38
+ * Width of the bars in the chart in pixels.
39
+ */
40
+ barMinWidth?: number | undefined;
41
+ /**
42
+ * Minimum height of the bars in the chart in pixels.
43
+ */
44
+ barMinHeight?: number | undefined;
45
+ /**
46
+ * Bars below this value will be hidden, showing as 0 height.
47
+ */
48
+ hideBarBelow?: number | undefined;
49
+ /**
50
+ * Whether or not the bar should grow to fill out the full width of the chart.
51
+ */
52
+ grow?: boolean | undefined;
53
+ /**
54
+ * The side the bars should align to when they do not completely fill out the chart.
55
+ */
56
+ align?: "left" | "right" | undefined;
57
+ /**
58
+ * Gap between the bars in pixels.
59
+ */
60
+ gap?: number | undefined;
61
+ /**
62
+ * Color of the bars, can be any valid CSS color.
63
+ */
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
+ */
72
+ fadeOpacity?: number | undefined;
73
+ /**
74
+ * Boolean whether or not this chart can be hovered at all.
75
+ */
76
+ hover?: boolean | undefined;
77
+ /**
78
+ * Transition the chart between different stats. Value is time in milliseconds.
79
+ */
80
+ transition?: number | undefined;
81
+ /**
82
+ * Boolean whether or not a value will be shown.
83
+ */
84
+ showValue?: boolean | undefined;
85
+ /**
86
+ * Default value when not hovering.
87
+ */
88
+ valueDefault?: string | undefined;
89
+ /**
90
+ * String to prepend the value.
91
+ */
92
+ valuePrepend?: string | undefined;
93
+ /**
94
+ * String to append to the value.
95
+ */
96
+ valueAppend?: string | undefined;
97
+ /**
98
+ * For when the hovering value returns undefined.
99
+ */
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
+ */
108
+ scaleMax?: number | undefined;
109
+ /**
110
+ * Use this to overwrite the default value floor of 0.
111
+ */
112
+ scaleMin?: number | undefined;
113
+ /**
114
+ * Can be set to "line" to display a line chart instead.
115
+ */
116
+ type?: "bar" | "line" | undefined;
117
+ /**
118
+ * Color of the line if used with type="line".
119
+ */
120
+ lineColor?: string | undefined;
121
+ /**
122
+ * Sets whether or not the SVG will preserve it's aspect ratio.
123
+ */
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
+ */
140
+ onclick?: ((args: {
141
+ key?: string | undefined;
142
+ index?: number | undefined;
143
+ }) => void) | undefined;
144
+ /**
145
+ * Function that executes when a value in the chart updates.
146
+ */
147
+ onvalueupdate?: ((args: {
148
+ value?: number | null | undefined;
149
+ uid?: string | undefined;
150
+ linkedKey?: string | undefined;
151
+ valueElement?: HTMLElement | null | undefined;
152
+ }) => void) | undefined;
153
+ /**
154
+ * Function that executes on hover of each bar, also fires on tab focus.
155
+ */
156
+ onhover?: ((args: {
157
+ /**
158
+ * ,
159
+ */
160
+ uid: string;
161
+ /**
162
+ * ,
163
+ */
164
+ key: string;
165
+ /**
166
+ * ,
167
+ */
168
+ index: number;
169
+ /**
170
+ * ,
171
+ */
172
+ linkedKey: string;
173
+ /**
174
+ * ,
175
+ */
176
+ value: number | null;
177
+ /**
178
+ * ,
179
+ */
180
+ valueElement: HTMLElement;
181
+ eventElement: EventTarget | null;
182
+ }) => void) | undefined;
183
+ /**
184
+ * Function that executes when hover or focus leaves the chart.
185
+ */
186
+ onblur?: ((args: {
187
+ /**
188
+ * ,
189
+ */
190
+ uid: string;
191
+ /**
192
+ * ,
193
+ */
194
+ linkedKey: string;
195
+ /**
196
+ * ,
197
+ */
198
+ valueElement: HTMLElement;
199
+ eventElement: EventTarget | null;
200
+ }) => void) | undefined;
41
201
  } & {
42
- [evt: string]: CustomEvent<any>;
43
- }, {}> {
44
- }
45
- export type LinkedChartProps = typeof __propDef.props;
46
- export type LinkedChartEvents = typeof __propDef.events;
47
- export type LinkedChartSlots = typeof __propDef.slots;
48
- import { SvelteComponent } from "svelte";
49
- declare const __propDef: {
50
- props: {
51
- [x: string]: any;
52
- hover?: boolean;
53
- uid?: string;
54
- data?: {};
55
- labels?: any[];
56
- values?: any[];
57
- linked?: string;
58
- height?: number;
59
- width?: number;
60
- barMinWidth?: number;
61
- barMinHeight?: number;
62
- hideBarBelow?: number;
63
- grow?: boolean;
64
- align?: string;
65
- gap?: number;
66
- fill?: string;
67
- fadeOpacity?: number;
68
- transition?: number;
69
- showValue?: boolean;
70
- valueDefault?: string;
71
- valuePrepend?: string;
72
- valueAppend?: string;
73
- valuePosition?: string;
74
- valueUndefined?: number;
75
- scaleMax?: number;
76
- scaleMin?: number;
77
- type?: string;
78
- lineColor?: string;
79
- tabindex?: number;
80
- dispatchEvents?: boolean;
81
- preserveAspectRatio?: boolean;
82
- clickHandler?: (key: any, i: any) => any;
83
- };
84
- events: {
85
- 'value-update': CustomEvent<any>;
86
- hover: CustomEvent<any>;
87
- blur: CustomEvent<any>;
88
- } & {
89
- [evt: string]: CustomEvent<any>;
90
- };
91
- slots: {};
202
+ [key: string]: any;
203
+ }, {}, "data">;
204
+ type Props = {
205
+ /**
206
+ * Unique ID to link this chart to a LinkedValue component with the same uid.
207
+ */
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
+ */
212
+ data?: Record<string, number> | undefined;
213
+ /**
214
+ * Labels supplied separately, to be used together with `values` property.
215
+ */
216
+ labels?: string[] | undefined;
217
+ /**
218
+ * Values supplied separately, to be used together with `labels` property.
219
+ */
220
+ values?: number[] | undefined;
221
+ /**
222
+ * Key to link this chart to other charts with the same key.
223
+ */
224
+ linked?: string | undefined;
225
+ /**
226
+ * Height of the chart in pixels.
227
+ */
228
+ height?: number | undefined;
229
+ /**
230
+ * Width of the chart in pixels.
231
+ */
232
+ width?: number | undefined;
233
+ /**
234
+ * Width of the bars in the chart in pixels.
235
+ */
236
+ barMinWidth?: number | undefined;
237
+ /**
238
+ * Minimum height of the bars in the chart in pixels.
239
+ */
240
+ barMinHeight?: number | undefined;
241
+ /**
242
+ * Bars below this value will be hidden, showing as 0 height.
243
+ */
244
+ hideBarBelow?: number | undefined;
245
+ /**
246
+ * Whether or not the bar should grow to fill out the full width of the chart.
247
+ */
248
+ grow?: boolean | undefined;
249
+ /**
250
+ * The side the bars should align to when they do not completely fill out the chart.
251
+ */
252
+ align?: "left" | "right" | undefined;
253
+ /**
254
+ * Gap between the bars in pixels.
255
+ */
256
+ gap?: number | undefined;
257
+ /**
258
+ * Color of the bars, can be any valid CSS color.
259
+ */
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
+ */
268
+ fadeOpacity?: number | undefined;
269
+ /**
270
+ * Boolean whether or not this chart can be hovered at all.
271
+ */
272
+ hover?: boolean | undefined;
273
+ /**
274
+ * Transition the chart between different stats. Value is time in milliseconds.
275
+ */
276
+ transition?: number | undefined;
277
+ /**
278
+ * Boolean whether or not a value will be shown.
279
+ */
280
+ showValue?: boolean | undefined;
281
+ /**
282
+ * Default value when not hovering.
283
+ */
284
+ valueDefault?: string | undefined;
285
+ /**
286
+ * String to prepend the value.
287
+ */
288
+ valuePrepend?: string | undefined;
289
+ /**
290
+ * String to append to the value.
291
+ */
292
+ valueAppend?: string | undefined;
293
+ /**
294
+ * For when the hovering value returns undefined.
295
+ */
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
+ */
304
+ scaleMax?: number | undefined;
305
+ /**
306
+ * Use this to overwrite the default value floor of 0.
307
+ */
308
+ scaleMin?: number | undefined;
309
+ /**
310
+ * Can be set to "line" to display a line chart instead.
311
+ */
312
+ type?: "bar" | "line" | undefined;
313
+ /**
314
+ * Color of the line if used with type="line".
315
+ */
316
+ lineColor?: string | undefined;
317
+ /**
318
+ * Sets whether or not the SVG will preserve it's aspect ratio.
319
+ */
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
+ */
336
+ onclick?: ((args: {
337
+ key?: string | undefined;
338
+ index?: number | undefined;
339
+ }) => void) | undefined;
340
+ /**
341
+ * Function that executes when a value in the chart updates.
342
+ */
343
+ onvalueupdate?: ((args: {
344
+ value?: number | null | undefined;
345
+ uid?: string | undefined;
346
+ linkedKey?: string | undefined;
347
+ valueElement?: HTMLElement | null | undefined;
348
+ }) => void) | undefined;
349
+ /**
350
+ * Function that executes on hover of each bar, also fires on tab focus.
351
+ */
352
+ onhover?: ((args: {
353
+ /**
354
+ * ,
355
+ */
356
+ uid: string;
357
+ /**
358
+ * ,
359
+ */
360
+ key: string;
361
+ /**
362
+ * ,
363
+ */
364
+ index: number;
365
+ /**
366
+ * ,
367
+ */
368
+ linkedKey: string;
369
+ /**
370
+ * ,
371
+ */
372
+ value: number | null;
373
+ /**
374
+ * ,
375
+ */
376
+ valueElement: HTMLElement;
377
+ eventElement: EventTarget | null;
378
+ }) => void) | undefined;
379
+ /**
380
+ * Function that executes when hover or focus leaves the chart.
381
+ */
382
+ onblur?: ((args: {
383
+ /**
384
+ * ,
385
+ */
386
+ uid: string;
387
+ /**
388
+ * ,
389
+ */
390
+ linkedKey: string;
391
+ /**
392
+ * ,
393
+ */
394
+ valueElement: HTMLElement;
395
+ eventElement: EventTarget | null;
396
+ }) => void) | undefined;
92
397
  };
93
- export {};
@@ -1,15 +1,16 @@
1
1
  <script>
2
2
  import { hoveringKey } from "./stores/tinyLinkedCharts.js"
3
3
 
4
- export let linked
5
- export let empty = "&nbsp;"
6
- export let transform = (label) => label
4
+ /** @type {{ linked: string, empty?: string, transform?: (label: string) => string }} */
5
+ let { linked, empty = "&nbsp;", transform = (label = "") => label } = $props()
7
6
 
8
- $: label = $hoveringKey[linked]
7
+ /** @type {string | null} */
8
+ let label = $derived($hoveringKey[linked])
9
9
  </script>
10
10
 
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}
@@ -1,27 +1,15 @@
1
- /** @typedef {typeof __propDef.props} LinkedLabelProps */
2
- /** @typedef {typeof __propDef.events} LinkedLabelEvents */
3
- /** @typedef {typeof __propDef.slots} LinkedLabelSlots */
4
- export default class LinkedLabel extends SvelteComponent<{
5
- linked: any;
1
+ export default LinkedLabel;
2
+ type LinkedLabel = {
3
+ $on?(type: string, callback: (e: any) => void): () => void;
4
+ $set?(props: Partial<$$ComponentProps>): void;
5
+ };
6
+ declare const LinkedLabel: import("svelte").Component<{
7
+ linked: string;
8
+ empty?: string;
9
+ transform?: (label: string) => string;
10
+ }, {}, "">;
11
+ type $$ComponentProps = {
12
+ linked: string;
6
13
  empty?: string;
7
- transform?: (label: any) => any;
8
- }, {
9
- [evt: string]: CustomEvent<any>;
10
- }, {}> {
11
- }
12
- export type LinkedLabelProps = typeof __propDef.props;
13
- export type LinkedLabelEvents = typeof __propDef.events;
14
- export type LinkedLabelSlots = typeof __propDef.slots;
15
- import { SvelteComponent } from "svelte";
16
- declare const __propDef: {
17
- props: {
18
- linked: any;
19
- empty?: string;
20
- transform?: (label: any) => any;
21
- };
22
- events: {
23
- [evt: string]: CustomEvent<any>;
24
- };
25
- slots: {};
14
+ transform?: (label: string) => string;
26
15
  };
27
- export {};
@@ -1,16 +1,21 @@
1
1
  <script>
2
2
  import { hoveringValue } from "./stores/tinyLinkedCharts.js"
3
3
 
4
- export let uid
5
- export let empty = "&nbsp;"
6
- export let valueUndefined = 0
7
- export let transform = (value) => value
4
+ /** @type {{ uid: string, empty?: string, valueUndefined?: number, transform?: (value: number) => number | string }} */
5
+ let {
6
+ uid,
7
+ empty = "&nbsp;",
8
+ valueUndefined = 0,
9
+ transform = (value) => value
10
+ } = $props()
8
11
 
9
- $: value = $hoveringValue[uid]
12
+ /** @type {number | null} */
13
+ let value = $derived($hoveringValue[uid])
10
14
  </script>
11
15
 
12
16
  {#if uid in $hoveringValue && value !== null}
13
17
  {transform(value) || valueUndefined}
14
18
  {:else}
19
+ <!-- eslint-disable-next-line svelte/no-at-html-tags -->
15
20
  {@html empty}
16
21
  {/if}
@@ -1,29 +1,17 @@
1
- /** @typedef {typeof __propDef.props} LinkedValueProps */
2
- /** @typedef {typeof __propDef.events} LinkedValueEvents */
3
- /** @typedef {typeof __propDef.slots} LinkedValueSlots */
4
- export default class LinkedValue extends SvelteComponent<{
5
- uid: any;
1
+ export default LinkedValue;
2
+ type LinkedValue = {
3
+ $on?(type: string, callback: (e: any) => void): () => void;
4
+ $set?(props: Partial<$$ComponentProps>): void;
5
+ };
6
+ declare const LinkedValue: import("svelte").Component<{
7
+ uid: string;
8
+ empty?: string;
6
9
  valueUndefined?: number;
10
+ transform?: (value: number) => number | string;
11
+ }, {}, "">;
12
+ type $$ComponentProps = {
13
+ uid: string;
7
14
  empty?: string;
8
- transform?: (value: any) => any;
9
- }, {
10
- [evt: string]: CustomEvent<any>;
11
- }, {}> {
12
- }
13
- export type LinkedValueProps = typeof __propDef.props;
14
- export type LinkedValueEvents = typeof __propDef.events;
15
- export type LinkedValueSlots = typeof __propDef.slots;
16
- import { SvelteComponent } from "svelte";
17
- declare const __propDef: {
18
- props: {
19
- uid: any;
20
- valueUndefined?: number;
21
- empty?: string;
22
- transform?: (value: any) => any;
23
- };
24
- events: {
25
- [evt: string]: CustomEvent<any>;
26
- };
27
- slots: {};
15
+ valueUndefined?: number;
16
+ transform?: (value: number) => number | string;
28
17
  };
29
- export {};
@@ -1,3 +1,4 @@
1
- /// <reference types="svelte" />
2
- export const hoveringKey: import("svelte/store").Writable<{}>;
3
- export const hoveringValue: import("svelte/store").Writable<{}>;
1
+ /** @type {import('svelte/store').Writable<Record<string, string | null>>} */
2
+ export const hoveringKey: import("svelte/store").Writable<Record<string, string | null>>;
3
+ /** @type {import('svelte/store').Writable<Record<string, number | null>>} */
4
+ export const hoveringValue: import("svelte/store").Writable<Record<string, number | null>>;
@@ -1,4 +1,7 @@
1
1
  import { writable } from "svelte/store"
2
2
 
3
+ /** @type {import('svelte/store').Writable<Record<string, string | null>>} */
3
4
  export const hoveringKey = writable({})
5
+
6
+ /** @type {import('svelte/store').Writable<Record<string, number | null>>} */
4
7
  export const hoveringValue = writable({})
package/package.json CHANGED
@@ -1,69 +1,71 @@
1
- {
2
- "name": "svelte-tiny-linked-charts",
3
- "version": "1.6.2",
4
- "scripts": {
5
- "dev": "vite dev",
6
- "build": "vite build && npm run package",
7
- "preview": "vite preview",
8
- "package": "svelte-kit sync && svelte-package && publint",
9
- "prepublishOnly": "npm run package",
10
- "check": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json",
11
- "check:watch": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json --watch",
12
- "test": "vitest",
13
- "lint": "prettier --plugin-search-dir . --check . && eslint .",
14
- "format": "prettier --plugin-search-dir . --write .",
15
- "publish-pages": "npm run build && git subtree push --prefix build origin gh-pages"
16
- },
17
- "exports": {
18
- ".": {
19
- "types": "./dist/index.d.ts",
20
- "svelte": "./dist/index.js"
21
- }
22
- },
23
- "files": [
24
- "dist",
25
- "!dist/**/*.test.*",
26
- "!dist/**/*.spec.*"
27
- ],
28
- "peerDependencies": {
29
- "svelte": "^4.0.0 || >= 5.0.0"
30
- },
31
- "devDependencies": {
32
- "@sveltejs/adapter-auto": "^2.0.0",
33
- "@sveltejs/adapter-static": "^2.0.3",
34
- "@sveltejs/kit": "^1.20.4",
35
- "@sveltejs/package": "^2.0.0",
36
- "@testing-library/svelte": "^4.0.3",
37
- "eslint": "^8.28.0",
38
- "eslint-config-prettier": "^8.5.0",
39
- "eslint-plugin-svelte": "^2.30.0",
40
- "jsdom": "^22.1.0",
41
- "prettier": "^2.8.0",
42
- "prettier-plugin-svelte": "^2.10.1",
43
- "publint": "^0.1.9",
44
- "svelte": "^4.0.5",
45
- "svelte-check": "^3.4.3",
46
- "tslib": "^2.4.1",
47
- "typescript": "^5.0.0",
48
- "vite": "^4.4.2",
49
- "vitest": "^0.34.0"
50
- },
51
- "svelte": "./dist/index.js",
52
- "types": "./dist/index.d.ts",
53
- "main": "./dist/index.js",
54
- "type": "module",
55
- "description": "A library to display tiny bar charts using Svelte. These charts are more so meant for graphic aids, rather than scientific representations. There's no axis labels, no extensive data visualisation, just bars.",
56
- "keywords": [
57
- "svelte",
58
- "tiny",
59
- "charts",
60
- "linked",
61
- "linked-charts",
62
- "tiny-linked-charts"
63
- ],
64
- "repository": {
65
- "type": "git",
66
- "url": "https://github.com/Mitcheljager/svelte-tiny-linked-charts"
67
- },
68
- "homepage": "https://mitcheljager.github.io/svelte-tiny-linked-charts/"
69
- }
1
+ {
2
+ "name": "svelte-tiny-linked-charts",
3
+ "version": "2.2.0",
4
+ "scripts": {
5
+ "dev": "vite dev",
6
+ "build": "vite build && npm run package",
7
+ "preview": "vite preview",
8
+ "package": "svelte-kit sync && svelte-package && publint",
9
+ "prepublishOnly": "npm run package",
10
+ "check": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json",
11
+ "check:watch": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json --watch",
12
+ "test": "vitest",
13
+ "lint": "prettier --plugin-search-dir . --check . && eslint .",
14
+ "format": "prettier --plugin-search-dir . --write .",
15
+ "publish-pages": "npm run build && git subtree push --prefix build origin gh-pages"
16
+ },
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "svelte": "./dist/index.js"
21
+ }
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "!dist/**/*.test.*",
26
+ "!dist/**/*.spec.*"
27
+ ],
28
+ "peerDependencies": {
29
+ "svelte": ">=5.0.0"
30
+ },
31
+ "devDependencies": {
32
+ "@sveltejs/adapter-static": "^3.0.0",
33
+ "@sveltejs/adapter-auto": "^3.0.0",
34
+ "@sveltejs/kit": "^2.5.27",
35
+ "@sveltejs/vite-plugin-svelte": "^4.0.0",
36
+ "@sveltejs/package": "^2.3.7",
37
+ "@testing-library/svelte": "^5.2.6",
38
+ "eslint": "^9.18.0",
39
+ "eslint-config-prettier": "^10.0.1",
40
+ "eslint-plugin-svelte": "^2.46.1",
41
+ "globals": "^15.14.0",
42
+ "happy-dom": "^16.5.3",
43
+ "prettier": "^3.1.0",
44
+ "prettier-plugin-svelte": "^3.2.6",
45
+ "publint": "^0.1.9",
46
+ "svelte": "^5.0.0",
47
+ "svelte-check": "^4.0.0",
48
+ "tslib": "^2.4.1",
49
+ "typescript": "^5.5.0",
50
+ "vite": "^5.4.4",
51
+ "vitest": "^2.1.8"
52
+ },
53
+ "svelte": "./dist/index.js",
54
+ "types": "./dist/index.d.ts",
55
+ "main": "./dist/index.js",
56
+ "type": "module",
57
+ "description": "A library to display tiny bar charts using Svelte. These charts are more so meant for graphic aids, rather than scientific representations. There's no axis labels, no extensive data visualisation, just bars.",
58
+ "keywords": [
59
+ "svelte",
60
+ "tiny",
61
+ "charts",
62
+ "linked",
63
+ "linked-charts",
64
+ "tiny-linked-charts"
65
+ ],
66
+ "repository": {
67
+ "type": "git",
68
+ "url": "https://github.com/Mitcheljager/svelte-tiny-linked-charts"
69
+ },
70
+ "homepage": "https://mitcheljager.github.io/svelte-tiny-linked-charts/"
71
+ }