Recovering x-axis value on click - NVD3 LineChart

自古美人都是妖i 提交于 2019-12-11 09:04:24

问题


Is there any way to recover the x axis value on click event in the lineChart() of NVD3?

The closest I get is by this answer: nvd3.js : unable to bind onClick event with the data points in the svg

But what I want is to recover the x axis value and redirect to another page, passing it as parameter.

I tried this approach too, similar as one that I use on multiBarChart, but unsuccessful:

$("g.nv-point-paths").on("hover", function (d) {
    $("path").off("click");
    $("path").on("click", function (d) {
        //do something with 'd'
    });
});

回答1:


I have been using the following:

chart.lines.dispatch.on('elementClick', function(e) { ... }

The e variable has everything you need inside of it. Just set a breakpoint and inspect the e var to see how to access whatever you want.

Example:

chart.lines.dispatch.on('elementClick', function(e) {
alert(e.point.label);
}



回答2:


I discovered it by myself, by debugging the javascript of the page:

$("g.nv-point-paths").on("hover", function (d) {
    $("g.nv-point-paths path").off("click");
    $("g.nv-point-paths path").on("click", function (d) {
        var xAxisValue = d.currentTarget.__data__.data.point[4].x;
    });
});

If someone have a better solution to this, please answer here.



来源:https://stackoverflow.com/questions/18019048/recovering-x-axis-value-on-click-nvd3-linechart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!