Created
April 22, 2014 21:33
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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+'"> </span>'); | |
options.type = 'pie'; | |
options.tooltipFormat = '<span style="color: {{color}}">●</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