将鼠标悬停在图例上,将鼠标悬停在nvD3中的路径效果上

时间:2019-08-13 11:51:01

标签: javascript css d3.js charts nvd3.js

我正在使用nvd3在我的项目中创建可视化。 我必须创建多种折线图,包括折线图。 这是我的小提琴: https://jsfiddle.net/ykga1jqw/

我的代码:

  // Line chart begins here

  var temperatureIndexJSON = [
    {
      key: "Average temp.",
      values: [{ "x": 1998, "y": 0.45 }, { "x": 1999, "y": 0.48 }, { "x": 2000, "y": 0.5 }, { "x": 2001, "y": 0.52 }, { "x": 2002, "y": 0.55 }, { "x": 2003, "y": 0.58 }, { "x": 2004, "y": 0.6 }, { "x": 2005, "y": 0.61 }, { "x": 2006, "y": 0.61 }, { "x": 2007, "y": 0.61 }, { "x": 2008, "y": 0.62 }, { "x": 2009, "y": 0.62 }, { "x": 2010, "y": 0.62 }, { "x": 2011, "y": 0.63 }, { "x": 2012, "y": 0.67 }, { "x": 2013, "y": 0.71 }, { "x": 2014, "y": 0.77 }, { "x": 2015, "y": 0.83 }, { "x": 2016, "y": 0.89 }, { "x": 2017, "y": 0.95 }]
    }
  ];

    nv.addGraph(function () {
      var chart = nv.models.lineChart() // Initialise the lineChart object.
        .useInteractiveGuideline(true); // Turn on interactive guideline (tooltips)
    chart.xAxis
        .axisLabel('TimeStamp (Year)'); // Set the label of the xAxis (Vertical)
    chart.yAxis
        .axisLabel('Degrees (c)') // Set the label of the xAxis (Horizontal)
        .tickFormat(d3.format('.02f')); // Rounded Numbers Format.
    d3.select('#averageDegreesLineChart svg') // Select the ID of the html element we defined earlier.
        .datum(temperatureIndexJSON) // Pass in the JSON
        .attr('class','line-testing')
        .transition().duration(500) // Set transition speed
        .call(chart); // Call & Render the chart
      nv.utils.windowResize(chart.update); // Intitiate listener for window resize so the chart responds and changes width.
      return;
    });

当用户将鼠标悬停在图例或路径本身上时,我想更改笔触宽度。 与这两个链接类似的效果:

https://jsfiddle.net/ymavtsbj/11/

http://bl.ocks.org/bobmonteverde/2070123

我尝试检查nvd3,但是由于其文档定义不明确,并且没有可用的此类示例,因此到目前为止,我还没有解决方案。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

您可以使用legendMouseoverelementMouseover事件,如下所示:

chart.legend.dispatch.on('legendMouseover', function(e) {
  d3.select('path.nv-line').attr('stroke-width', 4);
});

chart.lines.dispatch.on('elementMouseover', function(e) {
  d3.select('path.nv-line').attr('stroke-width', 4);
});

工作example