Data classes and popup
author(s): Torstein Hønsi
by shruti patil
HTML
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/maps/modules/map.js"></script>
<script src="https://code.highcharts.com/maps/modules/data.js"></script>
<script src="https://code.highcharts.com/maps/modules/exporting.js"></script>
<script src="https://code.highcharts.com/maps/modules/offline-exporting.js"></script>
<script src="https://code.highcharts.com/maps/modules/accessibility.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<link href="https://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet" type="text/css" />
<div id="container">
<div class="loading">
<i class="icon-spinner icon-spin icon-large"></i>
Loading data from Google Spreadsheets...
</div>
</div>
CSS
#container {
height: 500px;
min-width: 310px;
max-width: 600px;
margin: 0 auto;
}
.loading {
margin-top: 10em;
text-align: center;
color: gray;
}
JavaScript
(async () => {
const mapData = await fetch(
'https://code.highcharts.com/mapdata/countries/us/us-all.topo.json'
).then(response => response.json());
// Load the data from a Google Spreadsheet
// https://docs.google.com/spreadsheets/d/1uj1Gzv3fpH-b0w2tYpuKNp3TrGr43I9XAAqmgVE_jMs
Highcharts.data({
googleAPIKey: 'AIzaSyCQ0Jh8OFRShXam8adBbBcctlbeeA-qJOk',
googleSpreadsheetKey: '1uj1Gzv3fpH-b0w2tYpuKNp3TrGr43I9XAAqmgVE_jMs',
// Custom handler for columns
parsed: function (columns) {
/**
* Event handler for clicking points. Use jQuery UI to pop up
* a pie chart showing the details for each state.
*/
function pointClick() {
const row = this.options.row,
$div = $('<div></div>')
.dialog({
title: this.name,
width: 320,
height: 300
});
}
// Make the columns easier to read
let keys = columns[0];
const names = columns[1],
percent = columns[7],
// Build the chart options
options = {
chart: {
type: 'map',
map: mapData,
renderTo: 'container',
borderWidth: 1
},
title: {
text: 'US presidential election 2016 results',
align: 'left'
},
subtitle: {
text: 'Source: <a href="https://transition.fec.gov/pubrec/fe2016/2016presgeresults.pdf">Federal Election Commission</a>',
align: 'left'
},
legend: {
align: 'right',
...