2016-12-07 75 views
0

在一個簡單的例子中,我製作了2個折線圖,它們都有子彈。現在,在懸停子彈中,他們顯示了涉及y軸頻率值的工具提示值。現在,我的問題是如何知道我徘徊的是哪個節點(藍色或紅色),所以我在它的工具提示中獲得了相應的y軸值(頻率或頻率2)。請幫助,因爲我不能在下面的功能標識相同:D3 - 使用AngularJS分離2個折線圖的工具提示值 - 使用AngularJS

功能的工具提示:

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

片段:

<html> 

<head> 

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

    <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> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3-tip/0.7.1/d3-tip.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", "frequency2":"8.08"}, 
        {"letter": "B","frequency": "7.80", "frequency2": "12.13"}, 
        {"letter": "C","frequency": "15.35", "frequency2":"6.12"}, 
      ]; 

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

      // ------------------ 2nd Iteration -----------------------// 

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

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

       //plotting lines 
       path 
        .attr("d", lineFunction(data)) 
        .attr("stroke", "red") 
        .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.frequency2); 
        }) 
        .attr('r', 6) 
        .style("fill", "red") 
        .on('mouseover', tip.show) 
        .on('mouseout', tip.hide); 

     }); 

    </script> 

</body> 

</html> 

結果:

enter image description here

enter image description here

正如你可以看到我只看到兩個相同的頻率值。我可以在下面的行改變代碼:

return "<strong>Frequency:</strong> <span style='color:red'>" + d.frequency + "</span>"; 

到頻率2,但隨後都將具有紅色圖表的頻率值。如何檢測我徘徊的節點(藍色或紅色)?另外,你可以看到只有x軸的y軸頻率值被髮送到函數,但是沒有信息關於哪個被徘徊!

enter image description here

+0

沒有人可以幫忙嗎? ( – Deadpool

回答

2

三種解決方案:

1)創建兩個提示。一個會顯示frequency數據,您可以將其show/hide函數添加到映射到frequency數據的數據。另一個將顯示frequency2數據,您可以將其show/hide函數添加到映射到frequency2數據的數據。

2)你可以包裝tip.show功能,通過它只能顯示你想要的數據:

// blue line 
g.selectAll('...') 
    .on('mouseover', function(d) { 
    tip.show({frequency: d.frequency}) 
    }) 

// red line 
g.selectAll('...') 
    .on('mouseover', function(d) { 
    tip.show({frequency: d.frequency2}) 
    }) 

3)它綁定到你的圖表前,拆分您的數據。然後,只有您懸停的圓的數據纔會傳遞給工具提示。

var blueData = data.map(function(d) { 
    return { 
    letter: d.letter 
    frequency: d.frequency 
    }; 
}) 

var redData = data.map(function(d) { 
    return { 
    letter: d.letter 
    frequency: d.frequency2 
    }; 
})