I'd like to display a worldmap (GeoChart) using Google's Chart API where some countries are listed by country and others are displayed based on a grouping/region.
My data might look like this:
[["Country","DataValue"],["United States",2000],["West Africa",3000],["Great Britain",1500]];
Is this possible to do? I tried playing with the displayMode
property of the options but that didn't do any good. I also tried using "011" instead of "West Africa" since "011" is recognized as a region by the API. That did not work either.
I was able to display just the region of West Africa with the region
option but that isn't what I'm after. I really want to display the world but have regions like West Africa handled and colored as a group rather than individual countries.
Thanks for your help.
Google Geochart supports either the whole world or specific region (eg 011 Western Africa
)
Since your goal:
to display the world but have regions like West Africa handled and colored as a group rather than individual countries
i would propose the following solution: to dynamically expand data table row for a region into the rows per every country from that region:
function expandRegions(dataTable) {
var regions = {
'West Africa': {
'204': 'Benin',
'854': 'Burkina Faso',
'132': 'Cabo Verde',
'384': "Cote d'Ivoire",
'270': 'Gambia',
'288': 'Ghana',
'324': 'Guinea',
'624': 'Guinea-Bissau',
'430': 'Liberia',
'466': 'Mali',
'478': 'Mauritania',
'562': 'Niger',
'566': 'Nigeria',
'654': 'Saint Helena',
'686': 'Senegal',
'694': 'Sierra Leone',
'768': 'Togo'
}
};
for (var i = 0; i < dataTable.getNumberOfRows(); i++) {
var regionName = dataTable.getValue(i, 0);
var region = regions[regionName];
if (region) {
var value = dataTable.getValue(i, 1);
dataTable.removeRow(i); //remove region row
//add countries from region rows
for (var code in region) {
var countryName = region[code];
dataTable.addRow([countryName, value]);
}
}
}
return dataTable;
}
Complete example
google.load("visualization", "1", { packages: ["geochart"] }); google.setOnLoadCallback(drawRegionsMap); function drawRegionsMap() { var data = google.visualization.arrayToDataTable([ ["Country","DataValue"], ["United States",2000], ["West Africa",3000], ["Great Britain",1500] ]); data = expandRegions(data); var options = { }; var chart = new google.visualization.GeoChart(document.getElementById('regions_div')); chart.draw(data, options); } function expandRegions(dataTable) { var regions = { 'West Africa': { '204': 'Benin', '854': 'Burkina Faso', '132': 'Cabo Verde', '384': "Cote d'Ivoire", '270': 'Gambia', '288': 'Ghana', '324': 'Guinea', '624': 'Guinea-Bissau', '430': 'Liberia', '466': 'Mali', '478': 'Mauritania', '562': 'Niger', '566': 'Nigeria', '654': 'Saint Helena', '686': 'Senegal', '694': 'Sierra Leone', '768': 'Togo' } }; for (var i = 0; i < dataTable.getNumberOfRows(); i++) { var regionName = dataTable.getValue(i, 0); var region = regions[regionName]; if (region) { var value = dataTable.getValue(i, 1); dataTable.removeRow(i); for (var code in region) { var countryName = region[code]; dataTable.addRow([countryName, value]); } } } return dataTable; }
<script type="text/javascript" src="https://www.google.com/jsapi"></script> <div id="regions_div" style="width: 900px; height: 500px;"></div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.