在一个简单的例子中,我制作了2个折线图,它们也都有子弹。现在,在悬停子弹上,它们显示工具提示的值,这些工具提示指的是y轴频率值。现在,我的问题是如何知道我徘徊了哪个节点(蓝色或红色),所以我在其工具提示上得到了相应的y轴值(频率或频率2)。请帮忙,因为我在以下功能中无法识别:
工具提示的功能:
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>";
});
SNIPPET:
<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>
结果:
如您所见,我在两者上只看到相同的频率值。我可以在以下行中更改代码:
return "<strong>Frequency:</strong> <span style='color:red'>" + d.frequency + "</span>";
到frequency2,但两者都有红色图表的频率值。如何检测我徘徊的节点(蓝色或红色)?
另外,你可以看到只有xlabel&amp; 2个y轴频率值被发送到功能,但没有关于哪个已经悬停的信息!
答案 0 :(得分: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
};
})