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.
Inspired by steamcharts.com
Name | 30 day period | Value |
---|---|---|
A thing | 2289 | |
Another thing | 2179 | |
A third thing | 2189 | |
An incomplete thing | 1128 | |
A changing thing | 2337 | |
A thing using lines | 2167 |
Install using Yarn or NPM.
yarn add svelte-tiny-linked-charts
npm install --save svelte-tiny-linked-charts
Include the chart in your app.
<LinkedChart { data } />
import {
LinkedChart,
LinkedLabel,
LinkedValue
} from "svelte-tiny-linked-charts"
Supply your data in a simple key:value object:
let data = {
"2005-01-01": 25,
"2005-01-02": 20,
"2005-01-03": 18,
"2005-01-04": 17,
"2005-01-05": 21
}
<LinkedChart { data } />
Or if you prefer supply the labels and values separately:
let labels = [
"2005-01-01",
"2005-01-02",
"2005-01-03",
"2005-01-04",
"2005-01-05"
]
let values = [
25,
20,
18,
17,
21
]
<LinkedChart { labels } { values } />
<LinkedChart { data } />
<LinkedChart { data } linked="link-1" />
<LinkedChart { data } linked="link-1" />
<LinkedChart { data } linked="link-1" />
<LinkedChart { data } linked="link-1" />
<LinkedChart { data } scaleMax="100" />
<LinkedChart { data } scaleMax="100" />
<LinkedChart { data } scaleMin="30" scaleMax="31" />
<LinkedChart { data } scaleMin="5000" scaleMax="5010" />
<LinkedLabel linked="link-2" />
<LinkedChart { data } linked="link-2" />
<LinkedChart { data } linked="link-2" />
<LinkedChart { data } showValue />
<LinkedChart
{ data }
showValue
valueDefault="Empty label"
valuePrepend="Thing:"
valueAppend="views" />
<LinkedChart
{ data }
showValue
valuePosition="floating" />
<LinkedChart { data } uid="some-id" />
<LinkedValue uid="some-id" />
The value can be transformed in order to append, prepend, or otherwise format the value. This is done using the transform prop.
<LinkedValue uid="some-id" transform={(value) => value.toLocaleString() + "%"} />
<LinkedChart data={ ... } grow />
<LinkedChart data={ ... } barMinWidth="2" />
<LinkedChart data={ ... } barMinWidth="14" />
<LinkedChart data={ ... } barMinHeight="0" />
<LinkedChart data={ ... } barMinHeight="5" />
<LinkedChart
data={ ... }
barMinHeight="2"
hideBarBelow="1" />
<LinkedChart
data={ ... }
grow
barMinWidth="0" />
<LinkedChart
data={ ... }
width="250"
height="100" />
svg {
width: 100%;
height: auto;
}
svg {
width: 100%;
height: 50px;
}
<LinkedChart { data } gap="10" />
<LinkedChart { data } gap="0" />
<LinkedChart { data } align="left" />
<LinkedChart fill="#ff00ff" />
<LinkedChart fill="rgb(255, 255, 0)" />
<LinkedChart fill="hsla(290, 55%, 50%, 1)" />
<LinkedChart { data } fadeOpacity="0.15" />
<LinkedChart { data } hover={ false } />
<LinkedChart { data } transition="500" />
<LinkedChart { data } tabindex="0" />
<LinkedChart { data } type="line" />
<LinkedChart
{ data }
type="line"
lineColor="#4355db"
fill="var(--text-color)" />
<LinkedChart
dispatchEvents
on:hover={ event => console.log(event.detail) }
on:blur={ event => console.log(event.detail) }
on:value-update={ event => console.log(event.detail) } />
This could be used to construct your own value element that can be formatted as you wish. For example in this example the values are given as cents, but the value is formatted as dollars.
<LinkedChart
dispatchEvents
on:hover={ event =>
document.querySelector("[data-role='currency']")
.innerHTML = (event.detail.value / 100).toLocaleString("en-US", {
style: "currency", currency: "USD"
})
}
on:blur={ event =>
document.querySelector("[data-role='currency']").innerHTML = " "
} />
In this example we format the value element inside the chart directly to make use of "toLocaleString()" to format the number. Ideally you would supply the value already formatted to avoid having to do this, but that's not always possible.
<LinkedChart
dispatchEvents
showValue
valuePosition="floating"
valuePrepend="Value: "
on:value-update={ event => {
if (event.detail.valueElement)
event.detail.valueElement.innerText =
event.detail.value?.toLocaleString()
} } />
on:hover
uid, key, index, linkedKey, value, valueElement, eventElement
on:blur
uid, linkedKey, valueElement, eventElement
on:value-update
value, uid, linkedKey, valueElement
This is a list of all configurable properties on the "LinkedChart" component.
data
{}
labels
[]
values
[]
linked
uid
height
40
width
150
barMinWidth
4
barMinHeight
0
hideBarBelow
0
grow
false
align
right
gap
1
fill
#ff3e00
fadeOpacity
0.5
hover
true
transition
0
showValue
false
valueDefault
" "
valueUndefined
0
valuePrepend
valueAppend
valuePosition
static
scaleMax
0
scaleMax
0
type
bar
lineColor
fill
tabindex
-1
preserveAspectRatio
false
dispatchEvents
false
clickHandler
null
This is a list of all configurable properties on the "LinkedLabel" component.
linked
empty
transform
(label) => label
This is a list of all configurable properties on the "LinkedValue" component.
uid
empty
valueUndefined
0
transform
(value) => value