Map With Patterns
This demo shows off two concepts:
- Property fields: a way to bind properties of specific objects, like map polygons, to data.
- Using built-in pattern builder and set.
Property fields docs Pattern set docs
Demo source
<!-- Styles -->
<style>
#chartdiv {
width: 100%;
height: 500px
}
</style>
<!-- Resources -->
<script src="https://cdn.amcharts.com/lib/4/core.js"></script>
<script src="https://cdn.amcharts.com/lib/4/maps.js"></script>
<script src="https://cdn.amcharts.com/lib/4/geodata/continentsLow.js"></script>
<script src="https://cdn.amcharts.com/lib/4/themes/animated.js"></script>
<!-- Chart code -->
<script>
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_continentsLow;
// Set projection
chart.projection = new am4maps.projections.Miller();
// Create map polygon series
var polygonSeries = chart.series.push(new am4maps.MapPolygonSeries());
// Exclude Antartica
polygonSeries.exclude = ["antarctica"];
// Make map load polygon (like country names) data from GeoJSON
polygonSeries.useGeodata = true;
// Get a new PatternSet
var patterns = new am4core.PatternSet();
polygonSeries.data = [{
"id": "europe",
"name": "Europe",
"fill": patterns.next()
}, {
"id": "northAmerica",
"name": "North America",
"fill": patterns.next()
}, {
"id": "southAmerica",
"name": "South America",
"fill": patterns.next()
}, {
"id": "africa",
"name": "Africa",
"fill": patterns.next()
}, {
"id": "asia",
"name": "Asia",
"fill": patterns.next()
}, {
"id": "oceania",
"name": "Australia and Oceania",
"fill": patterns.next()
}];
// Configure series
var polygonTemplate = polygonSeries.mapPolygons.template;
polygonTemplate.tooltipText = "{name}";
polygonTemplate.propertyFields.fill = "fill";
// Create hover state and set alternative fill color
var hs = polygonTemplate.states.create("hover");
hs.properties.fill = am4core.color("#367B25");
// Create hover state and set alternative fill color
var hs = polygonTemplate.states.create("hover");
hs.properties.fill = am4core.color("#367B25");
}); // end am4core.ready()
</script>
<!-- HTML -->
<div id="chartdiv"></div>