Skip to content

Instantly share code, notes, and snippets.

@jccartwright
Created January 15, 2020 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jccartwright/d04b589e441884578bb6b4f134582984 to your computer and use it in GitHub Desktop.
Save jccartwright/d04b589e441884578bb6b4f134582984 to your computer and use it in GitHub Desktop.
Pie Chart using Google Charts API
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
// mock of data retrieved via the API
var rawDeviceData = [
{"name": "iPhone7,1", "valid": 303662, "total": 1090913},
{"name": "SM-G920V", "valid": 287159, "total": 1499170},
{"name": "LG-D850", "valid": 221267, "total": 1077458},
{"name": "iPhone8,2", "valid": 186012, "total": 649844}
];
// reformat to accommodate Google API
deviceData = rawDeviceData.map(x => {
return [x.name, x.total]
});
// add column header
deviceData.unshift(['Device', 'Count']);
function drawChart() {
var data = google.visualization.arrayToDataTable(
deviceData
);
var options = {
title: 'Most Popular Devices'
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment