gpt4 book ai didi

javascript - 从 d3 v3 到 v4 的代码迁移折线图

转载 作者:行者123 更新时间:2023-11-29 23:59:44 25 4
gpt4 key购买 nike

我是 d3 的新手,如果这很明显,我很抱歉,但我正在将折线图从 d3.js v3 迁移到 v4,但出现错误:

TypeError: Cannot read property '0' of undefined

我看过之前提出的问题,他们说可以使用 path[0][0] 而不是 path.node()

这是抛出错误的代码部分

var totalLength = [path[0][0].getTotalLength() , path[0].getTotalLength()] ;
console.log(totalLength);

这是完整的代码,但我认为问题出在我一开始选择的日期格式上:

var margin = {top: 20, right: 50, bottom: 100, left: 500},
width = 1000 //- margin.left - margin.right,
height = 400 - margin.top - margin.bottom;

var parseDate = d3.time.format("%Y-%m-%d").parse;
/* var parseDate = d3.time.format("%d-%b-%y").parse; */

var x = d3.time.scale()
.range([0, width]);

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

var color = d3.scale.category10();

var xAxis = d3.svg.axis()
.scale(x)
// .ticks(8)
.innerTickSize(2)
.outerTickSize(2)
.orient("bottom")
.tickFormat(d3.time.format("%b")); // tickFormat

var yAxis = d3.svg.axis()
.scale(y)
.tickFormat(function(d) { return d ;})
.ticks(5)
.innerTickSize(5)
.outerTickSize(2)
.orient("left");

var line = d3.svg.line()
.interpolate("basis")
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.accdnt); });


var svg = d3.select("#cell3").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 + ")");

d3.tsv("data.tsv", function(error, data) {
color.domain(d3.keys(data[2]).filter(function(key) { return key !== "date"; }));

data.forEach(function(d) {
d.date = parseDate(d.date);
});

var year = color.domain().map(function(name) {
return {
name: name,
values: data.map(function(d) {
return {date: d.date, accdnt: +d[name]};
})
};
});


x.domain(d3.extent(data, function(d) { return d.date; }));

y.domain([
0,d3.max(year, function(c) { return d3.max(c.values, function(v) { return v.accdnt; }); })
]);

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

svg.append("g")
.attr("class", "y axis")
.call(yAxis);


svg.append("line")
.attr(
{
"class":"horizontalGrid",
"x1" : 0,
"x2" : width,
"y1" : y(0),
"y2" : y(0),
"fill" : "none",
"shape-rendering" : "crispEdges",
"stroke" : "black",
"stroke-width" : "1px",
"stroke-dasharray": ("3, 3")
});

var company = svg.selectAll(".company")
.data(year)
.enter().append("g")
.attr("class", "company");



var path = svg.selectAll(".company").append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d.values); })
.style("stroke", function(d) { if (d.name == "2013")
{return "rgb(000,255,000)"}
if (d.name == "2014")
{return "rgb(045,255,251)"}
if (d.name == "2015")
{return "rgb(128,125,044)"}
else {return "#000";}
});




var totalLength = [path[0][0].getTotalLength(), path[0][1].getTotalLength()];

console.log(totalLength);


d3.select(path[0][0])
.attr("stroke-dasharray", totalLength[0] + " " + totalLength[0] )
.attr("stroke-dashoffset", totalLength[0])
.transition()
.duration(2500)
.ease("linear")
.attr("stroke-dashoffset", 0);

d3.select(path[0][1])
.attr("stroke-dasharray", totalLength[1] + " " + totalLength[1] )
.attr("stroke-dashoffset", totalLength[1])
.transition()
.duration(2500)
.ease("linear")
.attr("stroke-dashoffset", 0);

d3.select(path[0][2])
.attr("stroke-dasharray", totalLength[1] + " " + totalLength[1] )
.attr("stroke-dashoffset", totalLength[1])
.transition()
.duration(2500)
.ease("linear")
.attr("stroke-dashoffset", 0);

});

最佳答案

如果您尝试引用 D3 v4.x 的代码,几个 部分将引发错误,而不仅仅是您提到的那个。这些都是错误的:

  • d3.time.format
  • d3.time.scale()
  • d3.scale.linear()
  • d3.scale.category10()
  • d3.svg.axis()
  • d3.svg.line()
  • line.interpolate

仅解释您提到的部分(“这是抛出错误的代码部分”):

根据 D3 4.x API,

Selections no longer subclass Array using prototype chain injection; they are now plain objects, improving performance.

因此,在 D3 版本 4.x 中,选择是对象。这就是 path[0][0] 不再起作用的原因。

如果您 console.log 您的选择,您将看到类似这样的内容:

Selection {_groups: Array[1], _parents: Array[1]}

因此,您必须改用 path.nodes()

关于javascript - 从 d3 v3 到 v4 的代码迁移折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40897802/

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