D3.js 正在尝试向圆中添加id
我在学校为一位教授做研究,我用d3编码。我正在创建一个具有工具提示的图形,您可以通过单击图例来切换该图形。但是,当我切换线条时,这些线条消失了,但我无法获得与该线条关联的工具提示。我知道我需要为这些圆圈添加一个id,但我不知道如何添加。我已经在下面包含了我的代码,以显示到目前为止我所拥有的。任何帮助都将不胜感激D3.js 正在尝试向圆中添加id,d3.js,D3.js,我在学校为一位教授做研究,我用d3编码。我正在创建一个具有工具提示的图形,您可以通过单击图例来切换该图形。但是,当我切换线条时,这些线条消失了,但我无法获得与该线条关联的工具提示。我知道我需要为这些圆圈添加一个id,但我不知道如何添加。我已经在下面包含了我的代码,以显示到目前为止我所拥有的。任何帮助都将不胜感激 <!DOCTYPE html> <meta charset="utf-8"> <style> /* set the CSS */ body { fo
<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */
body { font: 12px Arial;}
path {
stroke: white;
stroke-width: 2;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
}
div.tooltip {
position: absolute;
text-align: center;
width: 170px;
height: 500px;
padding: 1px;
font: 12px sans-serif;
background: lightsteelblue;
border: 0px;
border-radius: 5px;
pointer-events: none;
}
.legend {
font-size: 16px;
font-weight: bold;
text-anchor: middle;
}
</style>
<body>
<!-- load the d3.js library -->
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
// Set the dimensions of the canvas / graph
var margin = {top: 20, right: 150, bottom: 60, left: 80},
width = 1160 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// Parse the date / time
// Set the ranges
var x = d3.scaleTime().range([0, width-100]);
var formatxAxis=d3.format('.0f');
var y = d3.scaleLinear().range([height, 0]);
// Define the axe
var xAxis = d3.axisBottom().scale(x)
.tickFormat(formatxAxis).ticks(20);
var yAxis = d3.axisLeft().scale(y)
.ticks(5);
// Define the line
var valueline = d3.line()
.curve(d3.curveMonotoneX)
.x(function(d) { return x(d.year); })
.y(function(d) { return y(d.count); });
// Define the div for the tooltip
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
// Adds the svg canvas
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// Get the data
d3.json("satisfaction.json", function(error, data) {
data.forEach(function(d) {
d.year = +d.year;
d.count = +d.count;
});
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.year; }));
y.domain([0, d3.max(data, function(d) { return d.count; })]);
//nest the entries by symbol
var dataNest = d3.nest()
.key(function(d) {
return d.word;
})
.entries(data);
//define colors for the lines
var color = d3.scaleOrdinal(d3.schemeCategory10);
// spacing for the legend
legendSpace = width/dataNest.length;
var circleid = svg.selectAll("circle")
.data(dataNest)
.enter()
.append("g")
.attr("id", function(d){
return "circle" + d.key.replace(/\s+/g, '');
});
// Loop through each symbol / key
dataNest.forEach(function(d,i) {
svg.append("path")
.attr("class", "line")
.style("stroke", function() { // Add the colours dynamically
return d.color = color(d.key); })
.attr("id", 'tag'+d.key.replace(/\s+/g, '')) // assign ID
.attr("d", valueline(d.values));
// Add the Legend
svg.append("text")
.attr("x", width - margin.left + 50)
.attr("y", legendSpace/4 + i*(legendSpace/6))
.attr("class", "legend") // style the legend
.style("fill", function() { // Add the colours dynamically
return d.color = color(d.key); })
.on("click", function(){
// Determine if current line is visible
var active = d.active ? false : true,
newOpacity = active ? 0 : 1;
// Hide or show the elements based on the ID
d3.select("#tag"+d.key.replace(/\s+/g, ''))
.transition().duration(100)
.style("opacity", newOpacity);
// Update whether or not the elements are active
d.active = active;
// Hide or show the elements based on the ID
d3.select("circle" + d.key.replace(/\s+/g, ''))
.transition().duration(100)
.style("opacity", newOpacity);
})
.text(d.key);
});
// Add the scatterplot
svg.selectAll("dot")
.data(data)
.enter().append("circle")
.attr("r", 5)
.attr("cx", function(d) { return x(d.year); })
.attr("cy", function(d) { return y(d.count); })
.on("click", function(d) {
div.transition()
.duration(200)
.style("opacity", .9);
div .html(d.word + "<br/>" + d.count + "<br/>" + d.songs)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 25) + "px");
})
.on("dblclick", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
})
;
// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add the Y Axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
});
</script>
</body>
/*设置CSS*/
正文{font:12px Arial;}
路径{
笔画:白色;
笔画宽度:2;
填充:无;
}
.轴线路径,
.轴线{
填充:无;
笔画:灰色;
笔画宽度:1;
形状渲染:边缘清晰;
}
div.tooltip{
位置:绝对位置;
文本对齐:居中;
宽度:170px;
高度:500px;
填充:1px;
字体:12px无衬线;
背景:淡蓝色;
边界:0px;
边界半径:5px;
指针事件:无;
}
.传奇{
字体大小:16px;
字体大小:粗体;
文本锚定:中间;
}
//设置画布/图形的尺寸
var margin={顶部:20,右侧:150,底部:60,左侧:80},
宽度=1160-margin.left-margin.right,
高度=500-margin.top-margin.bottom;
//解析日期/时间
//设定范围
var x=d3.scaleTime().range([0,width-100]);
var formatxAxis=d3.format('.0f');
变量y=d3.scaleLinear().range([height,0]);
//定义斧头
var xAxis=d3.axisBottom().scale(x)
.tickFormat(formatxAxis).ticks(20);
var yAxis=d3.axisLeft().scale(y)
.蜱(5);
//界定界线
var valueline=d3.line()
.curve(d3.curveMonotoneX)
.x(函数(d){返回x(d.year);})
.y(函数(d){返回y(d.count);});
//定义工具提示的div
var div=d3.选择(“主体”).追加(“div”)
.attr(“类”、“工具提示”)
.样式(“不透明度”,0);
//添加svg画布
var svg=d3.选择(“主体”)
.append(“svg”)
.attr(“宽度”,宽度+边距。左侧+边距。右侧)
.attr(“高度”,高度+边距。顶部+边距。底部)
.附加(“g”)
.attr(“转换”,
“翻译(“+margin.left+”,“+margin.top+”);
//获取数据
d3.json(“successment.json”),函数(错误,数据){
data.forEach(函数(d){
d、 年份=+d.year;
d、 计数=+d.count;
});
//缩放数据的范围
x、 域(d3.范围(数据,函数(d){返回d.year;}));
y、 域([0,d3.max(数据,函数(d){返回d.count;})];
//按符号嵌套条目
var dataNest=d3.nest()
.键(功能(d){
返回d.word;
})
.条目(数据);
//定义线条的颜色
var color=d3.scaleOrdinal(d3.schemeCategory 10);
//图例的间距
legendSpace=宽度/dataNest.length;
var circleid=svg.selectAll(“圆”)
.数据(数据嵌套)
.输入()
.附加(“g”)
.attr(“id”,函数(d){
返回“圆圈”+d键。替换(/\s+/g');
});
//循环遍历每个符号/键
forEach(函数(d,i){
追加(“路径”)
.attr(“类”、“行”)
.style(“stroke”,function(){//动态添加颜色
返回d.color=color(d.key);})
.attr(“id”,“tag”+d.key.replace(/\s+/g',)//分配id
.attr(“d”,valueline(d.values));
//添加图例
svg.append(“文本”)
.attr(“x”,宽度-边距。左侧+50)
.attr(“y”,legendSpace/4+i*(legendSpace/6))
.attr(“类”、“图例”)//设置图例的样式
.style(“fill”,function(){//动态添加颜色
返回d.color=color(d.key);})
.on(“单击”,函数(){
//确定当前行是否可见
var active=d.active?错误:正确,
newOpacity=active?0:1;
//根据ID隐藏或显示元素
d3.选择(“#tag”+d.key.replace(/\s+//g,”))
.transition().持续时间(100)
.style(“不透明度”,newOpacity);
//更新元素是否处于活动状态
d、 主动=主动;
//根据ID隐藏或显示元素
d3.选择(“圆圈”+d.键。替换(/\s+/g,”))
.transition().持续时间(100)
.style(“不透明度”,newOpacity);
})
.文本(d.key);
});
//添加散点图
svg.selectAll(“点”)
.数据(数据)
.enter().append(“圆”)
.attr(“r”,5)
.attr(“cx”,函数(d){返回x(d.year);})
.attr(“cy”,函数(d){返回y(d.count);})
。在“单击”时,函数(d){
过渡部()
.持续时间(200)
.样式(“不透明度”,.9);
html(d.word+“
”+d.count+“
”+d.songs)
.style(“左”,“d3.event.pageX)+“px”)
.style(“top”,(d3.event.pageY-25)+“px”);
})
.on(“dblclick”,函数(d){
过渡部()
.持续时间(500)
.样式(“不透明度”,0);
})
;
//添加X轴
svg.append(“g”)
.attr(“类”、“x轴”)
.attr(“变换”、“平移(0)”、“高度+”)
.呼叫(xAxis);
//添加Y轴
svg.append(“g”)
.attr(“类”、“y轴”)
.呼叫(yAxis);
});
现在,在将圆添加到图形之前,您正在尝试设置您的id
。相反,在将圆附加到图形时,可以设置id
// Add the scatterplot
svg.selectAll("dot")
.data(data)
.enter().append("circle")
.attr("id", function(d){ return "circle" + d.key.replace(/\s+/g, ''); })
// ^---- add the id here when you're appending the circles
.attr("r", 5)
.attr("cx", function(d) { return x(d.year); })
.attr("cy", function(d) { return y(d.count); })
.on("click", function(d) {
div.transition()
.duration(200)
.style("opacity", .9);
div .html(d.word + "<br/>" + d.count + "<br/>" + d.songs)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 25) + "px");
})
.on("dblclick", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
})
;
//添加散点图
svg.selectAll(“点”)
.数据(数据)
.enter().append(“圆”)
.attr(“id”,函数(d){return“circle”+d.key.replace(/\s+/g,,);})
//^----在添加圆时在此处添加id
.attr(“r”,5)
.attr(“cx”,函数(d){返回x(d.year);})
.attr(“cy”,函数(d){返回y(d.count);})
。在“单击”时,函数(d){
过渡部()
杜先生