在d3中访问SVG路径长度

5

我制作了一个多行图表,其中包含一个过渡设置,使得线条和标签一个接一个地“绘制”。它可以工作,但我不知道如何单独访问每条线的长度。我拥有的代码仅通过.node()返回第一条路径的长度。这样做的结果是,所有路径都被赋予第一条路径的长度,从而给其他路径带来错误的起始点。以下是代码。非常感谢任何帮助。

var margin = {top: 20, right: 80, bottom: 30, left: 60},
    width = 600 - margin.left - margin.right,
    height = 400 - margin.top - margin.bottom;

var x = d3.scale.ordinal()
    .rangePoints([0, width], 1);

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

var color = d3.scale.category20();

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

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

var line = d3.svg.line()
    .interpolate("linear")
    .x(function(d) { return x(d.month); })
    .y(function(d) { return y(d.rainfall); });

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 + ")");

d3.csv("data.csv", function(error, data) {
    color.domain(d3.keys(data[0]).filter(function(key) { return key !== "Month"; }));

var countries = color.domain().map(function(country) {
    return {
        country: country,
        values: data.map(function(d) {
            return {month: d.Month, rainfall: +d[country]};
        })
    };
});

x.domain(data.map(function(d) { return d.Month; }));
y.domain([
    d3.min(countries, function(c) { return d3.min(c.values, function(v) { return v.rainfall - 7; }); }),
    d3.max(countries, function(c) { return d3.max(c.values, function(v) { return v.rainfall + 7; }); })
]);

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

svg.append("g")
    .attr("class", "y axis")
    .call(yAxis)
  .append("text")
    .attr("transform", "rotate(-90)")
    .attr("y", "1.1em")
    .style("text-anchor", "end")
    .text("Rainfall (mm)");

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

var path = country.append("path")
    .attr("id", function(d, i) { return "line" + i; })
    .attr("class", "line")
    .attr("d", function(d) { return line(d.values); })
    .style("stroke", function(d) { return color(d.country); })

var totalLength = path.node().getTotalLength();

path.attr("stroke-dasharray", totalLength + " " + totalLength)
    .attr("stroke-dashoffset", totalLength)
  .transition()
    .delay(function(d, i) { return i * 1000; })
    .duration(1000)
    .ease("linear")
    .attr("stroke-dashoffset", 0)
    .each("end", function() {
        labels.transition()
            .delay(function(d, i) { return i * 1000; })
            .style("opacity", 1);
        });

var labels = country.append("text")
    .datum(function(d) { return {country: d.country, value: d.values[d.values.length - 1]}; })
    .attr("class", "label")
    .attr("transform", function(d) { return "translate(" + x(d.value.month) + "," + y(d.value.rainfall) + ")"; })
    .attr("x", 3)
    .attr("dy", ".35em")
    .style("opacity", 0)
    .text(function(d) { return d.country; });

});

2个回答

10
你可以将路径的总长度作为另一个属性添加到数据中,然后使用它:
path.each(function(d) { d.totalLength = this.getTotalLength(); })
    .attr("stroke-dasharray", function(d) { return d.totalLength + " " + d.totalLength; })
    .attr("stroke-dashoffset", function(d) { return d.totalLength; })
// etc

非常准确。感谢您的所有帮助! - user3194692
嗨,Lars。这对于单路径对象如何工作?例如,http://jsfiddle.net/5e2HD/。 - geotheory
不确定您的意思 - 应该完全相同 http://jsfiddle.net/5e2HD/1/ - Lars Kotthoff

0

Lars Kotthoff的答案完美地解决了问题,但是,当您使用TypeScript编码时,由于数据类型的原因,您可能会遇到许多错误。为了避免这种情况,只需在需要的任何函数中调用this.getTotalLength(),它不仅适用于each(...)函数。或者将其分配给一个变量。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接