最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dynamic Tooltip Text amChart - Stack Overflow

programmeradmin3浏览0评论

I have a Gantt chart made with amCharts and it works fine. As showed below, I set the TooltipText from my ColumnSeries.

var series1 = chart.series.push(new am4charts.ColumnSeries());
series1.columns.template.width = am4core.percent(80);
series1.columns.template.tooltipText = "Load nº: {Load}\nStart: {openDateX}\nEnd: {dateX}\nType: {PartType}";

This part is OK. But now I need dynamically change the TooltipText according to an rule. I read on the documentation about the adapters and i wrote this code.

series1.columns.template.adapter.add('getTooltipText', function(text, target) {
    var data = target.tooltipDataItem.dataContext;
    if (data.Load != null )
       return "Load nº: {data.Load}\nStart: {data.openDateX}\nEnd: {data.dateX}\nType: {data.PartType}";
    else
        return "Start: {data.openDateX}\nEnd: {data.dateX}";
});

But this code is not working. The Tooltip doesn't appear anymore. What I'm doing wrong? Can anyone help me?

I have a Gantt chart made with amCharts and it works fine. As showed below, I set the TooltipText from my ColumnSeries.

var series1 = chart.series.push(new am4charts.ColumnSeries());
series1.columns.template.width = am4core.percent(80);
series1.columns.template.tooltipText = "Load nº: {Load}\nStart: {openDateX}\nEnd: {dateX}\nType: {PartType}";

This part is OK. But now I need dynamically change the TooltipText according to an rule. I read on the documentation about the adapters and i wrote this code.

series1.columns.template.adapter.add('getTooltipText', function(text, target) {
    var data = target.tooltipDataItem.dataContext;
    if (data.Load != null )
       return "Load nº: {data.Load}\nStart: {data.openDateX}\nEnd: {data.dateX}\nType: {data.PartType}";
    else
        return "Start: {data.openDateX}\nEnd: {data.dateX}";
});

But this code is not working. The Tooltip doesn't appear anymore. What I'm doing wrong? Can anyone help me?

Share Improve this question edited May 28, 2019 at 20:21 Shreyas 1,0678 silver badges19 bronze badges asked May 28, 2019 at 20:12 regislsregisls 5297 silver badges23 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

Real close!

If you've seen getTooltipText, that was probably for Axis Tooltips.

For your columns, just tooltipText ought to do it, so try this instead:

series1.columns.template.adapter.add('tooltipText', function(text, target) {
    var data = target.tooltipDataItem.dataContext;
    if (data.Load != null )
       return "Load nº: {Load}\nStart: {openDateX}\nEnd: {dateX}\nType: {PartType}";
    else
        return "Start: {openDateX}\nEnd: {dateX}";
});

Also note I didn't nest the data above via data.fieldName, just fieldName. Check out our guide on Data to learn more about how the charts determine where to look for fieldName, i.e. string placeholders.

In the demo below I use this:

series.columns.template.adapter.add('tooltipText', function(text, target) {
    var data = target.tooltipDataItem.dataContext;
    if (data.Load != null )
       return "Load nº: {categoryX}: [bold]{valueY}[/]";
    else
        return "Start: {categoryX}: [bold]{valueY}[/]";
});

Demo:

https://codepen.io/team/amcharts/pen/ea9f73243aed2d62b768ad168a2e1dcc

This works with the Chart Cursor (am4charts.XYCursor), too.

发布评论

评论列表(0)

  1. 暂无评论