Map with custom markers and data-bound colors
This demo shows how we can use Graphics elements with SVG paths as map markers on a MapPointSeries, custom-colored using “template fields”.
Related code
Section titled “Related code”var bulletTemplate = am5.Template.new({ fill: am5.color(0xe6e6e6)});
pointSeries.bullets.push(function() { var pin = am5.Graphics.new(root, { width: 24, height: 24, tooltipY: 0, centerX: am5.p50, centerY: am5.p100, svgPath: "M12,1a9,9,0,0,0-9,9c0,8,9,13,9,13s9-5,9-13A9,9,0,0,0,12,1Zm0,13a4,4,0,1,1,4-4A4,4,0,0,1,12,14Z", stroke: am5.color(0xffffff), strokeOpacity: 0.5, templateField: "bulletSettings", tooltipText: "{title}", cursorOverStyle: "pointer" }, bulletTemplate);
return am5.Bullet.new(root, { sprite: pin });});var bulletTemplate = am5.Template.new({ fill: am5.color(0xe6e6e6)});
pointSeries.bullets.push(function() { var pin = am5.Graphics.new(root, { width: 24, height: 24, tooltipY: 0, centerX: am5.p50, centerY: am5.p100, svgPath: "M12,1a9,9,0,0,0-9,9c0,8,9,13,9,13s9-5,9-13A9,9,0,0,0,12,1Zm0,13a4,4,0,1,1,4-4A4,4,0,0,1,12,14Z", stroke: am5.color(0xffffff), strokeOpacity: 0.5, templateField: "bulletSettings", tooltipText: "{title}", cursorOverStyle: "pointer" }, bulletTemplate);
return am5.Bullet.new(root, { sprite: pin });});/** * --------------------------------------- * This demo was created using amCharts 5. * * For more information visit: * https://www.amcharts.com/ * * Documentation is available at: * https://www.amcharts.com/docs/v5/ * --------------------------------------- */
// Create root element// https://www.amcharts.com/docs/v5/getting-started/#Root_elementvar root = am5.Root.new("chartdiv");
// Set themes// https://www.amcharts.com/docs/v5/concepts/themes/root.setThemes([am5themes_Animated.new(root)]);
// Create the map chart// https://www.amcharts.com/docs/v5/charts/map-chart/var chart = root.container.children.push( am5map.MapChart.new(root, { panX: "rotateX", panY: "translateY", projection: am5map.geoMercator() }));
// Create main polygon series for countries// https://www.amcharts.com/docs/v5/charts/map-chart/map-polygon-series/var polygonSeries = chart.series.push( am5map.MapPolygonSeries.new(root, { geoJSON: am5geodata_worldLow, exclude: ["AQ"] }));
// Create line series for trajectory lines// https://www.amcharts.com/docs/v5/charts/map-chart/map-line-series/var lineSeries = chart.series.push(am5map.MapLineSeries.new(root, {}));lineSeries.mapLines.template.setAll({ stroke: root.interfaceColors.get("alternativeBackground"), strokeOpacity: 0.3});
// Create point series for markers// https://www.amcharts.com/docs/v5/charts/map-chart/map-point-series/var pointSeries = chart.series.push(am5map.MapPointSeries.new(root, {}));var colorset = am5.ColorSet.new(root, {});
var bulletTemplate = am5.Template.new({ fill: am5.color(0xe6e6e6)});
pointSeries.bullets.push(function () { var pin = am5.Graphics.new( root, { width: 24, height: 24, tooltipY: 0, centerX: am5.p50, centerY: am5.p100, svgPath: "M12,1a9,9,0,0,0-9,9c0,8,9,13,9,13s9-5,9-13A9,9,0,0,0,12,1Zm0,13a4,4,0,1,1,4-4A4,4,0,0,1,12,14Z", stroke: am5.color(0xffffff), strokeOpacity: 0.5, templateField: "bulletSettings", tooltipText: "{title}", cursorOverStyle: "pointer" }, bulletTemplate );
return am5.Bullet.new(root, { sprite: pin });});
var cities = [ { title: "Brussels", latitude: 50.8371, longitude: 4.3676, color: am5.color(0x895b1e) },
{ title: "Copenhagen", latitude: 55.6763, longitude: 12.5681, color: am5.color(0x895b1e) },
{ title: "Paris", latitude: 48.8567, longitude: 2.351, color: am5.color(0x994636) },
{ title: "Reykjavik", latitude: 64.1353, longitude: -21.8952, color: am5.color(0x994636) },
{ title: "Moscow", latitude: 55.7558, longitude: 37.6176, color: am5.color(0x17183b) },
{ title: "Madrid", latitude: 40.4167, longitude: -3.7033, color: am5.color(0x994636) },
{ title: "London", latitude: 51.5002, longitude: -0.1262, color: am5.color(0x895b1e) },
{ title: "Peking", latitude: 39.9056, longitude: 116.3958, color: am5.color(0x895b1e) },
{ title: "New Delhi", latitude: 28.6353, longitude: 77.225, color: am5.color(0x994636) },
{ title: "Tokyo", latitude: 35.6785, longitude: 139.6823, color: am5.color(0x17183b) },
{ title: "Ankara", latitude: 39.9439, longitude: 32.856, color: am5.color(0x895b1e) },
{ title: "Buenos Aires", latitude: -34.6118, longitude: -58.4173, color: am5.color(0x994636) },
{ title: "Brasilia", latitude: -15.7801, longitude: -47.9292, color: am5.color(0x895b1e) },
{ title: "Ottawa", latitude: 45.4235, longitude: -75.6979, color: am5.color(0x17183b) },
{ title: "Washington", latitude: 38.8921, longitude: -77.0241, color: am5.color(0x994636) },
{ title: "Kinshasa", latitude: -4.3369, longitude: 15.3271, color: am5.color(0x994636) },
{ title: "Cairo", latitude: 30.0571, longitude: 31.2272, color: am5.color(0x895b1e) },
{ title: "Pretoria", latitude: -25.7463, longitude: 28.1876, color: am5.color(0x17183b) }];
for (var i = 0; i < cities.length; i++) { var city = cities[i]; addCity(city.longitude, city.latitude, city.title, city.color);}
function addCity(longitude, latitude, title, color) { pointSeries.data.push({ geometry: { type: "Point", coordinates: [longitude, latitude] },
title: title, bulletSettings: { fill: color } });}
// Make stuff animate on loadchart.appear(1000, 100);<div id="chartdiv"></div>body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";}
#chartdiv { width: 100%; height: 95vh;}{ "type": "MapChart", "settings": { "panX": "rotateX", "panY": "translateY", "projectionName": "geoMercator" }, "properties": { "series": [ "#series-0", "#series-1", "#series-2" ] }, "refs": [ { "data-0": [] }, { "data-1": [] }, { "data-2": [ { "geometry": { "type": "Point", "coordinates": [ 4.3676, 50.8371 ], "__parse": true }, "title": "Brussels", "bulletSettings": { "fill": { "type": "Color", "value": "#895b1e" }, "__parse": true }, "__parse": true }, { "geometry": { "type": "Point", "coordinates": [ 12.5681, 55.6763 ], "__parse": true }, "title": "Copenhagen", "bulletSettings": { "fill": { "type": "Color", "value": "#895b1e" }, "__parse": true }, "__parse": true }, { "geometry": { "type": "Point", "coordinates": [ 2.351, 48.8567 ], "__parse": true }, "title": "Paris", "bulletSettings": { "fill": { "type": "Color", "value": "#994636" }, "__parse": true }, "__parse": true }, { "geometry": { "type": "Point", "coordinates": [ -21.8952, 64.1353 ], "__parse": true }, "title": "Reykjavik", "bulletSettings": { "fill": { "type": "Color", "value": "#994636" }, "__parse": true }, "__parse": true }, { "geometry": { "type": "Point", "coordinates": [ 37.6176, 55.7558 ], "__parse": true }, "title": "Moscow", "bulletSettings": { "fill": { "type": "Color", "value": "#17183b" }, "__parse": true }, "__parse": true }, { "geometry": { "type": "Point", "coordinates": [ -3.7033, 40.4167 ], "__parse": true }, "title": "Madrid", "bulletSettings": { "fill": { "type": "Color", "value": "#994636" }, "__parse": true }, "__parse": true }, { "geometry": { "type": "Point", "coordinates": [ -0.1262, 51.5002 ], "__parse": true }, "title": "London", "bulletSettings": { "fill": { "type": "Color", "value": "#895b1e" }, "__parse": true }, "__parse": true }, { "geometry": { "type": "Point", "coordinates": [ 116.3958, 39.9056 ], "__parse": true }, "title": "Peking", "bulletSettings": { "fill": { "type": "Color", "value": "#895b1e" }, "__parse": true }, "__parse": true }, { "geometry": { "type": "Point", "coordinates": [ 77.225, 28.6353 ], "__parse": true }, "title": "New Delhi", "bulletSettings": { "fill": { "type": "Color", "value": "#994636" }, "__parse": true }, "__parse": true }, { "geometry": { "type": "Point", "coordinates": [ 139.6823, 35.6785 ], "__parse": true }, "title": "Tokyo", "bulletSettings": { "fill": { "type": "Color", "value": "#17183b" }, "__parse": true }, "__parse": true }, { "geometry": { "type": "Point", "coordinates": [ 32.856, 39.9439 ], "__parse": true }, "title": "Ankara", "bulletSettings": { "fill": { "type": "Color", "value": "#895b1e" }, "__parse": true }, "__parse": true }, { "geometry": { "type": "Point", "coordinates": [ -58.4173, -34.6118 ], "__parse": true }, "title": "Buenos Aires", "bulletSettings": { "fill": { "type": "Color", "value": "#994636" }, "__parse": true }, "__parse": true }, { "geometry": { "type": "Point", "coordinates": [ -47.9292, -15.7801 ], "__parse": true }, "title": "Brasilia", "bulletSettings": { "fill": { "type": "Color", "value": "#895b1e" }, "__parse": true }, "__parse": true }, { "geometry": { "type": "Point", "coordinates": [ -75.6979, 45.4235 ], "__parse": true }, "title": "Ottawa", "bulletSettings": { "fill": { "type": "Color", "value": "#17183b" }, "__parse": true }, "__parse": true }, { "geometry": { "type": "Point", "coordinates": [ -77.0241, 38.8921 ], "__parse": true }, "title": "Washington", "bulletSettings": { "fill": { "type": "Color", "value": "#994636" }, "__parse": true }, "__parse": true }, { "geometry": { "type": "Point", "coordinates": [ 15.3271, -4.3369 ], "__parse": true }, "title": "Kinshasa", "bulletSettings": { "fill": { "type": "Color", "value": "#994636" }, "__parse": true }, "__parse": true }, { "geometry": { "type": "Point", "coordinates": [ 31.2272, 30.0571 ], "__parse": true }, "title": "Cairo", "bulletSettings": { "fill": { "type": "Color", "value": "#895b1e" }, "__parse": true }, "__parse": true }, { "geometry": { "type": "Point", "coordinates": [ 28.1876, -25.7463 ], "__parse": true }, "title": "Pretoria", "bulletSettings": { "fill": { "type": "Color", "value": "#17183b" }, "__parse": true }, "__parse": true } ] }, { "template-0": { "type": "Template", "settings": { "fill": { "type": "Color", "value": "#e6e6e6" } } } }, { "series-0": { "type": "MapPolygonSeries", "settings": { "geoJSON": "am5geodata_worldLow", "exclude": [ "AQ" ] } } }, { "series-1": { "type": "MapLineSeries", "properties": { "mapLines": { "properties": { "template": { "settings": { "stroke": "@root.interfaceColors.get('alternativeBackground')", "strokeOpacity": 0.3 } } } } } } }, { "series-2": { "type": "MapPointSeries", "properties": { "data": "#data-2", "bullets": [ { "type": "Bullet", "settings": { "sprite": { "type": "Graphics", "settings": { "width": 24, "height": 24, "tooltipY": 0, "centerX": { "type": "Percent", "value": 50 }, "centerY": { "type": "Percent", "value": 100 }, "svgPath": "M12,1a9,9,0,0,0-9,9c0,8,9,13,9,13s9-5,9-13A9,9,0,0,0,12,1Zm0,13a4,4,0,1,1,4-4A4,4,0,0,1,12,14Z", "stroke": { "type": "Color", "value": "#ffffff" }, "strokeOpacity": 0.5, "templateField": "bulletSettings", "tooltipText": "{title}", "cursorOverStyle": "pointer" }, "properties": { "template": "#template-0" } } } } ] } } } ]}map-with-custom-markers-and-data-bound-colors