Skip to content

Instantly share code, notes, and snippets.

@Guitlle
Created April 22, 2014 21:33
Show Gist options
  • Save Guitlle/11195176 to your computer and use it in GitHub Desktop.
Save Guitlle/11195176 to your computer and use it in GitHub Desktop.
Sparkline pie chart from object. the tooltips are the object keys and the values are the object value for each key.
function pieChartFromObject(data, options, className) {
var labels = [], total = 0,
values = $.map(data, function(value, label) { total += value; labels.push(label); return value; });
var chart = $('<span class="'+className+'">&nbsp;</span>');
options.type = 'pie';
options.tooltipFormat = '<span style="color: {{color}}">&#9679;</span> {{offset:labels}} - {{value}} ({{percent.1}}%)';
options.tooltipValueLookups = { labels: labels} ;
if (total == 0) {
// show an empty pie if the data is invalid
labels = [""];
values = [1];
options.sliceColors = ["#aaa"];
options.tooltipFormat = 'No data';
}
chart.sparkline(values, options );
return chart;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment