highcharts clickable labels how to goto anchor

2020-04-17 04:06发布

I'm using Highcharts within my Backbone application to display some information in a column graph.

I use the datalabels in the chart to allow the user to click on and move to the detail page of that datapoint. This is not a normal window.location call, but should be a window.location.hash call. This is because I want to move to an anchor, which will be picked up by my Backbone router.

This is what a typical label (using the formatter) is transformed into:

<tspan onclick="location.href=&quot;/#/test/1&quot;" style="cursor: pointer; " x="360">Sectie nummer 6</tspan>

So in other words, is it possible to have Highcharts do a onclick="window.location.hash=..." instead of location.href?

Thanks in advance!

2条回答
疯言疯语
2楼-- · 2020-04-17 04:50

window.location.hash="comment-16513878" & window.location="#comment-16513878" appear to have the same result. Have you tried that?

formatter: function() {
    return '<a href="#somePlaceHolder" >' + this.y + '</a>';
}

http://jsfiddle.net/jugal/BUxMZ/

查看更多
等我变得足够好
3楼-- · 2020-04-17 04:52

An alternative solution (not the most cross-browser solution because of its dependency on <text> but you can get it working with the incompatible IE versions)

$(".highcharts-data-labels").on("click", "text", function() {
    window.location.hash = "anchor";
});

Why jQuery.on()?

jsFiddle Demo

查看更多
登录 后发表回答