2016-12-02 11 views
0

次のプログラムで折れ線グラフを作成しましたが、ツールチップが機能していません。それはエラーを出す。私はツールチップを表示するのに役立ちます!D3 - 折れ線グラフエラーのツールヒント

SNIPPET:

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

    <style> 

    /* d3 tip */ 
    .d3-tip { 
     line-height: 1; font-weight: bold; padding: 12px; background: rgba(0, 0, 0, 0.8); color: #fff; border-radius: 2px;} 

    /* Creates a small triangle extender for the tooltip */ 
    .d3-tip:after {box-sizing: border-box; display: inline; font-size: 10px; width: 100%; line-height: 1; color: rgba(0, 0, 0, 0.8); content: "\25BC"; position: absolute; text-align: center;} 

    /* Style northward tooltips differently */ 
    .d3-tip.n:after {margin: -1px 0 0 0; top: 100%; left: 0;} 

    </style> 


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

       var tip = d3.tip() 
        .attr('class', 'd3-tip') 
        .offset([-10, 0]) 
        .html(function(d) { 
        return "<strong>Frequency:</strong> <span style='color:red'>" + d.frequency + "</span>"; 
        }); 

       svg.call(tip); 

       //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', tip.show) 
        .on('mouseout', tip.hide); 

     }); 

    </script> 

</body> 

</html> 

ERROR:

enter image description here

REFERENCE:

http://bl.ocks.org/Caged/6476579

+0

を、あなたの前の質問に私の答えを見ましたか? –

答えて

1

d3.tipはd3ライブラリの一部ではありません。あなたがそれを使用している場合、あなたはそれを参照する必要があります:

<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script> 

または:

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-tip/0.7.1/d3-tip.js"></script> 
+0

もう一度ありがとう - @ジェラルド。あなただけに戻ってきてくれるあなただけ! ;) – Deadpool

関連する問題