gpt4 book ai didi

javascript - D3 错误 : NotFoundError: Node was not found

转载 作者:行者123 更新时间:2023-11-30 12:57:39 26 4
gpt4 key购买 nike

我正在尝试实现 D3 的动态功能,为此我遵循了 http://mbostock.github.io/d3/tutorial/bar-2.html 中给出的示例它工作正常,但是当我为 x 轴和 y 轴添加代码时,我从 redraw() 函数中收到“NotFoundError: Node was not found”错误。

没有轴绘制的代码,它工作正常,但从 redraw() 函数中得到“NotFoundError: Node was not found”错误。

让我知道问题是什么以及如何解决。 -- 谢谢

//Data set
var t = 17;

var data = [
{"time": 1, "value": 56, "color": "green"},
{"time": 2, "value": 53, "color": "green"},
{"time": 3, "value": 58, "color": "green"},
{"time": 4, "value": 58, "color": "green"},
{"time": 5, "value": 56, "color": "green"},
{"time": 6, "value": 53, "color": "green"},
{"time": 7, "value": 58, "color": "red"},
{"time": 8, "value": 58, "color": "red"},
{"time": 9, "value": 56, "color": "green"},
{"time": 10, "value": 53, "color": "green"},
{"time": 11, "value": 58, "color": "green"},
{"time": 12, "value": 58, "color": "green"},
{"time": 13, "value": 56, "color": "orange"},
{"time": 14, "value": 53, "color": "green"},
{"time": 15, "value": 58, "color": "orange"},
{"time": 16, "value": 58, "color": "green"}
];

var minval = 0,
maxval = 100,
sumval = 0,
sampsize = 30;

var max = 4, min = 0;

//var label_array = new Array();
var val_array = new Array(),
val_array_sum = new Array(),
data_array = new Array(),
val_array_stackedbar = new Array();

sampsize = data.length;

function next() {
return {
time: ++t,
value: 60,
color: "green"
};
}

setInterval(function() {

data.shift();
data.push(next());

redraw();

}, 1500);

var width = 300, height = 300, height2 = 5;
var margin = {
top : 30,
right : 10,
bottom : 40,
left : 60
}, width = width - margin.left - margin.right, height = height
- margin.top - margin.bottom;

// create the graph object
var vis = d3.select("#stackedbar-chart3")
.append("svg:svg")
.attr("class", "metrics-container3")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
// .append("g").attr(
// "transform",
// "translate(" + margin.left + ","
// + margin.top + ")");


var y = d3.scale.linear().domain([0, maxval]).range([height, 0]);

var x = d3.scale.linear().domain([0, sampsize+1]).range([0, width]);

var y2 = d3.scale.linear().domain([0, maxval]).range([0, height]);

var yAxis = d3.svg.axis().scale(y).orient("left")
.ticks(5);

var xAxis = d3.svg.axis().scale(x).orient("bottom")
.ticks(5);


// Add first data-series
var bars = vis.selectAll("rect")
.data(data)
.enter().append("svg:rect")
.attr("fill", function(d) { return d.color; } )
.attr("x", function(d, i) { return x(i+1); })
.attr("y", function(d, i) { return height - y2(d.value); } )
.attr("width", 5)
.attr("height", function(d, i) { return y2(d.value); });

现在添加轴

//            Add x-axis and y-axis
vis.append("g").attr("class", "axis").call(yAxis);

vis.append("g").attr("class", "axis").attr("transform",
"translate(0," + height + ")").call(xAxis);

// Add the axes labels
vis.append("text").attr("class", "axis-label").attr(
"text-anchor", "end").attr("x", 20).attr("y",
height + 34).text('Time');

重绘功能用于使图表动态化

    function redraw() {

var bars = vis.selectAll("rect")
.data(data, function(d) { return d.time; });

bars.enter().insert("rect", "line")
.attr("fill", function(d) { return d.color; } )
.attr("x", function(d, i) { return x(i+1); })
.attr("y", function(d, i) { return height - y2(d.value); } )
.attr("width", 5)
.attr("height", function(d, i) { return y2(d.value); })
.transition()
.duration(1000)
.attr("x", function(d, i) { return x(i) - .5; });

bars.transition()
.duration(1000)
.attr("x", function(d, i) { return x(i) - .5; });

bars.exit().transition()
.duration(1000)
.attr("x", function(d, i) { return x(i - 1) - .5; })
.remove();

}

最佳答案

您的代码无法正常工作,因为用于插入新柱的行已不存在。即代码

bars.enter().insert("rect", "line")

不工作,因为没有 line 元素。您可以通过简单地附加新条来解决这个问题。修改后的 jsfiddle here .

关于javascript - D3 错误 : NotFoundError: Node was not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18484501/

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