[英]d3-tip doesn't work in a multiline chart
I am just learning how to use d3-tip in a multiline chart. 我正在学习如何在多折线图中使用d3-tip。 I took this example for practising: http://bl.ocks.org/d3noob/d8be922a10cb0b148cd5 I've added d3-tip to the code but the tooltip doesn't appear .
我以以下示例进行了练习: http : //bl.ocks.org/d3noob/d8be922a10cb0b148cd5我已在代码中添加了d3-tip,但工具提示未出现 。 The chart is displayed correctly , but when I check the console: "Uncaught TypeError: Cannot read property 'value' of undefined" pointing to this code line:
图表显示正确 ,但是当我检查控制台时:指向此代码行的“ Uncaught TypeError:无法读取未定义的属性'value'” :
.html(function(d) {
return "<b>" + d.value + "</b>";
})
Here I show you the script: 在这里,我向您展示脚本:
<script src="d3/d3.min.js" charset="utf-8"></script>
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<script>
// Set the dimensions of the canvas / graph
var margin = {top: 30, right: 20, bottom: 30, left: 50},
width = 600 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom;
// Parse the date / time
var parseDate = d3.time.format("%b %Y").parse;
// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
// Define the axes
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(5);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5);
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([0, 5])
.direction('n')
.html(function(d) {
return "<b>" + d.value + "</b>";
})
// Define the line
var priceline = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.value); });
// Adds the svg canvas
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
svg.call(tip);
// Get the data
d3.csv("stocks.csv", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.value = +d.value;
});
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.value; })]);
// Nest the entries by symbol
var dataNest = d3.nest()
.key(function(d) {return d.name;})
.entries(data);
// Loop through each symbol / key
dataNest.forEach(function(d) {
svg.append("path")
.attr("class", "line")
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
.attr("d", priceline(d.values))
});
// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add the Y Axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
});
function type(d) {
d.value = +d.value;
return d;
}
</script>
I have changed many things, but I don't find the way. 我已经改变了很多事情,但是我找不到路。 Any idea?
任何想法? Thanks in advance.
提前致谢。
You don't have any data bound to the elements you're calling the tooltip on. 您没有任何数据绑定到调用工具提示的元素。 You don't need a loop, just use D3's data binding:
您不需要循环,只需使用D3的数据绑定即可:
svg.selectAll("path")
.data(dataNest)
.enter()
.append("path")
.attr("class", "line")
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
.attr("d", function(d) { return priceline(d.values); });
You probably also want to redefine your tooltip function: 您可能还想重新定义工具提示功能:
.html(function(d) {
return "<b>" + d.key + "</b>";
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.