如何修改AmCharts 4图例自定义提示框及标签颜色
修改amCharts4图例悬停提示框的颜色
要修改图例项悬停时提示框的背景色与文字颜色,可通过配置图例项容器的tooltip模板实现:
- 获取
chart.legend.itemContainers.template.tooltip对象 - 用
background.fill设置提示框背景色 - 用
label.fill设置提示框文字颜色
以下是修改后的完整代码:
JavaScript代码
/** * --------------------------------------- * This demo was created using amCharts 4. * --------------------------------------- */ // 创建图表实例 var chart = am4core.create("chartdiv", am4charts.PieChart); // 添加数据 chart.data = [{ "category": "老无所依", "value": 501.9 }, { "category": "所有漂亮的马", "value": 301.9 }, { "category": "血色子午线", "value": 201.1 }, { "category": "果园守护者", "value": 165.8 }, { "category": "路", "value": 139.9 }]; // 添加并配置系列 var pieSeries = chart.series.push(new am4charts.PieSeries()); pieSeries.dataFields.value = "value"; pieSeries.dataFields.category = "category"; pieSeries.ticks.template.disabled = true; pieSeries.labels.template.disabled = true; chart.legend = new am4charts.Legend(); chart.legend.position = "right"; chart.legend.labels.template.maxWidth = 120; chart.legend.labels.template.truncate = true; chart.legend.itemContainers.template.tooltipText = "{category}"; // 修改提示框样式 var legendTooltip = chart.legend.itemContainers.template.tooltip; // 设置背景色(示例为浅灰色) legendTooltip.background.fill = am4core.color("#f0f0f0"); // 设置文字颜色(示例为深灰色) legendTooltip.label.fill = am4core.color("#333333");
CSS代码
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: 300px; }
HTML代码
<script src="https://www.amcharts.com/lib/4/core.js"></script> <script src="https://www.amcharts.com/lib/4/charts.js"></script> <div id="chartdiv"></div>
你可根据需求将示例中的颜色值(#f0f0f0、#333333)替换为任意十六进制、RGB格式或颜色名称。
内容的提问来源于stack exchange,提问作者Faizan Naeem
相关产品推荐
相关产品推荐

