D3-tip 在多折线图中不起作用



我正在学习如何在多行图表中使用d3提示。我举了这个例子练习:http://bl.ocks.org/d3noob/d8be922a10cb0b148cd5我已经在代码中添加了d3提示,但工具提示没有出现图表显示正确,但当我检查控制台时:指向此代码行的"未捕获类型错误:无法读取未定义的属性'value'":

.html(function(d) {
    return "<b>" + d.value + "</b>";
})

在这里我给你看脚本:

    <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>

我改变了很多事情,但我找不到办法。知道吗?提前谢谢。

您没有任何数据绑定到您调用工具提示的元素。您不需要循环,只需使用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); });

您可能还想重新定义工具提示功能:

.html(function(d) {
  return "<b>" + d.key + "</b>";
});

最新更新