gpt4 book ai didi

javascript - 图例切换中的 d3 错误 -- 未捕获 DOMException : Failed to execute 'querySelector' on 'Document' :

转载 作者:行者123 更新时间:2023-11-28 04:39:47 25 4
gpt4 key购买 nike

我正在尝试为我的折线图启用图例切换,但在尝试切换“通用汽车卡车公司”时收到 d3 错误

错误:“未捕获的 DOMException:无法在“文档”上执行“querySelector”:“#line_General Motors Truck Company (GMC)”不是有效的选择器。”

我认为这是因为图例中的“()”字符,但不确定。有人知道我该如何解决这个问题吗?

这是一个jsfiddle: https://jsfiddle.net/jw8dskvu/

还有代码片段:

var data = [{
"Brand": "Toyota",
"Count": 1800,
"Time": "2017-04-02 16"
}, {
"Brand": "Toyota",
"Count": 1172,
"Time": "2017-04-02 17"
}, {
"Brand": "Toyota",
"Count": 2000,
"Time": "2017-04-02 18"
}, {
"Brand": "General Motors Truck Company (GMC)",
"Count": 8765,
"Time": "2017-04-02 16"
}, {
"Brand": "General Motors Truck Company (GMC)",
"Count": 3445,
"Time": "2017-04-02 17"
}, {
"Brand": "General Motors Truck Company (GMC)",
"Count": 1232,
"Time": "2017-04-02 18"
}];

data.forEach(function(d) {
d.Time = d3.timeParse("%Y-%m-%d %H")(d.Time)
});

var dataGroup = d3.nest() //d3 method that groups data by Brand
.key(function(d) {
return d.Brand;
})
.entries(data);

//var color = d3.scale.category10();
var vis = d3.select("#visualisation"),
WIDTH = 1000,
HEIGHT = 500,
MARGINS = {
top: 50,
right: 20,
bottom: 50,
left: 50
},
xScale = d3.scaleTime().range([MARGINS.left, WIDTH - MARGINS.right]).domain([d3.min(data, function(d) { //set up x-axis based on data
return d.Time;
}), d3.max(data, function(d) {
return d.Time;
})]),

yScale = d3.scaleLinear().range([HEIGHT - MARGINS.top, MARGINS.bottom]).domain([d3.min(data, function(d) { //set up y-axis based on data
return d.Count;
}), d3.max(data, function(d) {
return d.Count;
})]),

xAxis = d3.axisBottom()
.scale(xScale),
yAxis = d3.axisLeft()
.scale(yScale)

vis.append("svg:g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (HEIGHT - MARGINS.bottom) + ")")
.call(xAxis);

vis.append("svg:g")
.attr("class", "y axis")
.attr("transform", "translate(" + (MARGINS.left) + ",0)")
.call(yAxis);

var lineGen = d3.line()
.x(function(d) {
return xScale(d.Time);
})
.y(function(d) {
return yScale(d.Count);
})
.curve(d3.curveBasis);

dataGroup.forEach(function(d, i) { //iterate over the dataGroup and create line graph for each brand
vis.append('svg:path')
.attr('d', lineGen(d.values))
.attr('stroke', function(d, j) {
return "hsl(" + Math.random() * 360 + ",100%,50%)"; //random color for each brand line on graph
})
.attr('stroke-width', 2)
.attr('id', 'line_' + d.key)
.attr('fill', 'none');

lSpace = WIDTH / dataGroup.length; //define the legend space based on number of brands
vis.append("text")
.attr("x", (lSpace / 2) + i * lSpace)
.attr("y", HEIGHT)
.style("fill", "black")
.attr("class", "legend")
.on('click', function() {
var active = d.active ? false : true;
var opacity = active ? 0 : 1;
d3.select("#line_" + d.key).style("opacity", opacity);
d.active = active;
})
.text(d.key);
});
   .axis path {
fill: none;
stroke: #777;
shape-rendering: crispEdges;
}

.axis text {
font-family: Lato;
font-size: 13px;
}

.legend {
font-size: 14px;
font-weight: bold;
cursor: pointer;
<title>D3 Test</title>
<script src="https://d3js.org/d3.v4.js"></script>

<body>
<svg id="visualisation" width="1000" height="600"></svg>
<script src="InitChart.js"></script>
</body>

最佳答案

I think it is because of the "()" character in the legend, not sure though.

是的,确实如此,但不仅如此:您也不应该在 CSS 选择器中使用空格。

因此,一个简单的解决方案是在设置 ID 时删除它们:

.attr('id', 'line_' + d.key.replace(/[ )(]/g,''))

当然,在选择时:

d3.select("#line_" + d.key.replace(/[ )(]/g,''))

这是您更新的代码:

var data = [{
"Brand": "Toyota",
"Count": 1800,
"Time": "2017-04-02 16"
}, {
"Brand": "Toyota",
"Count": 1172,
"Time": "2017-04-02 17"
}, {
"Brand": "Toyota",
"Count": 2000,
"Time": "2017-04-02 18"
}, {
"Brand": "General Motors Truck Company (GMC)",
"Count": 8765,
"Time": "2017-04-02 16"
}, {
"Brand": "General Motors Truck Company (GMC)",
"Count": 3445,
"Time": "2017-04-02 17"
}, {
"Brand": "General Motors Truck Company (GMC)",
"Count": 1232,
"Time": "2017-04-02 18"
}];

data.forEach(function(d) {
d.Time = d3.timeParse("%Y-%m-%d %H")(d.Time)
});

var dataGroup = d3.nest() //d3 method that groups data by Brand
.key(function(d) {
return d.Brand;
})
.entries(data);

//var color = d3.scale.category10();
var vis = d3.select("#visualisation"),
WIDTH = 1000,
HEIGHT = 500,
MARGINS = {
top: 50,
right: 20,
bottom: 50,
left: 50
},
xScale = d3.scaleTime().range([MARGINS.left, WIDTH - MARGINS.right]).domain([d3.min(data, function(d) { //set up x-axis based on data
return d.Time;
}), d3.max(data, function(d) {
return d.Time;
})]),

yScale = d3.scaleLinear().range([HEIGHT - MARGINS.top, MARGINS.bottom]).domain([d3.min(data, function(d) { //set up y-axis based on data
return d.Count;
}), d3.max(data, function(d) {
return d.Count;
})]),

xAxis = d3.axisBottom()
.scale(xScale),
yAxis = d3.axisLeft()
.scale(yScale)

vis.append("svg:g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (HEIGHT - MARGINS.bottom) + ")")
.call(xAxis);

vis.append("svg:g")
.attr("class", "y axis")
.attr("transform", "translate(" + (MARGINS.left) + ",0)")
.call(yAxis);

var lineGen = d3.line()
.x(function(d) {
return xScale(d.Time);
})
.y(function(d) {
return yScale(d.Count);
})
.curve(d3.curveBasis);

dataGroup.forEach(function(d, i) { //iterate over the dataGroup and create line graph for each brand
vis.append('svg:path')
.attr('d', lineGen(d.values))
.attr('stroke', function(d, j) {
return "hsl(" + Math.random() * 360 + ",100%,50%)"; //random color for each brand line on graph
})
.attr('stroke-width', 2)
.attr('id', 'line_' + d.key.replace(/[ )(]/g,''))
.attr('fill', 'none');

lSpace = WIDTH / dataGroup.length; //define the legend space based on number of brands
vis.append("text")
.attr("x", (lSpace / 2) + i * lSpace)
.attr("y", HEIGHT)
.style("fill", "black")
.attr("class", "legend")
.on('click', function() {
var active = d.active ? false : true;
var opacity = active ? 0 : 1;
d3.select("#line_" + d.key.replace(/[ )(]/g,'')).style("opacity", opacity);
d.active = active;
})
.text(d.key);
});
   .axis path {
fill: none;
stroke: #777;
shape-rendering: crispEdges;
}

.axis text {
font-family: Lato;
font-size: 13px;
}

.legend {
font-size: 14px;
font-weight: bold;
cursor: pointer;
<title>D3 Test</title>
<script src="https://d3js.org/d3.v4.js"></script>

<body>
<svg id="visualisation" width="1000" height="600"></svg>
<script src="InitChart.js"></script>
</body>

关于javascript - 图例切换中的 d3 错误 -- 未捕获 DOMException : Failed to execute 'querySelector' on 'Document' :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43881927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com