简体   繁体   English

折线图中未定义的D3提示

[英]D3-tip undefined in line chart

First of all; 首先; I know there are similar questions here or there but none of them solved my problem. 我知道这里那里也有类似的问题但没有一个解决了我的问题。

My D3-Tip is not showing on my line chart. 我的D3提示未显示在我的折线图上。 (It works on a map) (在地图上有效)

JS JS

var tip = d3.tip()
    .attr('class', 'd3-tip')
    .html(function(d) {
        console.log(d); //undefined
        return "Sale : " + d.sale;
    });

svg.call(tip);

svg.append("path")
    .attr("class", "line")
    .attr("d", line(data))
    .attr("stroke", "black")
    .attr("stroke-width", 2)
    .attr("fill", "none")
    .on("mouseover", tip.show);

Nothing is shown and I got undefined inside my html function. 什么都没有显示,并且我的html函数内部undefined

What am I missing ? 我想念什么?

In D3, the first argument (traditionally named d ) refers to the data bound. 在D3中,第一个参数(传统名称为d )是指数据绑定。 So, when you write console.log(d) , you expect to see the data bound to that element. 因此,当您编写console.log(d) ,您希望看到绑定到该元素的数据。 But, in your case, you have none! 但是,就您而言,您什么都没有!

A (partial) solution is changing this: 一个(部分)解决方案正在改变这一点:

svg.append("path")
    .attr("class", "line")
    .attr("d", line(data))
    .attr("stroke", "black")
    .attr("stroke-width", 2)
    .attr("fill", "none")
    .on("mouseover", tip.show);

To this: 对此:

svg.append("path")
    .datum(data)
    .attr("class", "line")
    .attr("d", line)
    .attr("stroke", "black")
    .attr("stroke-width", 2)
    .attr("fill", "none")
    .on('mouseover', tip.show);

PS: I wrote partial because it will probably (I don't know your data) log an array of objects, not a single value corresponding to the position of the mouse over the line, which is what you seem to expect. PS:我之所以写成Partial,是因为它可能(我不知道您的数据)记录一个对象数组,而不是一个与鼠标在行上的位置相对应的单个值,这似乎是您期望的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM