JSFiddle - React, Tailwind, and code Playground
by cmoreira
HTML
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/maps.js"></script>
<script src="https://www.amcharts.com/lib/4/geodata/worldLow.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
<!-- HTML -->
<div id="chartdiv"></div>
CSS
#chartdiv {
width: 100%;
height: auto;
}
JavaScript
am4core.ready(function() {
// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end
/* Create map instance */
var chart = am4core.create("chartdiv", am4maps.MapChart);
/* Set map definition */
chart.geodata = am4geodata_worldLow;
/* Set projection */
chart.projection = new am4maps.projections.Mercator();
/* Create map polygon series */
var polygonSeries = chart.series.push(new am4maps.MapPolygonSeries());
/* Make map load polygon (like country names) data from GeoJSON */
polygonSeries.useGeodata = true;
/* Configure series */
var polygonTemplate = polygonSeries.mapPolygons.template;
polygonTemplate.applyOnClones = true;
polygonTemplate.togglable = true;
polygonTemplate.tooltipText = "{name}";
polygonTemplate.nonScalingStroke = true;
polygonTemplate.strokeOpacity = 0.5;
polygonTemplate.fill = chart.colors.getIndex(0);
var lastSelected;
polygonTemplate.events.on("hit", function(ev) {
if (lastSelected) {
// This line serves multiple purposes:
// 1. Clicking a country twice actually de-activates, the line below
// de-activates it in advance, so the toggle then re-activates, making it
// appear as if it was never de-activated to begin with.
// 2. Previously activated countries should be de-activated.
lastSelected.isActive = false;
}
ev.target.series.chart.zoomToMapObject(ev.target);
if (lastSelected !== ev.target) {
lastSelected = ev.target;
}
})
/* Create selected and hover states and set alternative fill color */
var ss = polygonTemplate.states.create("active");
ss.properties.fill = chart.colors.getIndex(2);
var hs = polygonTemplate.states.create("hover");
hs.properties.fill = chart.colors.getIndex(4);
// Hide Antarctica
polygonSeries.exclude = ["AQ","US"];
// Small map
chart.smallMap = new am4maps.SmallMap();
// Re-position to top right (it defaults to bottom left)
chart.smallMap.align = "right";
chart.smallMap.valign = "top";
chart.smallMap.series.push(polygonSeries);
// Zoom...