
Peity Vanilla is a JavaScript charting library that turns numeric text in HTML into compact SVG pie, donut, line, and bar charts.
It is based on the Peity jQuery plugin and uses a plain JavaScript API.
Features
- Ratio and comma-separated data formats for pie and donut charts.
- Positive and negative values in line and bar charts.
- Custom line strokes, bar spacing, and chart dimensions.
- Per-chart JSON configuration through
data-peity. - Color arrays and callbacks for chart segments and bars.
- Configurable global defaults for each chart type.
How To Use It
Installation
Download peity-vanilla.min.js and include it in your web page.
<script src="peity-vanilla.min.js"></script>
You can also import the ES module with NPM as follows:
npm install peity-vanilla
import peity from "peity-vanilla";
Basic Usage
Place numeric values inside HTML elements and initialize each chart:
<span id="completion">3/5</span>
<span id="categories">9,4,2</span>
<span id="visits">4,6,3,7,5,8,9</span>
<span id="orders">2,7,4,9,5,8,6</span>
<script src="peity-vanilla.min.js"></script>
<script>
peity(document.getElementById("completion"), "pie");
peity(document.getElementById("categories"), "donut");
peity(document.getElementById("visits"), "line", {
width: 120,
height: 32,
stroke: "#0f766e",
fill: "#d1fae5"
});
peity(document.getElementById("orders"), "bar", {
width: 120,
height: 32,
fill: ["#0f766e"]
});
</script>Data Formats
For pie and donut charts, 3/5 represents three parts out of five. Comma-separated input such as 3,5,2 creates three slices whose values add up to ten. Line and bar charts read comma-separated numbers, including negative values.
Color Bars By Value
The fill option accepts color arrays or a callback for pie, donut, and bar charts. Callbacks receive (value, index, values); arrays cycle through their colors. This example colors negative bars red:
<span id="weekly-change">4,-2,7,-1,5</span>
<script>
peity(document.getElementById("weekly-change"), "bar", {
width: 120,
height: 28,
fill: (value) => value < 0 ? "#dc2626" : "#15803d"
});
</script>Set Options In HTML
Use a data-peity attribute for per-chart JSON settings.
<span id="progress" data-peity='{"radius":26,"innerRadius":12,"fill":["#0f766e","#e5e7eb"]}'>7/10</span>
<script>
peity(document.getElementById("progress"), "donut");
</script>Initialization Parameters
peity(element, type, options) initializes a chart and returns its instance.
| Parameter | Description |
|---|---|
element | The HTML element whose text contains the numeric data. |
type | One of "pie", "donut", "line", or "bar". |
options | Optional object that overrides the chart defaults and values in data-peity. |
Pie And Donut Options
| Option | Description |
|---|---|
delimiter | Custom input separator. By default, the chart detects a separator from the element text; / uses the numerator and total. |
fill | Array of slice colors or a color callback. Default: ["#ff9900", "#fff4dd", "#ffc66e"]. |
radius | Fallback radius in pixels. Default: 8. |
width | SVG width. Default: diameter from radius. |
height | SVG height. Default: diameter from radius. |
innerRadius | Donut hole radius. Default: half the calculated radius; donut charts only. |
Line Chart Options
| Option | Description |
|---|---|
delimiter | Numeric separator. Default: ",". |
fill | Area fill color. Default: "#c6d9fd"; a falsy value disables the filled area. |
height | SVG height in pixels. Default: 16. |
max | Upper chart bound. Default: derived from the data. |
min | Lower chart bound. Default: 0, extended for negative data. |
stroke | Line color. Default: "#4d89f9". |
strokeWidth | Line thickness in pixels. Default: 1; 0 hides the stroke. |
width | SVG width in pixels. Default: 32. |
Bar Chart Options
| Option | Description |
|---|---|
delimiter | Numeric separator. Default: ",". |
fill | Array of bar colors or a color callback. Default: ["#4D89F9"]. |
height | SVG height in pixels. Default: 16. |
max | Upper chart bound. Default: derived from the data. |
min | Lower chart bound. Default: 0, extended for negative data. |
padding | Fraction of each bar slot reserved on either side. Default: 0.1. |
width | SVG width in pixels. Default: 32. |
To change a chart type’s defaults for subsequent instances, update its entry in peity.defaults before initialization:
peity.defaults.line.stroke = "#0f766e";
Limitations
Peity Vanilla listens for the obsolete DOMSubtreeModified event to redraw charts when their text changes. Chrome 127 removed that event, so changing the source text no longer updates the chart in current Chrome. For frequently changing data, use a chart library with a supported update API.
Alternatives And Related Resources
- Pure JavaScript Library for Inline Charts – Sparklines.js
- Lightweight Responsive SVG Charts with CSS Transitions – Beaned-Charts
- Responsive SVG Charts with Vanilla JavaScript – Chartist







