首页 > 解决方案 > plotly_click 不会触发

问题描述

按照此示例https://community.plot.ly/t/hyperlink-to-markers-on-map/17858/6,我试图使我的散点图可点击(具体来说,点击时打开网页)。单击数据点时如何触发单击事件?

这是一个 python turbogears 应用程序,plotly_click事件发生在js. 我正在使用plotly.js v1.44.1.. 就像在示例中一样,我首先创建fig

plotlyHtml = webhelpers2.html.literal(plotly.offline.plot(fig, include_plotlyjs=True, output_type='div'))

然后我div在图中找到了一个:

res = re.search('<div id="([^"]*)"', plotlyHtml)
div_id = res.groups()[0]

然后我构建一个js callback要插入到script模板中的标签中,如示例中所示。这是注入到 html 中的脚本标记:

<script type="text/javascript">


        var plot_element = document.getElementById('3f56277f-e84b-4b68-ae85-91d3cd62d01c');
        console.log('callback js has been added to page. did we get plot_element?');
        console.log(plot_element);

        plot_element.on('plotly_click', function(data){
            console.log('Im inside the plotly_click!!');
            console.log(data);
            var point = data.points[0];
            if (point) {
                console.log(point.customdata);
                window.open(point.customdata);
            }
        })


</script>

console.log渲染页面时,事件侦听器之前的前两个语句会打印出来,这意味着js成功注入html. 特别是,plot_element是一个带有class="plotly-graph-div js-plotly-plot". 但是,单击散点图上的点不会产生任何结果,不会产生任何错误、事件或控制台日志语句。我至少期望控制台日志语句if会发生。我选择了错误的 div id 吗?

标签: onclickplotlyplotly.jsplotly-python

解决方案


问题出在我的layout. 我有'clickmode': 'select',这把事情搞砸了。删除该启用事件按预期触发。


推荐阅读