I am currently using amcharts in my project. I have overridden the default tooltip using
pieSeries.slices.template.tooltipHTML = //MY HTML HERE FOR THE TOOLTIP
In the HTML, I want the background color same as the slice color that is hovered. I have gone through the documentation but could not find anything. What I was thinking is maybe if I can get the hover event, then if I get the color or index of currently hovered slice(assuming that I can get the color of the slice from its instance), I can set the color of my HTML Element via Javascript. But I don't know, maybe there is a straight forward way to achieve this as this is not a rare requirement. Any suggestions ?
It seems that amCharts sets the same colour for the tooltip by default, even when custom tooltip HTML is being set:
/**
* ---------------------------------------
* This demo was created using amCharts 4.
*
* For more information visit:
* https://www.amcharts.com/
*
* Documentation is available at:
* https://www.amcharts.com/docs/v4/
* ---------------------------------------
*/
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.PieChart);
// Add data
chart.data = [{
"country": "Lithuania",
"litres": 501.9,
"color": am4core.color("#ED1C24")
}, {
"country": "Czechia",
"litres": 301.9,
"color": am4core.color("#235789")
}, {
"country": "Ireland",
"litres": 201.1,
"color": am4core.color("#F1D302")
}, {
"country": "Germany",
"litres": 165.8,
"color": am4core.color("#020100")
}];
// Add and configure Series
var pieSeries = chart.series.push(new am4charts.PieSeries());
pieSeries.dataFields.value = "litres";
pieSeries.dataFields.category = "country";
pieSeries.slices.template.propertyFields.fill = "color";
pieSeries.slices.template.tooltipHTML = "<p>My custom tooltip color: {color}</p>"
chart.legend = new am4charts.Legend();
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: 400px;
}
<script src="https://cdn.amcharts.com/lib/4/core.js"></script>
<script src="https://cdn.amcharts.com/lib/4/charts.js"></script>
<div id="chartdiv"></div>
(example based on this amCharts demo)
This holds true whether using the custom colours, or theme colours by removing this line:
pieSeries.slices.template.propertyFields.fill = "color";
So maybe this property is being overridden elsewhere in your code. You can force the tooltip to have the same fill colour by adding this line (more details here):
chart.tooltip.getFillFromObject = true;
EDIT: The colour can also be accessed in the tooltip HTML via "{color}"
, so that it could be used in a style attribute, for example.