Peity

Peity (sounds like deity) is a simple jQuery plugin that converts an element's content into a simple mini pie 2/5 line 5,3,9,6,5,9,7,3,5,2 or bar chart 5,3,9,6,5,9,7,3,5,2 and is compatible with any browser that supports : Chrome, Firefox, IE9+, Opera, Safari.

Download version 1.2.0

Uncompressed 6.4Kb
jquery.peity.js
Minified 3.0Kb (+gzipped 1.4Kb)
jquery.peity.min.js
Source

Pie Charts

Simply call peity("pie") on a jQuery selection. There are two subtly different pie chart semantics, a "/" delimiter is assumed to mean "three out of five" and only the first two values will be drawn, otherwise all of the values are included in the chart and the total is the sum of all values.

You can also pass colours, delimiter, diameter, height and width options. Setting a diameter is a shortcut for setting the width and height to the same value.

1/5 226/360 0.52/1.561 1,4 226,134 0.52,1.041 1,2,3,2,2

HTML

1/5
226/360
0.52/1.561
1,4
226,134
0.52,1.041
1,2,3,2,2

JavaScript

$("span.pie").peity("pie")

Line Charts

Line charts work on a comma-separated list of digits. Line charts can take the following options: colour, strokeColour, strokeWidth, delimiter, max, min, width and height.

5,3,9,6,5,9,7,3,5,2 5,3,2,-1,-3,-2,2,3,5,2 0,-3,-6,-4,-5,-4,-7,-3,-5,-2

HTML

5,3,9,6,5,9,7,3,5,2
5,3,2,-1,-3,-2,2,3,5,2
0,-3,-6,-4,-5,-4,-7,-3,-5,-2

JavaScript

$(".line").peity("line")

Bar Charts

Bar charts work in the same way as line charts and take the following options: colours, delimiter, max, min, spacing, width and height.

5,3,9,6,5,9,7,3,5,2 5,3,2,-1,-3,-2,2,3,5,2 0,-3,-6,-4,-5,-4,-7,-3,-5,-2

HTML

5,3,9,6,5,9,7,3,5,2
5,3,2,-1,-3,-2,2,3,5,2
0,-3,-6,-4,-5,-4,-7,-3,-5,-2

JavaScript

$(".bar").peity("bar")

Setting Colours Dynamically

Bar and pie chart colours can be defined dynamically based on the values of the chart. When passing an array its values are cycled, when passing a function it is called once for each value allowing you to define each bar's colour. The function is called with a similar signature to Array#forEach with the bar's value, its index, and the full array of values.

5,3,9,6,5,9,7,3,5,2 5,3,2,-1,-3,-2,2,3,5,2 0,-3,-6,-4,-5,-4,-7,-3,-5,-2 4,7,6,5 5,3,9,6,5

HTML

5,3,9,6,5,9,7,3,5,2
5,3,2,-1,-3,-2,2,3,5,2
0,-3,-6,-4,-5,-4,-7,-3,-5,-2
4,7,6,5
5,3,9,6,5

JavaScript

$(".bar-colours-1").peity("bar", {
  colours: ["red", "green", "blue"]
})

$(".bar-colours-2").peity("bar", {
  colours: function(value) {
    return value > 0 ? "green" : "red"
  }
})

$(".bar-colours-3").peity("bar", {
  colours: function(_, i, all) {
    var g = parseInt((i / all.length) * 255)
    return "rgb(255, " + g + ", 0)"
  }
})

$(".pie-colours-1").peity("pie", {
  colours: ["cyan", "magenta", "yellow", "black"]
})

$(".pie-colours-2").peity("pie", {
  colours: function(_, i, all) {
    var g = parseInt((i / all.length) * 255)
    return "rgb(255, " + g + ", 0)"
  }
})

Updating Charts

Charts can be updated by changing the the jQuery selection's text content and calling change() on it. The chart will be redrawn with the same options that were originally passed to it.

5,3,9,6,5,9,7,3,5,2,5,3,9,6,5,9,7,3,5,2

HTML

5,3,9,6,5,9,7,3,5,2,5,3,9,6,5,9,7,3,5,2

JavaScript

var updatingChart = $(".updating-chart").peity("line", { width: 64 })

setInterval(function() {
  var random = Math.round(Math.random() * 10)
  var values = updatingChart.text().split(",")
  values.shift()
  values.push(random)

  updatingChart
    .text(values.join(","))
    .change()
}, 1000)

Custom Charts

You can easily add your own custom chart by registering it with Peity with name, defaults and the draw function which receives an options object.

$.fn.peity.add("custom", {
    colour: "#FFCC00"
  }, function(opts) {
    ...
  }
)

data-* attributes

If you use data attributes, Peity will apply their values to the defaults with the same names automatically. Options passed to the peity() function take precedence over data-* attributes.

1/7 2/7 3/7 4/7 5/7 6/7 7/7

HTML

1/7
2/7
3/7
4/7
5/7
6/7
7/7

JavaScript

$(".pie span").peity("pie")

Events

Peity adds a "change" event trigger to your graph elements, so if you update their data your can regenerate one or more charts by triggering change() on them.

Nothing's happened yet.

HTML

Nothing's happened yet.

JavaScript

$("select").change(function() {
  $(this)
    .siblings("span.graph")
    .text($(this).val() + "/" + 5).change()
}).change()

var chartUpdate = function(event, value, max) {
  $("#notice").text(
    "Chart updated: " + value + "/" + max
  )
}

$("span.graph")
  .peity("pie")
  .bind("change.peity", chartUpdate)

Default Settings

Defaults can be overridden globally like so:

$.fn.peity.defaults.pie = {
  colours: ["#ff9900", "#fff4dd", "#ffd592"],
  delimiter: null,
  diameter: 16,
  height: null,
  width: null
}

$.fn.peity.defaults.line = {
  colour: "#c6d9fd",
  strokeColour: "#4d89f9",
  strokeWidth: 1,
  delimiter: ",",
  height: 16,
  max: null,
  min: 0,
  width: 32
}

$.fn.peity.defaults.bar = {
  colours: ["#4d89f9"],
  delimiter: ",",
  height: 16,
  max: null,
  min: 0,
  spacing: devicePixelRatio || 1,
  width: 32
}