d3-tip doesn't work in a multiline chart

ぐ巨炮叔叔 提交于 2020-01-07 03:48:06

问题


I am just learning how to use d3-tip in a multiline chart. 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. The chart is displayed correctly, but when I check the console: "Uncaught TypeError: Cannot read property 'value' of undefined" pointing to this code line:

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


回答1:


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:

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>";
});


来源:https://stackoverflow.com/questions/34711934/d3-tip-doesnt-work-in-a-multiline-chart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!