Highlight areas of an XY chart using a column series
This demo shows how we can use a separate ColumnSeries to add square-shaped fills at any place on an XY chart.
let highlightSeries;function addHighlight(data) { if (!highlightSeries) { highlightSeries = chart.series.push( am5xy.ColumnSeries.new(root, { xAxis: xAxis, yAxis: yAxis, valueYField: "y2", openValueYField: "y1", valueXField: "x2", openValueXField: "x1" }) ); } highlightSeries.columns.template.setAll({ templateField: "settings", strokeOpacity: 0 }); highlightSeries.data.push(data);}var highlightSeries;function addHighlight(data) { if (!highlightSeries) { highlightSeries = chart.series.push( am5xy.ColumnSeries.new(root, { xAxis: xAxis, yAxis: yAxis, valueYField: "y2", openValueYField: "y1", valueXField: "x2", openValueXField: "x1" }) ); } highlightSeries.columns.template.setAll({ templateField: "settings", strokeOpacity: 0 }); highlightSeries.data.push(data);}/** * --------------------------------------- * 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)]);
var data = [ { date: new Date(2012, 1, 1).getTime(), value: 8 }, { date: new Date(2012, 1, 2).getTime(), value: 10 }, { date: new Date(2012, 1, 3).getTime(), value: 12 }, { date: new Date(2012, 1, 4).getTime(), value: 14 }, { date: new Date(2012, 1, 5).getTime(), value: 11 }, { date: new Date(2012, 1, 6).getTime(), value: 6 }, { date: new Date(2012, 1, 7).getTime(), value: 7 }, { date: new Date(2012, 1, 8).getTime(), value: 9 }, { date: new Date(2012, 1, 9).getTime(), value: 13 }, { date: new Date(2012, 1, 10).getTime(), value: 15 }, { date: new Date(2012, 1, 11).getTime(), value: 19 }, { date: new Date(2012, 1, 12).getTime(), value: 21 }, { date: new Date(2012, 1, 13).getTime(), value: 22 }, { date: new Date(2012, 1, 14).getTime(), value: 20 }, { date: new Date(2012, 1, 15).getTime(), value: 18 }, { date: new Date(2012, 1, 16).getTime(), value: 14 }, { date: new Date(2012, 1, 17).getTime(), value: 16 }, { date: new Date(2012, 1, 18).getTime(), value: 18 }, { date: new Date(2012, 1, 19).getTime(), value: 17 }, { date: new Date(2012, 1, 20).getTime(), value: 15 }, { date: new Date(2012, 1, 21).getTime(), value: 12 }, { date: new Date(2012, 1, 22).getTime(), value: 10 }, { date: new Date(2012, 1, 23).getTime(), value: 8 }];
// Create chart// https://www.amcharts.com/docs/v5/charts/xy-chart/var chart = root.container.children.push( am5xy.XYChart.new(root, { panX: true, panY: true, wheelX: "panX", wheelY: "zoomX" }));
// Create axes// https://www.amcharts.com/docs/v5/charts/xy-chart/axes/var xAxis = chart.xAxes.push( am5xy.DateAxis.new(root, { maxDeviation: 0.1, groupData: false, baseInterval: { timeUnit: "day", count: 1 },
renderer: am5xy.AxisRendererX.new(root, { minGridDistance: 50 }),
tooltip: am5.Tooltip.new(root, {}) }));
var yAxis = chart.yAxes.push( am5xy.ValueAxis.new(root, { maxDeviation: 0.1, renderer: am5xy.AxisRendererY.new(root, {}) }));
// Add series// https://www.amcharts.com/docs/v5/charts/xy-chart/series/var series = chart.series.push( am5xy.LineSeries.new(root, { minBulletDistance: 10, xAxis: xAxis, yAxis: yAxis, valueYField: "value", valueXField: "date", tooltip: am5.Tooltip.new(root, { pointerOrientation: "horizontal", labelText: "{valueY}" }) }));
series.data.setAll(data);
series.bullets.push(function () { return am5.Bullet.new(root, { sprite: am5.Circle.new(root, { radius: 5, fill: series.get("fill"), stroke: root.interfaceColors.get("background"), strokeWidth: 2 }) });});
// Add cursor// https://www.amcharts.com/docs/v5/charts/xy-chart/cursor/var cursor = chart.set( "cursor", am5xy.XYCursor.new(root, { xAxis: xAxis }));
cursor.lineY.set("visible", false);
// add scrollbarchart.set( "scrollbarX", am5.Scrollbar.new(root, { orientation: "horizontal" }));
// Make stuff animate on load// https://www.amcharts.com/docs/v5/concepts/animations/series.appear(1000, 100);chart.appear(1000, 100);
// Create a function to add highlight squaresvar highlightSeries;function addHighlight(data) { if (!highlightSeries) { highlightSeries = chart.series.push( am5xy.ColumnSeries.new(root, { xAxis: xAxis, yAxis: yAxis, valueYField: "y2", openValueYField: "y1", valueXField: "x2", openValueXField: "x1" }) ); } highlightSeries.columns.template.setAll({ templateField: "settings", strokeOpacity: 0 });
highlightSeries.data.push(data);}
addHighlight({ x1: new Date(2012, 1, 3).getTime(), x2: new Date(2012, 1, 7).getTime(), y1: 6, y2: 15, settings: { fill: am5.color(0xff0000), fillOpacity: 0.2 }});
addHighlight({ x1: new Date(2012, 1, 9).getTime(), x2: new Date(2012, 1, 15).getTime(), y1: 10, y2: 20, settings: { fill: am5.color(0x00ff00), fillOpacity: 0.2 }});
addHighlight({ x1: new Date(2012, 1, 11).getTime(), x2: new Date(2012, 1, 22).getTime(), y1: 7, y2: 9, settings: { fill: am5.color(0x0000ff), fillOpacity: 0.2 }});<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": true, "panY": true, "wheelX": "panX", "wheelY": "zoomX", "cursor": { "type": "XYCursor", "settings": { "xAxis": "#axis-0" }, "properties": { "lineY": { "settings": { "visible": false } } } }, "scrollbarX": { "type": "Scrollbar", "settings": { "orientation": "horizontal" } } }, "properties": { "series": [ "#series-0", "#series-1" ], "xAxes": [ "#axis-0" ], "yAxes": [ "#axis-1" ] }, "refs": [ { "data-0": [ { "date": 1328047200000, "value": 8 }, { "date": 1328133600000, "value": 10 }, { "date": 1328220000000, "value": 12 }, { "date": 1328306400000, "value": 14 }, { "date": 1328392800000, "value": 11 }, { "date": 1328479200000, "value": 6 }, { "date": 1328565600000, "value": 7 }, { "date": 1328652000000, "value": 9 }, { "date": 1328738400000, "value": 13 }, { "date": 1328824800000, "value": 15 }, { "date": 1328911200000, "value": 19 }, { "date": 1328997600000, "value": 21 }, { "date": 1329084000000, "value": 22 }, { "date": 1329170400000, "value": 20 }, { "date": 1329256800000, "value": 18 }, { "date": 1329343200000, "value": 14 }, { "date": 1329429600000, "value": 16 }, { "date": 1329516000000, "value": 18 }, { "date": 1329602400000, "value": 17 }, { "date": 1329688800000, "value": 15 }, { "date": 1329775200000, "value": 12 }, { "date": 1329861600000, "value": 10 }, { "date": 1329948000000, "value": 8 } ] }, { "data-1": [ { "x1": 1328220000000, "x2": 1328565600000, "y1": 6, "y2": 15, "settings": { "fill": { "type": "Color", "value": "#ff0000" }, "fillOpacity": 0.2, "__parse": true }, "__parse": true }, { "x1": 1328738400000, "x2": 1329256800000, "y1": 10, "y2": 20, "settings": { "fill": { "type": "Color", "value": "#00ff00" }, "fillOpacity": 0.2, "__parse": true }, "__parse": true }, { "x1": 1328911200000, "x2": 1329861600000, "y1": 7, "y2": 9, "settings": { "fill": { "type": "Color", "value": "#0000ff" }, "fillOpacity": 0.2, "__parse": true }, "__parse": true } ] }, { "axis-0": { "type": "DateAxis", "settings": { "maxDeviation": 0.1, "groupData": false, "baseInterval": { "timeUnit": "day", "count": 1 }, "renderer": { "type": "AxisRendererX", "settings": { "minGridDistance": 50 } }, "tooltip": { "type": "Tooltip" } } } }, { "axis-1": { "type": "ValueAxis", "settings": { "maxDeviation": 0.1, "renderer": { "type": "AxisRendererY" } } } }, { "series-0": { "type": "LineSeries", "settings": { "minBulletDistance": 10, "xAxis": "#axis-0", "yAxis": "#axis-1", "valueYField": "value", "valueXField": "date", "tooltip": { "type": "Tooltip", "settings": { "pointerOrientation": "horizontal", "labelText": "{valueY}" } } }, "properties": { "data": "#data-0", "bullets": [ { "type": "Bullet", "settings": { "sprite": { "type": "Circle", "settings": { "radius": 5, "fill": "@series.get('fill')", "stroke": "@root.interfaceColors.get('background')", "strokeWidth": 2 } } } } ] } } }, { "series-1": { "type": "ColumnSeries", "settings": { "xAxis": "#axis-0", "yAxis": "#axis-1", "valueYField": "y2", "openValueYField": "y1", "valueXField": "x2", "openValueXField": "x1" }, "properties": { "data": "#data-1", "columns": { "properties": { "template": { "settings": { "templateField": "settings", "strokeOpacity": 0 } } } } } } } ]}using-columnseries-to-highlight-square-areas-on-an-xychart