svelte-tiny-linked-charts 1.0.4 → 1.1.1
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 +7 -1
- package/package.json +4 -4
- package/src/LinkedChart.svelte +26 -9
- package/src/LinkedValue.svelte +3 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Tiny Linked Charts for Svelte
|
|
2
2
|
|
|
3
|
-
This is a library to display tiny bar charts. These charts are more so meant for
|
|
3
|
+
This is a library to display tiny bar charts. These charts are more so meant for graphic aids, rather than scientific representations. There's no axis labels, no extensive data visualisation, just bars.
|
|
4
4
|
|
|
5
5
|
**Demo and Docs**: https://mitcheljager.github.io/svelte-tiny-linked-charts/
|
|
6
6
|
|
|
@@ -81,10 +81,15 @@ hover | true | Boolean whether or not this chart can be hovered at all.
|
|
|
81
81
|
transition | 0 | Transition the chart between different stats. Value is time in milliseconds.
|
|
82
82
|
showValue | false | Boolean whether or not a value will be shown.
|
|
83
83
|
valueDefault | "\ " | Default value when not hovering.
|
|
84
|
+
valueUndefined | 0 | For when the hovering value returns undefined.
|
|
84
85
|
valuePrepend | | String to prepend the value.
|
|
85
86
|
valueAppend | | String to append to the value.
|
|
86
87
|
valuePosition | static | Can be set to "floating" to follow the position of the hover.
|
|
87
88
|
scaleMax | 0 | Use this to overwrite the automatic scale set to the highest value in your array.
|
|
89
|
+
type | bar | Can be set to "line" to display a line chart instead.
|
|
90
|
+
lineColor | fill | Color of the line if used with type="line".
|
|
91
|
+
tabindex | -1 | Sets the tabindex of each bar.
|
|
92
|
+
dispatchEvents | false | Boolean whether or not to dispatch events on certain actions.
|
|
88
93
|
|
|
89
94
|
`<LinkedLabel />` component.
|
|
90
95
|
Property | Default | Description
|
|
@@ -97,3 +102,4 @@ Property | Default | Description
|
|
|
97
102
|
--- | --- | ---
|
|
98
103
|
uid | | Unique ID to link this value to a chart with the same uid.
|
|
99
104
|
empty | \ | String that will be displayed when no bar is being hovered.
|
|
105
|
+
valueUndefined | 0 | For when the hovering value returns undefined.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "svelte-tiny-linked-charts",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "rollup -c",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"svelte-jest": "^0.3.1",
|
|
28
28
|
"svelte-jester": "^2.0.1"
|
|
29
29
|
},
|
|
30
|
-
"description": "A library to display tiny bar charts using Svelte. These charts are more so meant for
|
|
30
|
+
"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.",
|
|
31
31
|
"keywords": [
|
|
32
32
|
"svelte",
|
|
33
33
|
"tiny",
|
|
@@ -40,8 +40,8 @@
|
|
|
40
40
|
"src/"
|
|
41
41
|
],
|
|
42
42
|
"license": "MIT",
|
|
43
|
-
"main": "
|
|
44
|
-
"module": "
|
|
43
|
+
"main": "src/index.js",
|
|
44
|
+
"module": "src/index.mjs",
|
|
45
45
|
"svelte": "src/index.js",
|
|
46
46
|
"author": {
|
|
47
47
|
"email": "mitchel_jager@hotmail.com",
|
package/src/LinkedChart.svelte
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
<script>
|
|
2
|
+
import { createEventDispatcher, tick } from "svelte"
|
|
2
3
|
import { hoveringKey, hoveringValue } from "./stores/tinyLinkedCharts.js"
|
|
3
4
|
|
|
4
5
|
export let uid = (Math.random() + 1).toString(36).substring(7)
|
|
@@ -21,25 +22,32 @@
|
|
|
21
22
|
export let valuePrepend = ""
|
|
22
23
|
export let valueAppend = ""
|
|
23
24
|
export let valuePosition = "static"
|
|
25
|
+
export let valueUndefined = 0
|
|
24
26
|
export let scaleMax = 0
|
|
25
27
|
export let type = "bar"
|
|
26
28
|
export let lineColor = fill
|
|
29
|
+
export let tabindex = -1
|
|
30
|
+
export let dispatchEvents = false
|
|
31
|
+
|
|
32
|
+
const dispatch = createEventDispatcher()
|
|
27
33
|
|
|
28
34
|
let valuePositionOffset = 0
|
|
29
35
|
let polyline = []
|
|
30
|
-
|
|
36
|
+
let valueElement
|
|
37
|
+
|
|
31
38
|
$: dataLength = Object.keys(data).length
|
|
32
39
|
$: barWidth = grow ? getBarWidth(dataLength) : parseInt(barMinWidth)
|
|
33
40
|
$: highestValue = getHighestValue(dataLength)
|
|
34
41
|
$: alignmentOffset = dataLength ? getAlignment() : 0
|
|
35
42
|
$: linkedKey = linked || (Math.random() + 1).toString(36).substring(7)
|
|
36
43
|
$: if (labels.length && values.length) data = Object.fromEntries(labels.map((_, i) => [labels[i], values[i]]))
|
|
37
|
-
$: if (valuePosition == "floating") valuePositionOffset = (parseInt(gap) + barWidth) * Object.keys(data).indexOf($hoveringKey[linkedKey])
|
|
44
|
+
$: if (valuePosition == "floating") valuePositionOffset = (parseInt(gap) + barWidth) * Object.keys(data).indexOf($hoveringKey[linkedKey]) + alignmentOffset
|
|
38
45
|
$: if (type == "line") polyline = getPolyLinePoints(data)
|
|
46
|
+
$: if (dispatchEvents) dispatch('value-update', { value: $hoveringValue[uid], uid, linkedKey, valueElement })
|
|
39
47
|
$: {
|
|
40
48
|
if ($hoveringKey[linkedKey]) {
|
|
41
49
|
$hoveringValue[uid] = data[$hoveringKey[linkedKey]]
|
|
42
|
-
} else {
|
|
50
|
+
} else {
|
|
43
51
|
$hoveringValue[uid] = null
|
|
44
52
|
}
|
|
45
53
|
}
|
|
@@ -51,7 +59,7 @@
|
|
|
51
59
|
}
|
|
52
60
|
|
|
53
61
|
function getHeight(value) {
|
|
54
|
-
return Math.round((parseInt(height) / highestValue) * value - (type == "line" ? barWidth / 2 : 0))
|
|
62
|
+
return Math.round((parseInt(height) / highestValue) * value - (type == "line" ? barWidth / 2 : 0)) || 0
|
|
55
63
|
}
|
|
56
64
|
|
|
57
65
|
function getBarWidth() {
|
|
@@ -73,14 +81,22 @@
|
|
|
73
81
|
return points
|
|
74
82
|
}
|
|
75
83
|
|
|
76
|
-
function startHover(key, index) {
|
|
84
|
+
async function startHover(key, index) {
|
|
77
85
|
if (!hover) return
|
|
78
86
|
$hoveringKey[linkedKey] = key
|
|
87
|
+
|
|
88
|
+
await tick()
|
|
89
|
+
|
|
90
|
+
if (dispatchEvents) dispatch('hover', { uid, key, index, linkedKey, value: $hoveringValue[uid], valueElement, eventElement: event.target })
|
|
79
91
|
}
|
|
80
92
|
|
|
81
|
-
function endHover() {
|
|
93
|
+
async function endHover() {
|
|
82
94
|
if (!hover) return
|
|
83
95
|
$hoveringKey[linkedKey] = null
|
|
96
|
+
|
|
97
|
+
await tick()
|
|
98
|
+
|
|
99
|
+
if (dispatchEvents) dispatch('blur', { uid, linkedKey, valueElement, eventElement: event.target })
|
|
84
100
|
}
|
|
85
101
|
</script>
|
|
86
102
|
|
|
@@ -110,7 +126,8 @@
|
|
|
110
126
|
width={ barWidth }
|
|
111
127
|
height={ type == "line" ? height : getHeight(value) }
|
|
112
128
|
y={ type == "line" ? 0 : (height - getHeight(value)) }
|
|
113
|
-
x={ (parseInt(gap) + barWidth) * i }
|
|
129
|
+
x={ (parseInt(gap) + barWidth) * i }
|
|
130
|
+
{ tabindex } />
|
|
114
131
|
|
|
115
132
|
{ #if type == "line" }
|
|
116
133
|
<circle
|
|
@@ -123,11 +140,11 @@
|
|
|
123
140
|
</g>
|
|
124
141
|
</svg>
|
|
125
142
|
|
|
126
|
-
{ #if showValue }
|
|
143
|
+
{ #if showValue && ($hoveringValue[uid] || valueDefault) }
|
|
127
144
|
<div class="tiny-linked-charts-value" style={ valuePosition == "floating" ? `position: absolute; transform: translateX(${ valuePositionOffset }px)` : null }>
|
|
128
145
|
{ #if $hoveringValue[uid] !== null }
|
|
129
146
|
{ valuePrepend }
|
|
130
|
-
{ $hoveringValue[uid] }
|
|
147
|
+
<span bind:this={valueElement}>{ $hoveringValue[uid] || valueUndefined }</span>
|
|
131
148
|
{ valueAppend }
|
|
132
149
|
{ :else }
|
|
133
150
|
{ @html valueDefault }
|
package/src/LinkedValue.svelte
CHANGED
|
@@ -3,14 +3,15 @@
|
|
|
3
3
|
|
|
4
4
|
export let uid
|
|
5
5
|
export let empty = " "
|
|
6
|
+
export let valueUndefined = 0
|
|
6
7
|
|
|
7
8
|
$: value = $hoveringValue[uid]
|
|
8
9
|
</script>
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
|
|
12
|
-
{ #if value }
|
|
13
|
-
{ value }
|
|
13
|
+
{ #if value !== null }
|
|
14
|
+
{ value || valueUndefined }
|
|
14
15
|
{ :else }
|
|
15
16
|
{ @html empty }
|
|
16
17
|
{ /if }
|