首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在D3.js树中在中心和不同节点之间绘制不同颜色的“笔画”

在D3.js树中在中心和不同节点之间绘制不同颜色的“笔画”
EN

Stack Overflow用户
提问于 2014-01-19 16:44:33
回答 2查看 6.9K关注 0票数 3

这是我的密码。我正在试图连接/绘制中心和不同节点之间的路径。现在,我想为不同的路径做一个不同颜色的stroke。我已经创建了一个颜色数组,对于哪个路径,什么将是笔画颜色。但是我不能用不同的颜色制作stroke

代码语言:javascript
运行
复制
var lineFunction = d3.svg.line()
                     .x(function(d) { return d.x;})
                     .y(function(d) { return d.y;})
                     .interpolate("linear");
var lineGraph = vis.append("g")
                  .append("path")
                  .attr("d", lineFunction(CenterList))
                  .attr("stroke", function(d, i) { return ColorArr[i]; } )
                  .attr("stroke-width", 5)
                  .attr("fill", "none")
                  .attr("id", "viz");

ColorArr=black, black, red, red, black, black, black, red, black, red;
EN

Stack Overflow用户

回答已采纳

发布于 2014-01-19 18:18:19

要使不同的线段具有不同的样式,它们必须是单独的行元素,而不是一条路径的所有部分。

但是,将线条图绘制为一系列行元素会使代码复杂化。对于每一个点,您不仅需要知道该点的数据,还需要了解前一点或下一个点(行的开始或结束)的数据。

我已经写了一个(更复杂的)例子,用前一个这样的问题的方式绘制一个线条图,所以我能够快速地为您修改它。

这是小提琴:http://fiddle.jshell.net/4xZwb/3/

对于这个例子,我使用了一个“每个”调用来对所有计算进行分组:

代码语言:javascript
运行
复制
var lines = svg.append("g").attr("class", "plot").selectAll("line");
        //define a d3 selection consisting of <line> elements
        //that are grouped within g.plot

    lines = lines.data(data);  
        //tell the selection to make room for every point in the data array

    lines.enter().append("line");
        //add one <line> element for every data point

    lines.each(function(d,i){
        //for each line in the selection, this function will be called
        //with d equal to the data point and i equal to the index
        //and "this" equal to the <line> element

        var value = d;
        //y-value for current point 
        //(we're just using the raw number from the array,
        //normally it would be something like d.y or d[1] )

        //find next point
        var next = i+1; //x-position (just using index #)
        var nextValue = data[i+1]; //y-position

        if (isNaN(nextValue)){
            // there is no next value,
            // so repeat this point as the end of line
            //(line will have zero length, but the marker will show up)
            next = i;
            nextValue = value;
        }

        d3.select(this) //select this particular <line> element
                        //so that we can use d3 methods

            //set coordinates:
            .attr( 
                {x1: xScale(i),
                 y1: yScale(value),
                 x2: xScale(next),
                 y2: yScale(nextValue)}
                )

            //Set styles for this individual line segment
            //e.g., based on whether value is increasing
            //or decreasing, we can set stroke colour red or black
            .style("stroke", ( (value > nextValue) ?
                              "red" :
                              "black" )
                  );
    });//end of "each" call

但是,您也可以单独设置它们,如在此版本中:

代码语言:javascript
运行
复制
lines = lines.data(data);

lines.enter().append("line");

lines.attr("x1", function(d,i){return xScale(i);})
.attr("x2", function(d,i){
    return xScale( (i+1 == data.length)?i:i+1);
})
.attr("y1", function(d,i){return yScale(d);})
.attr("y2", function(d,i){
    return yScale( (i+1 == data.length)?
                  d:data[i+1]);
})
.style("stroke", function(d,i){
    return ( (d > data[i+1]) ?
             "red" : "black" )
    });

http://fiddle.jshell.net/4xZwb/4/

票数 5
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21219557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档