Generate a Sparkline as an SVG.
npm install sparkline-svg
oryarn add sparkline-svg
The sparkline-svg
package exports a Sparkline class, which can be constructed with or without an array of values used to generate the sparkline.
import Sparkline from 'sparkline-svg';
const sparkline= new Sparkline();
// or
const sparkline = new Sparkline(values);
sparkline.d
returns the <path />
's d
attribute for the sparkline (stroke) itself.
sparkline.dataUri
returns the sparkline SVG as a data URI, i.e. data:image/svg+xml;base64,...
. This is particularly useful for CSS background images.
sparkline.outerHTML
returns a string of the HTML for an SVG containing the sparkline.
Sets the number of decimal places used to generate the sparkline. A larger number of decimal places will result in better precision, but a larger file size.
Default: 4
Sets the description of the sparkline. Used to populate the <desc>
element.
Default: 'A line graph representation of a value's change over time.'
Synonymous with setDesc
.
Sets the color of the area underneath the sparkline.
Default: 'transparent'
Sets the height of the sparkline's SVG element. Not to be confused with setViewBoxHeight
.
Default: '100%'
Sets the preserveAspectRatio
attribute of the SVG element.
Default: 'none'
Sets the color of the sparkline itself.
Default: 'currentColor'
Sets the width of the sparkline itself. If using a number, this will be relative to the view box height and width.
Default: '1%'
Sets the title of the sparkline SVG by populating the <title>
element. This is useful for accessibility purposes and often appears as a tooltip, similar to the title
attribute on an anchor tag.
Default: 'Sparkline'
Sets the values used to generate the sparkline. These can also be provided in the constructor.
Default: []
Sets the height of the sparkline's view box. Not to be confused with setHeight
. The sparkline will always stretch to fit the view box.
Default: 100
Sets the width of the sparkline's view box. Not to be confused with setWidth
. The sparkline will always stretch to fit the view box.
Default: 100
Sets the width of the sparkline's SVG element. Not to be confused with setViewBoxWidth
.
Default: '100%'
If you are a fan of this project, you may become a sponsor via GitHub's Sponsors Program.