Created
January 15, 2020 18:11
-
-
Save jccartwright/d04b589e441884578bb6b4f134582984 to your computer and use it in GitHub Desktop.
Pie Chart using Google Charts API
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
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