svelte-tiny-linked-charts 2.3.2 → 2.4.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/README.md CHANGED
@@ -105,6 +105,7 @@ scaleMax | 0 | Use this to overwrite the default value floor of 0.
105
105
  type | bar | Can be set to "line" to display a line chart instead.
106
106
  lineColor | fill | Color of the line if used with type="line".
107
107
  lineFill | transparent | Color of the fill area if used with type="line".
108
+ lineFillGradient | null | Gradient stops of fill area if used with type="line", each value being a stop for any css color.
108
109
  lineWidth | 1 | Width of the line if used with type="line".
109
110
  lineDotRadius | 0 (derived) | The size of the dot when hovering a line if used with type="line".
110
111
  preserveAspectRatio | false | Sets whether or not the SVG will preserve it's aspect ratio.
@@ -62,6 +62,7 @@
62
62
  * @property {"bar" | "line"} [type] Can be set to "line" to display a line chart instead.
63
63
  * @property {string} [lineColor] Color of the line if used with type="line".
64
64
  * @property {string} [lineFill] Color of the fill area if used with type="line".
65
+ * @property {string[] | null} [lineFillGradient] Gradient stops of fill area if used with type="line", each value being a stop for any css color.
65
66
  * @property {number} [lineWidth] Width of the line if used with type="line".
66
67
  * @property {number} [lineDotRadius] The size of the dot when hovering a line if used with type="line".
67
68
  * @property {boolean} [preserveAspectRatio] Sets whether or not the SVG will preserve it's aspect ratio.
@@ -105,6 +106,7 @@
105
106
  type = "bar",
106
107
  lineColor = fill,
107
108
  lineFill = "transparent",
109
+ lineFillGradient = null,
108
110
  lineWidth = 1,
109
111
  lineDotRadius = 0,
110
112
  preserveAspectRatio = false,
@@ -119,14 +121,13 @@
119
121
  } = $props()
120
122
 
121
123
  let valuePositionOffset = $state(0)
122
- /** @type {number[][]} */
123
- let polyline = $state([])
124
124
  let valueElement = $state()
125
125
  let dataLength = $derived(Object.keys(data).length)
126
126
  let barWidth = $derived(dataLength && grow ? getBarWidth() : barMinWidth)
127
127
  let highestValue = $derived(data ? getHighestValue() : 0)
128
128
  let alignmentOffset = $derived(dataLength ? getAlignment() : 0)
129
129
  let linkedKey = $derived(linked || (Math.random() + 1).toString(36).substring(7))
130
+ let polyline = $derived(type === "line" ? getPolylinePoints(data) : [])
130
131
 
131
132
  $effect(() => {
132
133
  if (labels.length && values.length) data = Object.fromEntries(labels.map((_, i) => [labels[i], values[i]]))
@@ -140,10 +141,6 @@
140
141
  if (valuePosition === "floating") valuePositionOffset = (gap + barWidth) * Object.keys(data).indexOf($hoveringKey[linkedKey] || "") + alignmentOffset
141
142
  })
142
143
 
143
- $effect(() => {
144
- if (type == "line") polyline = getPolyLinePoints(data)
145
- })
146
-
147
144
  $effect(() => {
148
145
  onvalueupdate({ value: $hoveringValue[uid], uid, linkedKey, valueElement })
149
146
  })
@@ -185,7 +182,7 @@
185
182
  }
186
183
 
187
184
  /** @param {Record<string, number>} data */
188
- function getPolyLinePoints(data) {
185
+ function getPolylinePoints(data) {
189
186
  let points = []
190
187
 
191
188
  for (let i = 0; i < Object.keys(data).length; i++) {
@@ -244,10 +241,20 @@
244
241
  <desc>{description}</desc>
245
242
  {/if}
246
243
 
244
+ {#if lineFillGradient}
245
+ <defs>
246
+ <linearGradient id="chart-{uid}-gradient" x1="0" x2="0" y1="0" y2="1">
247
+ {#each lineFillGradient as color, index}
248
+ <stop offset="{100 / (lineFillGradient.length - 1) * index}%" stop-color={color} />
249
+ {/each}
250
+ </linearGradient>
251
+ </defs>
252
+ {/if}
253
+
247
254
  <g transform="translate({alignmentOffset}, 0)">
248
255
  {#if type == "line"}
249
- {#if lineFill && lineFill !== "transparent"}
250
- <polyline points={[...polyline, [polyline[polyline.length - 1]?.[0], height], [0, height]].join(" ")} fill={lineFill} />
256
+ {#if (lineFill && lineFill !== "transparent") || lineFillGradient}
257
+ <polygon points={[...polyline, [polyline[polyline.length - 1]?.[0], height], [0, height]].join(" ")} fill={lineFillGradient ? `url(#chart-${uid}-gradient)` : lineFill} />
251
258
  {/if}
252
259
 
253
260
  <polyline points={polyline.join(" ")} stroke={lineColor} stroke-width={lineWidth} fill="transparent" />
@@ -122,6 +122,10 @@ declare const LinkedChart: import("svelte").Component<{
122
122
  * Color of the fill area if used with type="line".
123
123
  */
124
124
  lineFill?: string | undefined;
125
+ /**
126
+ * Gradient stops of fill area if used with type="line", each value being a stop for any css color.
127
+ */
128
+ lineFillGradient?: string[] | null | undefined;
125
129
  /**
126
130
  * Width of the line if used with type="line".
127
131
  */
@@ -330,6 +334,10 @@ type Props = {
330
334
  * Color of the fill area if used with type="line".
331
335
  */
332
336
  lineFill?: string | undefined;
337
+ /**
338
+ * Gradient stops of fill area if used with type="line", each value being a stop for any css color.
339
+ */
340
+ lineFillGradient?: string[] | null | undefined;
333
341
  /**
334
342
  * Width of the line if used with type="line".
335
343
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-tiny-linked-charts",
3
- "version": "2.3.2",
3
+ "version": "2.4.0",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "vite build && npm run package",