Skip to content

Instantly share code, notes, and snippets.

@jlittlejohn
Created August 22, 2013 19:26
Show Gist options
  • Save jlittlejohn/6311663 to your computer and use it in GitHub Desktop.
Save jlittlejohn/6311663 to your computer and use it in GitHub Desktop.
JS: Pie Chart Using Canvas Element
<!-- Add to HTML -->
<canvas id="myChart" width="400" height="400"></canvas>
<script>
// JSON Data Object
var data = [
{label: 'Example Item 1', value: 3.0, color: '#21280b'},
{label: 'Example Item 2', value: 0.18, color: '#374213'},
{label: 'Example Item 3', value: 1.96, color: '#4d5d1b'},
{label: 'Example Item 4', value: 0.01, color: '#627722'}
];
//Get the context of the canvas element we want to select
var ctx = document.getElementById("myChart").getContext("2d");
var myNewChart = new Chart(ctx).Pie(data);
var legendHtml = "";
$(data).each(function() {
legendHtml += '<li><span style="width: 1em; height: 1em; display: inline-block; background-color: '+this.color+'"></span> '+this.label+'</li>';
});
$('.legend').append(legendHtml);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment