gpt4 book ai didi

javascript - 如何删除这些 D3.js 控制台日志错误

转载 作者:行者123 更新时间:2023-11-30 13:49:20 25 4
gpt4 key购买 nike

在尝试了一些其他解决方法(例如尝试针对类似问题的建议解决方案)后,我遇到了以下错误 - 没有成功。

Error: <rect> attribute height: Expected length, "NaN"

有人可以看一下我的代码并帮助我更改它以使这些错误消失吗?

可以找到我的数据样本here .

This是在从评论中获得帮助之前错误的样子。

This是当前错误的样子

下面是我的 js - 代码:

我没有使用当前最新版本的 D3.js,而是使用旧版本 4

我在 D3.js 方面还不是很擅长(仍在学习中)。

var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 30, left: 40},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");

// The scale spacing the groups:
var x0 = d3.scaleBand()
.rangeRound([0, width])
.paddingInner(0.1);

// The scale for spacing each group's bar:
var x1 = d3.scaleBand()
.padding(0.05);

var y = d3.scaleLinear()
.rangeRound([height, 0]);

var z = d3.scaleOrdinal()
.range(["#008000", "#8a89a6", "#7b6888", "#008080", "#ff0000", "#d0743c", "#ff8c00"]);


// trying to add tooltips
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);

d3.csv("data.csv", function(d, i, columns) {
for (var i = 1, n = columns.length; i < n; ++i) d[columns[i]] = +d[columns[i]];
return d;
}, function(error, data) {
if (error) throw error;

var keys = data.columns.slice(1);

x0.domain(data.map(function(d) { return d.years; }));
x1.domain(keys).rangeRound([0, x0.bandwidth()]);
y.domain([0, d3.max(data, function(d) { return d3.max(keys, function(key) { return d[key]; }); })]).nice();

g.append("g")
.selectAll("g")
.data(data)
.enter().append("g")
.attr("class","bar")
.attr("transform", function(d) { return "translate(" + x0(d.years) + ",0)"; })
.selectAll("rect")
.data(function(d) { return keys.map(function(key) { return {key: key, value: d[key]}; }); })
.enter().append("rect")
.attr("x", function(d) { return x1(d.key); })
.attr("y", function(d) { return y(d.value); })
.attr("width", x1.bandwidth())
.attr("height", function(d) { return height - y(d.value); })
.attr("fill", function(d) { return z(d.key); });

g.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x0));

g.append("g")
.attr("class", "y axis")
.call(d3.axisLeft(y).ticks(null, "s"))
.append("text")
.attr("x", 2)
.attr("y", y(y.ticks().pop()) + 0.5)
.attr("dy", "0.32em")
.attr("fill", "#000")
.attr("font-weight", "bold")
.attr("text-anchor", "start")
.text("ZAR");

var legend = g.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("text-anchor", "end")
.selectAll("g")
.data(keys.slice().reverse())
.enter().append("g")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });

legend.append("rect")
.attr("x", width - 17)
.attr("width", 15)
.attr("height", 15)
.attr("fill", z)
.attr("stroke", z)
.attr("stroke-width",2)
.on("click",function(d) { update(d) });

legend.append("text")
.attr("x", width - 24)
.attr("y", 4.5)
.attr("dy", "0.32em")
.text(function(d) { return d;});


var filtered = [];

////
//// Update and transition on click:
////

function update(d) {

//
// Update the array to filter the chart by:
//

// add the clicked key if not included:
if (filtered.indexOf(d) == -1) {
filtered.push(d);
// if all bars are un-checked, reset:
if(filtered.length == keys.length) filtered = [];
}
// otherwise remove it:
else {
filtered.splice(filtered.indexOf(d), 1);
}

//
// Update the scales for each group(/years)'s items:
//
var newKeys = [];
keys.forEach(function(d) {
if (filtered.indexOf(d) == -1 ) {
newKeys.push(d);
}
})
x1.domain(newKeys).rangeRound([0, x0.bandwidth()]);
y.domain([0, d3.max(data, function(d) { return d3.max(keys, function(key) { if (filtered.indexOf(key) == -1) return d[key]; }); })]).nice();

// update the y axis:
svg.select(".y")
.transition()
.call(d3.axisLeft(y).ticks(null, "s"))
.duration(500);


//
// Filter out the bands that need to be hidden:
//
var bars = svg.selectAll(".bar").selectAll("rect")
.data(function(d) { return keys.map(function(key) { return {key: key, value: d[key]}; }); })

bars.filter(function(d) {
return filtered.indexOf(d.key) > -1;
})
.transition()
.attr("x", function(d) {
return (+d3.select(this).attr("x")) + (+d3.select(this).attr("width"))/2;
})
.attr("height",0)
.attr("width",0)
.attr("y", function(d) { return height; })
.duration(500);

//


// Adjust the remaining bars:
//
bars.filter(function(d) {
return filtered.indexOf(d.key) == -1;
})
.transition()
.attr("x", function(d) { return x1(d.key); })
.attr("y", function(d) { return y(d.value); })
.attr("height", function(d) { return height - y(d.value); })
.attr("width", x1.bandwidth())
.attr("fill", function(d) { return z(d.key); })
.duration(500);


// update legend:
legend.selectAll("rect")
.transition()
.attr("fill",function(d) {
if (filtered.length) {
if (filtered.indexOf(d) == -1) {
return z(d);
}
else {
return "white";
}
}
else {
return z(d);
}
})
.duration(100);

legend.selectAll("bar")
.text(function(d, i) { return label[i]; });


}

});

</script>

最佳答案

此问题的根本原因是数据文件末尾有额外的行。当 d3.csv 方法解析此文件时,它将生成一个包含额外元素的数据数组,其中每个值为 NaN。数据文件实际上只有7个数据:

enter image description here

由于 OP 不想更改 CSV 文件以删除这些行,我们可以通过在解析后删除 d3.csv 回调中的错误行来解决错误数据:

function(error, data) {
if (error) throw error;
// copy the columns since filter will remove this property
let c = data.columns;
// filter the data to remove the rows without a year
data = data.filter(function(d){
return d.years != "";
})
// add the columns property back in
data.columns = c;

Here's the code running correctly .

关于javascript - 如何删除这些 D3.js 控制台日志错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58584690/

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