简体   繁体   English

amcharts:如何在工具提示中添加操作

[英]amcharts: How to add an action in tooltip

I am trying to add an action in amcharts like:我正在尝试在 amcharts 中添加一个操作,例如:

function myfunc(name){
  alert("hi "+name);
}

var tooltipDetail='<div class="detail" name="{name}" onclick="myfunc({name});">detail {name}</div>'

series1.columns.template.tooltipHTML = tooltipDetail;

When I replace 'myfunc()' by 'alert(1)' it launch an alert but myfunc defined in code launch an error in console 'Uncaught ReferenceError: myfunc is not defined'.当我将 'myfunc()' 替换为 'alert(1)' 时,它会启动一个警报,但代码中定义的 myfunc 在控制台中会启动一个错误 'Uncaught ReferenceError: myfunc is not defined'。 why?为什么? how can I solve this issue?我该如何解决这个问题?

My intention was create a more elaborated function with jquery like:我的意图是使用 jquery 创建一个更详细的 function ,例如:

$('.detail').click(function(){
  var name=this.attr("name");
  $("#selected").html(name);
});

But it does not work, then I simplify the code... I checked $('.detail').html() is not available if tooltip is not showed yet so I think it is built when tooltip is launched.但它不起作用,然后我简化了代码......我检查了 $('.detail').html() 如果工具提示尚未显示则不可用,所以我认为它是在启动工具提示时构建的。

Also I tried to include the function in tooltip like:我还尝试在工具提示中包含 function,例如:

var tooltipDetail='<script>function hi({name}){alert("hi"+name);}</script><div class="detail" name="{name}" onclick="hi({name});">detail {name}</div>'

It results in same issue, 'hi' is not defined.它会导致同样的问题,'hi' 没有定义。

Any recommendation?有什么推荐吗? thanks谢谢

AmCharts seems to have an event system. AmCharts 似乎有一个事件系统。 Try using an on click event handler:尝试使用点击事件处理程序:

function myfunc(){
  alert(1);
}
series.tooltip.events.on('hit', myFunc);

See this modified CodePen where the tooltip is clickable: https://codepen.io/krassdanke/pen/ZEbyQyY (original from official amCharts docs: https://www.amcharts.com/docs/v4/tutorials/clickable-links-in-tooltips/ )请参阅此修改后的 CodePen,其中工具提示可单击: https://codepen.io/krassdanke/pen/ZEbyQyY (来自 amCharts 官方文档: https://www.amcharts.com/docs/v4/tutorials/在工具提示/ )

@dth was in good way, the complete answer would be: @dth 很好,完整的答案是:

function myFunction(name){
  alert(name);
};

series1.columns.template.events.on(
  "hit",
  ev => {myFunction(ev.target._dataItem.dataContext["name"]);},
  this
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM