【问题标题】:Adding tooltip to line chart not showing向折线图添加工具提示未显示
【发布时间】:2017-04-17 02:30:59
【问题描述】:

我做了一个折线图,在节点上添加了项目符号。现在,我只想添加一些在悬停点时显示的工具提示。

似乎添加了文本,但我在悬停的节点上看不到任何值(必须是频率)。

片段:

<html>

<head>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.12/angular.min.js"></script> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"></script>

</head> 

<body ng-app="myApp" ng-controller="myCtrl"> 

    <svg></svg>

    <script>

        //module declaration 
        var app = angular.module('myApp',[]);

        //Controller declaration
        app.controller('myCtrl',function($scope){

            $scope.svgWidth = 800;//svg Width
            $scope.svgHeight = 500;//svg Height 

            //Data in proper format 
            var data = [
                  {"letter": "A","frequency": "5.01"},
                  {"letter": "B","frequency": "7.80"},
                  {"letter": "C","frequency": "15.35"},
                  {"letter": "D","frequency": "22.70"},
                  {"letter": "E","frequency": "34.25"},
                  {"letter": "F","frequency": "10.21"},
                  {"letter": "G","frequency": "7.68"},
            ];

                //removing prior svg elements ie clean up svg 
                d3.select('svg').selectAll("*").remove();

                //resetting svg height and width in current svg 
                d3.select("svg").attr("width", $scope.svgWidth).attr("height", $scope.svgHeight);

                //Setting up of our svg with proper calculations 
                var svg = d3.select("svg");
                var margin = {top: 20, right: 20, bottom: 30, left: 40};
                var width = svg.attr("width") - margin.left - margin.right;
                var height = svg.attr("height") - margin.top - margin.bottom;

                //Plotting our base area in svg in which chart will be shown 
                var g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");

                //X and Y scaling 
                var x = d3.scaleBand().rangeRound([0, width]).padding(0.4);
                var y = d3.scaleLinear().rangeRound([height, 0]);

                x.domain(data.map(function(d) { return d.letter; }));
                y.domain([0, d3.max(data, function(d) { return +d.frequency; })]);

                //Final Plotting 

                //for x axis 
                g.append("g")
                    .call(d3.axisBottom(x))
                    .attr("transform", "translate(0," + height + ")");

                //for y axis 
                g.append("g")
                    .call(d3.axisLeft(y))
                    .append("text").attr("transform", "rotate(-90)").attr("text-anchor", "end");

                //the line function for path 
                var lineFunction = d3.line()
                    .x(function(d) {return x(d.letter); })
                    .y(function(d) { return y(d.frequency); })
                    .curve(d3.curveCardinal);

                //defining the lines
                var path = g.append("path");

                //plotting lines
                path
                    .attr("d", lineFunction(data))
                    .attr("stroke", "#fc9027")
                    .attr("stroke-width", 2)
                    .attr("fill", "none");

                g.selectAll('.circles1')
                    .data(data)
                    .enter().append('circle')
                    .attr('cx', function(d) {
                    return x(d.letter);
                    })
                    .attr('cy', function(d) {
                    return y(d.frequency);
                    })
                    .attr('r', 6)
                    .style("fill", "#fc9027")
                    .on("mouseover", function(d,i) {
                        d3.select(this).append("text")
                        .text(d.frequency)
                        .attr("x", x(d.letter))
                        .attr("y", y(d.frequency))
                    }); 

        });

    </script> 

</body> 

</html> 

结果:

【问题讨论】:

    标签: javascript angularjs d3.js svg tooltip


    【解决方案1】:

    当你这样做时:

    d3.select(this).append("text")
    

    您将&lt;text&gt; 元素附加到&lt;circle&gt; 元素,这将工作。

    还有其他选择。最简单的方法是附加一个&lt;title&gt;

     d3.select(this).append("title")
    

    但是title 非常有限。或者,您可以使用 div 来显示工具提示(在此处查看我的答案:https://stackoverflow.com/a/35665974/5768908

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-08
      • 2014-11-11
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多