A custom "heat legend" using a gradient
This demo adds a Container element with a gradient-filled rectangle and some labels to create a custom “heat legend” using multi-color steps.
The code
Section titled “The code”let legend = chart.children.push(am5.Container.new(root, { width: am5.p100, height: 100, layout: root.verticalLayout}));
let gradient = am5.LinearGradient.new(root, { stops: [ { color: am5.color(0xca0101), offset: 0 }, { color: am5.color(0xca0101), offset: 0.2 }, { color: am5.color(0xe17a2d), offset: 0.2 }, { color: am5.color(0xe17a2d), offset: 0.4 }, { color: am5.color(0xe1d92d), offset: 0.4 }, { color: am5.color(0xe1d92d), offset: 0.6 }, { color: am5.color(0x5dbe24), offset: 0.6 }, { color: am5.color(0x5dbe24), offset: 0.8 }, { color: am5.color(0x0b7d03), offset: 0.8 }, { color: am5.color(0x0b7d03), offset: 1 } ], rotation: 0});
let labels = legend.children.push(am5.Container.new(root, { width: am5.p100}))
let fromLabel = labels.children.push(am5.Label.new(root, { text: "Critical"}))
let toLabel = labels.children.push(am5.Label.new(root, { text: "Perfect", x: am5.p100, centerX: am5.p100}))
let bar = legend.children.push(am5.Rectangle.new(root, { width: am5.p100, height: 40, fillGradient: gradient, fillOpacity: 1}));var legend = chart.children.push(am5.Container.new(root, { width: am5.p100, height: 100, layout: root.verticalLayout}));
var gradient = am5.LinearGradient.new(root, { stops: [ { color: am5.color(0xca0101), offset: 0 }, { color: am5.color(0xca0101), offset: 0.2 }, { color: am5.color(0xe17a2d), offset: 0.2 }, { color: am5.color(0xe17a2d), offset: 0.4 }, { color: am5.color(0xe1d92d), offset: 0.4 }, { color: am5.color(0xe1d92d), offset: 0.6 }, { color: am5.color(0x5dbe24), offset: 0.6 }, { color: am5.color(0x5dbe24), offset: 0.8 }, { color: am5.color(0x0b7d03), offset: 0.8 }, { color: am5.color(0x0b7d03), offset: 1 } ], rotation: 0});
var labels = legend.children.push(am5.Container.new(root, { width: am5.p100}))
var fromLabel = labels.children.push(am5.Label.new(root, { text: "Critical"}))
var toLabel = labels.children.push(am5.Label.new(root, { text: "Perfect", x: am5.p100, centerX: am5.p100}))
var bar = legend.children.push(am5.Rectangle.new(root, { width: am5.p100, height: 40, fillGradient: gradient, fillOpacity: 1}));Related docs
Section titled “Related docs”The demo
Section titled “The demo”/** * --------------------------------------- * 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 chart// https://www.amcharts.com/docs/v5/charts/xy-chart/var chart = root.container.children.push( am5xy.XYChart.new(root, { panX: false, panY: false, wheelX: "none", wheelY: "none", layout: root.verticalLayout }));
// Create axes and their renderersvar yRenderer = am5xy.AxisRendererY.new(root, { visible: false, minGridDistance: 20, inversed: true});
yRenderer.grid.template.set("visible", false);
var yAxis = chart.yAxes.push( am5xy.CategoryAxis.new(root, { renderer: yRenderer, categoryField: "category" }));
var xRenderer = am5xy.AxisRendererX.new(root, { visible: false, minGridDistance: 30, inversed: true});
xRenderer.grid.template.set("visible", false);
var xAxis = chart.xAxes.push( am5xy.CategoryAxis.new(root, { renderer: xRenderer, categoryField: "category" }));
// Create series// https://www.amcharts.com/docs/v5/charts/xy-chart/#Adding_seriesvar series = chart.series.push( am5xy.ColumnSeries.new(root, { calculateAggregates: true, stroke: am5.color(0xffffff), clustered: false, xAxis: xAxis, yAxis: yAxis, categoryXField: "x", categoryYField: "y", valueField: "value" }));
series.columns.template.setAll({ tooltipText: "{value}", strokeOpacity: 1, strokeWidth: 2, cornerRadiusTL: 5, cornerRadiusTR: 5, cornerRadiusBL: 5, cornerRadiusBR: 5, width: am5.percent(100), height: am5.percent(100), templateField: "columnSettings"});
var circleTemplate = am5.Template.new({});
// Add heat rule// https://www.amcharts.com/docs/v5/concepts/settings/heat-rules/series.set("heatRules", [ { target: circleTemplate, min: 10, max: 35, dataField: "value", key: "radius" }]);
series.bullets.push(function () { return am5.Bullet.new(root, { sprite: am5.Circle.new( root, { fill: am5.color(0x000000), fillOpacity: 0.5, strokeOpacity: 0 },
circleTemplate ) });});
series.bullets.push(function () { return am5.Bullet.new(root, { sprite: am5.Label.new(root, { fill: am5.color(0xffffff), populateText: true, centerX: am5.p50, centerY: am5.p50, fontSize: 10, text: "{value}" }) });});
var colors = { critical: am5.color(0xca0101), bad: am5.color(0xe17a2d), medium: am5.color(0xe1d92d), good: am5.color(0x5dbe24), verygood: am5.color(0x0b7d03)};
// Set data// https://www.amcharts.com/docs/v5/charts/xy-chart/#Setting_datavar data = [ { y: "Critical", x: "Very good", columnSettings: { fill: colors.medium },
value: 20 },
{ y: "Bad", x: "Very good", columnSettings: { fill: colors.good },
value: 15 },
{ y: "Medium", x: "Very good", columnSettings: { fill: colors.verygood },
value: 25 },
{ y: "Good", x: "Very good", columnSettings: { fill: colors.verygood },
value: 15 },
{ y: "Very good", x: "Very good", columnSettings: { fill: colors.verygood },
value: 12 },
{ y: "Critical", x: "Good", columnSettings: { fill: colors.bad },
value: 30 },
{ y: "Bad", x: "Good", columnSettings: { fill: colors.medium },
value: 24 },
{ y: "Medium", x: "Good", columnSettings: { fill: colors.good },
value: 25 },
{ y: "Good", x: "Good", columnSettings: { fill: colors.verygood },
value: 15 },
{ y: "Very good", x: "Good", columnSettings: { fill: colors.verygood },
value: 25 },
{ y: "Critical", x: "Medium", columnSettings: { fill: colors.bad },
value: 33 },
{ y: "Bad", x: "Medium", columnSettings: { fill: colors.bad },
value: 14 },
{ y: "Medium", x: "Medium", columnSettings: { fill: colors.medium },
value: 20 },
{ y: "Good", x: "Medium", columnSettings: { fill: colors.good },
value: 19 },
{ y: "Very good", x: "Medium", columnSettings: { fill: colors.good },
value: 25 },
{ y: "Critical", x: "Bad", columnSettings: { fill: colors.critical },
value: 31 },
{ y: "Bad", x: "Bad", columnSettings: { fill: colors.critical },
value: 24 },
{ y: "Medium", x: "Bad", columnSettings: { fill: colors.bad },
value: 25 },
{ y: "Good", x: "Bad", columnSettings: { fill: colors.medium },
value: 15 },
{ y: "Very good", x: "Bad", columnSettings: { fill: colors.good },
value: 15 },
{ y: "Critical", x: "Critical", columnSettings: { fill: colors.critical },
value: 12 },
{ y: "Bad", x: "Critical", columnSettings: { fill: colors.critical },
value: 14 },
{ y: "Medium", x: "Critical", columnSettings: { fill: colors.critical },
value: 15 },
{ y: "Good", x: "Critical", columnSettings: { fill: colors.bad },
value: 25 },
{ y: "Very good", x: "Critical", columnSettings: { fill: colors.medium },
value: 19 }];
series.data.setAll(data);
yAxis.data.setAll([ { category: "Critical" }, { category: "Bad" }, { category: "Medium" }, { category: "Good" }, { category: "Very good" }]);
xAxis.data.setAll([ { category: "Critical" }, { category: "Bad" }, { category: "Medium" }, { category: "Good" }, { category: "Very good" }]);
// Make stuff animate on load// https://www.amcharts.com/docs/v5/concepts/animations/#Initial_animationchart.appear(1000, 100);
// Custom heat legendvar legend = chart.children.push( am5.Container.new(root, { width: am5.p100, height: 100, layout: root.verticalLayout }));
var gradient = am5.LinearGradient.new(root, { stops: [ { color: colors.critical, offset: 0 }, { color: colors.critical, offset: 0.2 }, { color: colors.bad, offset: 0.2 }, { color: colors.bad, offset: 0.4 }, { color: colors.medium, offset: 0.4 }, { color: colors.medium, offset: 0.6 }, { color: colors.good, offset: 0.6 }, { color: colors.good, offset: 0.8 }, { color: colors.verygood, offset: 0.8 }, { color: colors.verygood, offset: 1 } ],
rotation: 0});
var labels = legend.children.push( am5.Container.new(root, { width: am5.p100 }));
var fromLabel = labels.children.push( am5.Label.new(root, { text: "Critical" }));
var toLabel = labels.children.push( am5.Label.new(root, { text: "Perfect", x: am5.p100, centerX: am5.p100 }));
var bar = legend.children.push( am5.Rectangle.new(root, { width: am5.p100, height: 40, fillGradient: gradient, fillOpacity: 1 }));<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": "XYChart", "settings": { "panX": false, "panY": false, "wheelX": "none", "wheelY": "none", "layout": { "type": "VerticalLayout" } }, "properties": { "series": [ "#series-0" ], "xAxes": [ "#axis-0" ], "yAxes": [ "#axis-1" ] }, "children": [ { "type": "Container", "settings": { "width": { "type": "Percent", "value": 100 }, "height": 100, "layout": { "type": "VerticalLayout" } }, "children": [ { "type": "Container", "settings": { "width": { "type": "Percent", "value": 100 } }, "children": [ { "type": "Label", "settings": { "text": "Critical" } }, { "type": "Label", "settings": { "text": "Perfect", "x": { "type": "Percent", "value": 100 }, "centerX": { "type": "Percent", "value": 100 } } } ] }, { "type": "Rectangle", "settings": { "width": { "type": "Percent", "value": 100 }, "height": 40, "fillGradient": { "type": "LinearGradient", "settings": { "stops": [ { "color": { "type": "Color", "value": "#ca0101" }, "offset": 0 }, { "color": { "type": "Color", "value": "#ca0101" }, "offset": 0.2 }, { "color": { "type": "Color", "value": "#e17a2d" }, "offset": 0.2 }, { "color": { "type": "Color", "value": "#e17a2d" }, "offset": 0.4 }, { "color": { "type": "Color", "value": "#e1d92d" }, "offset": 0.4 }, { "color": { "type": "Color", "value": "#e1d92d" }, "offset": 0.6 }, { "color": { "type": "Color", "value": "#5dbe24" }, "offset": 0.6 }, { "color": { "type": "Color", "value": "#5dbe24" }, "offset": 0.8 }, { "color": { "type": "Color", "value": "#0b7d03" }, "offset": 0.8 }, { "color": { "type": "Color", "value": "#0b7d03" }, "offset": 1 } ], "rotation": 0 } }, "fillOpacity": 1 } } ] } ], "refs": [ { "data-0": [ { "y": "Critical", "x": "Very good", "columnSettings": { "fill": { "type": "Color", "value": "#e1d92d" }, "__parse": true }, "value": 20, "__parse": true }, { "y": "Bad", "x": "Very good", "columnSettings": { "fill": { "type": "Color", "value": "#5dbe24" }, "__parse": true }, "value": 15, "__parse": true }, { "y": "Medium", "x": "Very good", "columnSettings": { "fill": { "type": "Color", "value": "#0b7d03" }, "__parse": true }, "value": 25, "__parse": true }, { "y": "Good", "x": "Very good", "columnSettings": { "fill": { "type": "Color", "value": "#0b7d03" }, "__parse": true }, "value": 15, "__parse": true }, { "y": "Very good", "x": "Very good", "columnSettings": { "fill": { "type": "Color", "value": "#0b7d03" }, "__parse": true }, "value": 12, "__parse": true }, { "y": "Critical", "x": "Good", "columnSettings": { "fill": { "type": "Color", "value": "#e17a2d" }, "__parse": true }, "value": 30, "__parse": true }, { "y": "Bad", "x": "Good", "columnSettings": { "fill": { "type": "Color", "value": "#e1d92d" }, "__parse": true }, "value": 24, "__parse": true }, { "y": "Medium", "x": "Good", "columnSettings": { "fill": { "type": "Color", "value": "#5dbe24" }, "__parse": true }, "value": 25, "__parse": true }, { "y": "Good", "x": "Good", "columnSettings": { "fill": { "type": "Color", "value": "#0b7d03" }, "__parse": true }, "value": 15, "__parse": true }, { "y": "Very good", "x": "Good", "columnSettings": { "fill": { "type": "Color", "value": "#0b7d03" }, "__parse": true }, "value": 25, "__parse": true }, { "y": "Critical", "x": "Medium", "columnSettings": { "fill": { "type": "Color", "value": "#e17a2d" }, "__parse": true }, "value": 33, "__parse": true }, { "y": "Bad", "x": "Medium", "columnSettings": { "fill": { "type": "Color", "value": "#e17a2d" }, "__parse": true }, "value": 14, "__parse": true }, { "y": "Medium", "x": "Medium", "columnSettings": { "fill": { "type": "Color", "value": "#e1d92d" }, "__parse": true }, "value": 20, "__parse": true }, { "y": "Good", "x": "Medium", "columnSettings": { "fill": { "type": "Color", "value": "#5dbe24" }, "__parse": true }, "value": 19, "__parse": true }, { "y": "Very good", "x": "Medium", "columnSettings": { "fill": { "type": "Color", "value": "#5dbe24" }, "__parse": true }, "value": 25, "__parse": true }, { "y": "Critical", "x": "Bad", "columnSettings": { "fill": { "type": "Color", "value": "#ca0101" }, "__parse": true }, "value": 31, "__parse": true }, { "y": "Bad", "x": "Bad", "columnSettings": { "fill": { "type": "Color", "value": "#ca0101" }, "__parse": true }, "value": 24, "__parse": true }, { "y": "Medium", "x": "Bad", "columnSettings": { "fill": { "type": "Color", "value": "#e17a2d" }, "__parse": true }, "value": 25, "__parse": true }, { "y": "Good", "x": "Bad", "columnSettings": { "fill": { "type": "Color", "value": "#e1d92d" }, "__parse": true }, "value": 15, "__parse": true }, { "y": "Very good", "x": "Bad", "columnSettings": { "fill": { "type": "Color", "value": "#5dbe24" }, "__parse": true }, "value": 15, "__parse": true }, { "y": "Critical", "x": "Critical", "columnSettings": { "fill": { "type": "Color", "value": "#ca0101" }, "__parse": true }, "value": 12, "__parse": true }, { "y": "Bad", "x": "Critical", "columnSettings": { "fill": { "type": "Color", "value": "#ca0101" }, "__parse": true }, "value": 14, "__parse": true }, { "y": "Medium", "x": "Critical", "columnSettings": { "fill": { "type": "Color", "value": "#ca0101" }, "__parse": true }, "value": 15, "__parse": true }, { "y": "Good", "x": "Critical", "columnSettings": { "fill": { "type": "Color", "value": "#e17a2d" }, "__parse": true }, "value": 25, "__parse": true }, { "y": "Very good", "x": "Critical", "columnSettings": { "fill": { "type": "Color", "value": "#e1d92d" }, "__parse": true }, "value": 19, "__parse": true } ] }, { "data-1": [ { "category": "Critical" }, { "category": "Bad" }, { "category": "Medium" }, { "category": "Good" }, { "category": "Very good" } ] }, { "template-0": { "type": "Template" } }, { "axis-0": { "type": "CategoryAxis", "settings": { "renderer": { "type": "AxisRendererX", "settings": { "visible": false, "minGridDistance": 30, "inversed": true }, "properties": { "grid": { "properties": { "template": { "settings": { "visible": false } } } } } }, "categoryField": "category" }, "properties": { "data": "#data-1" } } }, { "axis-1": { "type": "CategoryAxis", "settings": { "renderer": { "type": "AxisRendererY", "settings": { "visible": false, "minGridDistance": 20, "inversed": true }, "properties": { "grid": { "properties": { "template": { "settings": { "visible": false } } } } } }, "categoryField": "category" }, "properties": { "data": "#data-1" } } }, { "series-0": { "type": "ColumnSeries", "settings": { "calculateAggregates": true, "stroke": { "type": "Color", "value": "#ffffff" }, "clustered": false, "xAxis": "#axis-0", "yAxis": "#axis-1", "categoryXField": "x", "categoryYField": "y", "valueField": "value", "heatRules": [ { "target": "#template-0", "min": 10, "max": 35, "dataField": "value", "key": "radius", "__parse": true } ] }, "properties": { "data": "#data-0", "columns": { "properties": { "template": { "settings": { "tooltipText": "{value}", "strokeOpacity": 1, "strokeWidth": 2, "cornerRadiusTL": 5, "cornerRadiusTR": 5, "cornerRadiusBL": 5, "cornerRadiusBR": 5, "width": { "type": "Percent", "value": 100 }, "height": { "type": "Percent", "value": 100 }, "templateField": "columnSettings" } } } }, "bullets": [ { "type": "Bullet", "settings": { "sprite": { "type": "Circle", "settings": { "fill": { "type": "Color", "value": "#000000" }, "fillOpacity": 0.5, "strokeOpacity": 0 }, "properties": { "template": "#template-0" } } } }, { "type": "Bullet", "settings": { "sprite": { "type": "Label", "settings": { "fill": { "type": "Color", "value": "#ffffff" }, "populateText": true, "centerX": { "type": "Percent", "value": 50 }, "centerY": { "type": "Percent", "value": 50 }, "fontSize": 10, "text": "{value}" } } } } ] } } } ]}a-chart-with-custom-heat-legend-using-a-gradient