Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mitjafelicijan/sparklines

Simple SVG Sparklines
https://github.com/mitjafelicijan/sparklines

chart js sparkline svg

Last synced: about 1 month ago
JSON representation

Simple SVG Sparklines

Awesome Lists containing this project

README

        

# Simple SVG Sparklines

![Preview](https://github.com/mitjafelicijan/sparklines/assets/296714/48b46813-3cd7-4eed-971a-24f92f957922)

Self-contained tiny SVG sparkline chart library written in vanilla JS with zero dependencies.

Add `sparklines.js` to your HTML file and use proper data attributes and that is about it.

Check kitchensink demo on https://mitjafelicijan.github.io/sparklines/.

## Basic usage

```html

```

Check `kitchensink.html` for more examples.

> To create minified version of `sparklines.js` use `npx minify sparklines.js > sparklines.min.js`.

You can also use CDN for this library so that you don't need to host it
yourself:

- https://cdn.jsdelivr.net/gh/mitjafelicijan/sparklines/sparklines.js
- https://cdn.jsdelivr.net/gh/mitjafelicijan/sparklines/sparklines.min.js

## Bar charts

| Attribute | Description | Default Value |
|-----------|-------------|---------------|
| data-sparkline | If sparkline is enabled on this element | None |
| data-points | List of numbers / data | [] |
| data-width | Width of the container in px | 100 |
| data-height | Height of the container in px | 30 |
| data-gap | Gap between the bars in px | 5 |
| data-colors | List of colors of the bars in HEX or color name | gray |

```html







```

## Line charts

| Attribute | Description | Default Value |
|-----------|-------------|---------------|
| data-sparkline | If sparkline is enabled on this element | None |
| data-points | List of numbers / data | [] |
| data-width | Width of the container in px | 100 |
| data-height | Height of the container in px | 30 |
| data-stroke-width | Size of the line in px | 2 |
| data-colors | Color of the line in HEX or color name | gray |

```html




```

## Pie charts

| Attribute | Description | Default Value |
|-----------|-------------|---------------|
| data-sparkline | If sparkline is enabled on this element | None |
| data-points | List of numbers / data | [] |
| data-width | Width of the container in px | 100 |
| data-height | Height of the container in px | 30 |
| data-colors | List of colors of the slices in HEX or color name | gray |

```html




```
## Stacked charts

| Attribute | Description | Default Value |
|-----------|-------------|---------------|
| data-sparkline | If sparkline is enabled on this element | None |
| data-points | List of numbers / data | [] |
| data-width | Width of the container in px | 100 |
| data-height | Height of the container in px | 30 |
| data-gap | Gap between the bars in px | 5 |
| data-colors | List of colors of the bars in HEX or color name | gray |

```html




```

## Programmatic update

Each initialized sparkline chart is also listening to a custom event called
`update` that triggers re-rendering of the chart.

Let's assume we have a basic chart like this.

```html


```

Then we can update `data-points` attribute and emit update event.

```js
const chart = document.getElementById('chart1');
chart.dataset.points = '6,44,21,95,11';
chart.dispatchEvent(new Event('update'));
```

You can also change colors or any `data-*` attribute. There are no restrictions.

## Notes

- `transparent` is a valid option for `data-colors` attribute.
- If not enough colors are provided the library will cycle through available
ones until all datapoints are used.

## Additional reading material

- https://www.w3.org/2002/Talks/www2002-svgtut-ih/hwtut.pdf
- https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Introduction
- https://www.w3schools.com/graphics/svg_intro.asp
- https://tutorial.math.lamar.edu/pdf/Trig_Cheat_Sheet_Reduced.pdf

## License

[sparklines](https://github.com/mitjafelicijan/sparklines) was written by [Mitja
Felicijan](https://mitjafelicijan.com) and is released under the BSD two-clause
license, see the LICENSE file for more information.