Javascript D3-两个折线图的单独工具提示值-使用AngularJS
在一个简单的例子中,我制作了两个折线图,它们都有项目符号。现在,在悬停的项目符号上,它们将显示工具提示的值,这些值引用y轴频率值。现在,我的问题是如何知道我悬停了哪个节点(蓝色或红色),因此我在其工具提示上获得了相应的y轴值(频率或频率2)。请帮助,因为我无法在以下功能中识别相同的内容: 工具提示功能:Javascript D3-两个折线图的单独工具提示值-使用AngularJS,javascript,angularjs,function,d3.js,svg,Javascript,Angularjs,Function,D3.js,Svg,在一个简单的例子中,我制作了两个折线图,它们都有项目符号。现在,在悬停的项目符号上,它们将显示工具提示的值,这些值引用y轴频率值。现在,我的问题是如何知道我悬停了哪个节点(蓝色或红色),因此我在其工具提示上获得了相应的y轴值(频率或频率2)。请帮助,因为我无法在以下功能中识别相同的内容: 工具提示功能: var tip = d3.tip() .attr('class', 'd3-tip') .offset([-10, 0]) .html(function(d) { retur
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>
var tip=d3.tip()
.attr('class','d3 tip')
.偏移量([-10,0])
.html(函数(d){
返回“频率:”+d.Frequency+”;
});
片段:
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>
/*d3提示*/
.d3提示{
线条高度:1;字体大小:粗体;填充:12px;背景:rgba(0,0,0,0.8);颜色:#fff;边框半径:2px;}
/*为工具提示创建一个小三角形延长线*/
.d3提示:{框大小:边框框;显示:内联;字体大小:10px;宽度:100%;线条高度:1;颜色:rgba(0,0,0,0.8);内容:“\25BC”;位置:绝对;文本对齐:中心;}
/*以不同方式设置向北工具提示的样式*/
.d3 tip.n:在{margin:-1px0;top:100%;left:0;}
//模块声明
var-app=angular.module('myApp',[]);
//控制器声明
应用程序控制器('myCtrl',函数($scope){
$scope.svgWidth=800;//svg宽度
$scope.svgHeight=500;//svg高度
//正确格式的数据
风险值数据=[
{“字母”:“A”,“频率”:“5.01”,“频率2”:“8.08”},
{“字母”:“B”,“频率”:“7.80”,“频率2”:“12.13”},
{“字母”:“C”,“频率”:“15.35”,“频率2”:“6.12”},
];
//删除以前的svg元素,即清理svg
d3.select('svg')。selectAll(“*”).remove();
//在当前svg中重置svg高度和宽度
d3.选择(“svg”).attr(“宽度”、$scope.svgWidth).attr(“高度”、$scope.svgHeight);
//通过正确的计算设置svg
var svg=d3.选择(“svg”);
var-margin={顶部:20,右侧:20,底部:30,左侧: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')
.偏移量([-10,0])
.html(函数(d){
返回“频率:”+d.Frequency+”;
});
svg.call(tip);
//在svg中绘制我们的基本区域,其中将显示图表
var g=svg.append(“g”).attr(“transform”、“translate”(“+margin.left+”,“+margin.top+”));
//X和Y缩放
var x=d3.scaleBand().rangeRound([0,宽度]).padding(0.4);
变量y=d3.scaleLinear().rangeRound([height,0]);
x、 域(data.map(函数(d){返回d.letter;}));
y、 域([0,d3.max(数据,函数(d){return+d.frequency;})]);
//最终绘图
//对于x轴
g、 附加(“g”)
.call(d3.axisBottom(x))
.attr(“变换”、“平移(0)”、“高度+”);
//对于y轴
g、 附加(“g”)
.呼叫(d3.左(y))
.append(“text”).attr(“变换”、“旋转(-90)”).attr(“文本锚定”、“结束”);
//路径的线函数
var lineFunction=d3.line()
.x(函数(d){返回x(d字母);})
.y(函数(d){返回y(d.频率);})
.曲线(d3.曲线中心);
//界定界线
var path=g.append(“路径”);
//绘制线
路径
.attr(“d”,线函数(数据))
.attr(“笔划”、“蓝色”)
.attr(“笔划宽度”,2)
.attr(“填充”、“无”);
g、 选择全部(“.circles1”)
.数据(数据)
.enter().append('圆')
.attr('cx',函数(d){
返回x(d.letter);
})
.attr('cy',函数(d){
返回y(d.频率);
})
.attr('r',6)
.style(“填充”、“蓝色”)
.on('mouseover',tip.show)
.on('mouseout',tip.hide);
//----------------第二次迭代-----------------//
//路径的线函数
var lineFunction=d3.line()
.x(函数(d){返回x(d字母);})
.y(函数(d){返回y(d.frequency2);})
.曲线(d3.曲线中心);
//界定界线
var path=g.append(“路径”);
//绘制线
路径
.attr(“d”,线函数(数据))
.attr(“笔划”、“红色”)
.attr(“笔划宽度”,2)
.attr(“填充”、“无”);
g、 选择全部(“.circles1”)
.数据(数据)
.enter().append('圆')
.attr('cx',函数(d){
返回x(d.letter);
})
.attr('cy',函数(d){
返回y(d.频率2);
})
.attr('r',6)
.样式(“填充”、“红色”)
.on('mouseover',tip.show)
.on('mouseout',tip.hide);
});
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
};
})