向图表中的所有d3.js数据点添加唯一链接

Sco*_*ell 10 javascript svg d3.js nvd3.js

我正在使用nvd3.js创建一个线图,显示我随时间计算的评级.我有关于每个单独数据点(评级)的更多信息,并希望将图表上的每个数据点链接到一个唯一页面,其中包含有关该特定数据点的更多信息.

例如:我希望能够将鼠标悬停在图表上的第一个数据点(x:1345457533,y:-0.0126262626263)并单击它以转到特定页面(http://www.example.com/) info?id = 1)提供有关该评级或数据点的更多信息.每个数据点都有一个唯一的ID和唯一的URL,我想链接到它.

这是我用来生成图形的代码:

nv.addGraph(function() {
  var chart = nv.models.lineChart();

  chart.xAxis
      .axisLabel('Time')
      .tickFormat(d3.format('r'));

  chart.yAxis
      .axisLabel('Rating')
      .tickFormat(d3.format('.2f'));

  d3.select('#chart svg')
      .datum(data())
      .transition().duration(500)
      .call(chart);

  nv.utils.windowResize(chart.update);

  return chart;
});

function data() {
  var data = [ { x: 1345457533, y: -0.0126262626263 },
               { x: 1345457409, y: 0.0224089635854 },
               { x: 1345457288, y: 0.0270935960591 },
               { x: 1345457168, y: -0.0378151260504 },
               { x: 1345457046, y: -0.115789473684 } ]

  return [
    {
      values: data,
      key: "Sample1",
      color: "#232066"
    }
  ];
}
Run Code Online (Sandbox Code Playgroud)

HTML:

<div id="chart">
  <svg></svg>
</div>
Run Code Online (Sandbox Code Playgroud)

这是一个有效的例子.

Liv*_*uel 5

这是一个有效的解决方案http://jsfiddle.net/66hAj/7/

$('#chart svg').on('click', function(e){
    var elem = $(e.target),
        currentItem, currentUrl;

    if(elem.parent('.nv-point-paths').length) {
        currentItem = e.target.getAttribute('class').match(/\d+/)[0];
        currentUrl = _data[0].urls[ currentItem ];

        $('#log').text(currentUrl);
        //window.location = currentUrl
    }
})
Run Code Online (Sandbox Code Playgroud)

我使用jQuery绑定画布上的单击处理程序,然后根据单击图形上的元素获取数据.

currentItem 为您提供您单击的当前项的ID

currentUrl 提供与当前点击的项目相关的网址.

当您点击图表上的每个点时,您可以在图表下方的div中看到网址更改.