Peity Vanilla: Inline SVG Charts with Plain JavaScript

Category: Chart & Graph , Javascript | November 23, 2026
Authorrailsjazz
Last UpdateNovember 23, 2026
LicenseMIT
Tags
Views0 views
Peity Vanilla: Inline SVG Charts with Plain JavaScript

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.

ParameterDescription
elementThe HTML element whose text contains the numeric data.
typeOne of "pie", "donut", "line", or "bar".
optionsOptional object that overrides the chart defaults and values in data-peity.

Pie And Donut Options

OptionDescription
delimiterCustom input separator. By default, the chart detects a separator from the element text; / uses the numerator and total.
fillArray of slice colors or a color callback. Default: ["#ff9900", "#fff4dd", "#ffc66e"].
radiusFallback radius in pixels. Default: 8.
widthSVG width. Default: diameter from radius.
heightSVG height. Default: diameter from radius.
innerRadiusDonut hole radius. Default: half the calculated radius; donut charts only.

Line Chart Options

OptionDescription
delimiterNumeric separator. Default: ",".
fillArea fill color. Default: "#c6d9fd"; a falsy value disables the filled area.
heightSVG height in pixels. Default: 16.
maxUpper chart bound. Default: derived from the data.
minLower chart bound. Default: 0, extended for negative data.
strokeLine color. Default: "#4d89f9".
strokeWidthLine thickness in pixels. Default: 1; 0 hides the stroke.
widthSVG width in pixels. Default: 32.

Bar Chart Options

OptionDescription
delimiterNumeric separator. Default: ",".
fillArray of bar colors or a color callback. Default: ["#4D89F9"].
heightSVG height in pixels. Default: 16.
maxUpper chart bound. Default: derived from the data.
minLower chart bound. Default: 0, extended for negative data.
paddingFraction of each bar slot reserved on either side. Default: 0.1.
widthSVG 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

You Might Be Interested In:


Leave a Reply