Adaptively changing tooltip orientation based on column value
This demo shows how we can use an adapter to change the orientation of a ColumnSeries tooltip based on the column value.
Related code
Section titled “Related code”The following code will change value of tooltip’s pointerOrientation to either point down or up based on whether target column’s value is above or below zero.
series.get("tooltip").adapters.add("pointerOrientation", function(orientation, target) { return target.dataItem && target.dataItem.get("valueY") < 0 ? "up": orientation;});series.get("tooltip").adapters.add("pointerOrientation", function(orientation, target) { return target.dataItem && target.dataItem.get("valueY") < 0 ? "up": orientation;});/** * --------------------------------------- * 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, {}));
// Add cursor// https://www.amcharts.com/docs/v5/charts/xy-chart/cursor/var cursor = chart.set("cursor", am5xy.XYCursor.new(root, {}));//cursor.lineY.set("visible", false);
// Create axes// https://www.amcharts.com/docs/v5/charts/xy-chart/axes/var xRenderer = am5xy.AxisRendererX.new(root, { minGridDistance: 30, minorGridEnabled: true});
xRenderer.labels.template.setAll({ rotation: -90, centerY: am5.p50, centerX: am5.p100, paddingRight: 15});
xRenderer.grid.template.setAll({ location: 1});
var xAxis = chart.xAxes.push( am5xy.CategoryAxis.new(root, { maxDeviation: 0.3, categoryField: "country", renderer: xRenderer }));
var yRenderer = am5xy.AxisRendererY.new(root, { strokeOpacity: 0.1});
var yAxis = chart.yAxes.push( am5xy.ValueAxis.new(root, { maxDeviation: 0.3, renderer: yRenderer }));
// Create series// https://www.amcharts.com/docs/v5/charts/xy-chart/series/var series = chart.series.push( am5xy.ColumnSeries.new(root, { name: "Series 1", xAxis: xAxis, yAxis: yAxis, valueYField: "value", sequencedInterpolation: true, categoryXField: "country", tooltip: am5.Tooltip.new(root, { labelText: "{valueY}", pointerOrientation: "down" }) }));
series.get("tooltip").adapters.add("pointerOrientation", function (orientation, target) { return target.dataItem && target.dataItem.get("valueY") < 0 ? "up" : orientation;});
series.columns.template.setAll({ cornerRadiusTL: 5, cornerRadiusTR: 5, strokeOpacity: 0 });series.columns.template.adapters.add("fill", function (fill, target) { return chart.get("colors").getIndex(series.columns.indexOf(target));});
series.columns.template.adapters.add("stroke", function (stroke, target) { return chart.get("colors").getIndex(series.columns.indexOf(target));});
// Set datavar data = [ { country: "USA", value: 2025 }, { country: "China", value: 1882 }, { country: "Japan", value: -1809 }, { country: "Germany", value: 1322 }, { country: "UK", value: -1022 }, { country: "France", value: 1114 }, { country: "India", value: 984 }, { country: "Spain", value: 711 }, { country: "Netherlands", value: -65 }, { country: "South Korea", value: -43 }, { country: "Canada", value: 441 }];
xAxis.data.setAll(data);series.data.setAll(data);
// Make stuff animate on load// https://www.amcharts.com/docs/v5/concepts/animations/series.appear(1000);chart.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": "XYChart", "settings": { "cursor": { "type": "XYCursor" } }, "properties": { "series": [ "#series-0" ], "xAxes": [ "#axis-0" ], "yAxes": [ "#axis-1" ] }, "refs": [ { "data-0": [ { "country": "USA", "value": 2025 }, { "country": "China", "value": 1882 }, { "country": "Japan", "value": -1809 }, { "country": "Germany", "value": 1322 }, { "country": "UK", "value": -1022 }, { "country": "France", "value": 1114 }, { "country": "India", "value": 984 }, { "country": "Spain", "value": 711 }, { "country": "Netherlands", "value": -65 }, { "country": "South Korea", "value": -43 }, { "country": "Canada", "value": 441 } ] }, { "axis-0": { "type": "CategoryAxis", "settings": { "maxDeviation": 0.3, "categoryField": "country", "renderer": { "type": "AxisRendererX", "settings": { "minGridDistance": 30, "minorGridEnabled": true }, "properties": { "labels": { "properties": { "template": { "settings": { "rotation": -90, "centerY": { "type": "Percent", "value": 50 }, "centerX": { "type": "Percent", "value": 100 }, "paddingRight": 15 } } } }, "grid": { "properties": { "template": { "settings": { "location": 1 } } } } } } }, "properties": { "data": "#data-0" } } }, { "axis-1": { "type": "ValueAxis", "settings": { "maxDeviation": 0.3, "renderer": { "type": "AxisRendererY", "settings": { "strokeOpacity": 0.1 } } } } }, { "series-0": { "type": "ColumnSeries", "settings": { "name": "Series 1", "xAxis": "#axis-0", "yAxis": "#axis-1", "valueYField": "value", "sequencedInterpolation": true, "categoryXField": "country", "tooltip": { "type": "Tooltip", "settings": { "labelText": "{valueY}", "pointerOrientation": "down" } } }, "properties": { "data": "#data-0", "columns": { "properties": { "template": { "settings": { "cornerRadiusTL": 5, "cornerRadiusTR": 5, "strokeOpacity": 0 } } } } } } } ]}adaptive-tooltip-orientation-based-on-value