D3 Sankey Chart上的颜色特定链接

时间:2017-11-30 15:17:59

标签: javascript d3.js sankey-diagram

我们需要帮助着色此sankey图表的特定链接。我们希望与“Raleigh”连接的链接为绿色,所有其他链接保持灰色。

下面是一张它看起来像什么的图片,然后是绿色链接的图片:

Sankey Now Sankey Future

我们是d3的新手,只是无法弄明白,任何帮助都会很棒!!

var svg = d3.select("svg").attr("style", "outline: thin solid grey;"),
    width = +svg.attr("width"),
    height = +svg.attr("height");

var formatNumber = d3.format(",.0f"),
    format = function(d) { return formatNumber(d) + " TWh"; },
    color = d3.scaleOrdinal(d3.schemeCategory10);

var school = {"nodes": [




{"name":"Happiness Index"}, // 0
{"name":"Business Cost Index"}, // 1
{"name":"Home Price to Income"}, // 2
{"name":"Population Growth"}, // 3  
{"name":"3 Year GDP Growth"}, // 4
{"name":"Percent with Degree"}, // 5


{"name":"Austin"}, // 6
{"name":"Nashville"}, // 7
{"name":"Atlanta"}, // 8
{"name":"Raleigh"}, // 9
{"name":"Washington DC"}, // 10

],
"links":[


// From Happiness
    {"source":0,"target":6,"value":97},
    {"source":0,"target":9,"value":100},
    {"source":0,"target":10,"value":96},


// From Business Cost
    {"source":1,"target":9,"value":87},
    {"source":1,"target":8,"value":88},
    {"source":1,"target":7,"value":99},

// From PTI
    {"source":2,"target":8,"value":86},



// From Pop Growth
    {"source":3,"target":9,"value":87},
    {"source":3,"target":6,"value":94},

// From 3yrgdp
    {"source":4,"target":9,"value":100},
    {"source":4,"target":6,"value":88},
    {"source":4,"target":7,"value":96},

// From percent undergrad
    {"source":5,"target":9,"value":85},
    {"source":5,"target":10,"value":100},
]};

var sankey = d3.sankey()
    .nodeWidth(15)
    .nodePadding(10)
    .extent([[1, 1], [width - 1, height - 6]]);

var link = svg.append("g")
    .attr("class", "links")
    .attr("fill", "none")
    .attr("stroke", "#000")
    .attr("stroke-opacity", 0.2)
  .selectAll("path");

var node = svg.append("g")
    .attr("class", "nodes")
    .attr("font-family", "sans-serif")
    .attr("font-size", 15)
  .selectAll("g");

sankey(school);

link = link
    .data(school.links)
    .enter().append("path")
      .attr("d", d3.sankeyLinkHorizontal())
      .attr("stroke-width", function(d) { return Math.max(1, d.width); })


// link hover values
link.append("title")
      .text(function(d) { return d.source.name + " → " + d.target.name + "\n" + format(d.value); });

node = node
    .data(school.nodes)
    .enter().append("g");

node.append("rect")
      .attr("x", function(d) { return d.x0; })
      .attr("y", function(d) { return d.y0; })
      .attr("height", function(d) { return d.y1 - d.y0; })
      .attr("width", function(d) { return d.x1 - d.x0; })
      .attr("fill", function(d) { return color(d.name.replace(/ .*/, "")); })
      .attr("stroke", "#000");

node.append("text")
      .attr("x", function(d) { return d.x0 - 6; })
      .attr("y", function(d) { return (d.y1 + d.y0) / 2; })
      .attr("dy", "0.35em")
      .attr("text-anchor", "end")
      .text(function(d) { return d.name; })
    .filter(function(d) { return d.x0 < width / 2; })
      .attr("x", function(d) { return d.x1 + 6; })
      .attr("text-anchor", "start");

svg.append("text")
        .attr("x", 10)
        .attr("y", 30)
        .attr("class", "graphTitle")
        .text(" ");

svg.append("text")
        .attr("x", width - 80)
        .attr("y", height - 10)

1 个答案:

答案 0 :(得分:1)

将链接修改为:

link = link
    .data(school.links)
    .enter().append("path")
    .attr("d", d3.sankeyLinkHorizontal())
    .attr("stroke-width", function(d) { return Math.max(1, d.width); })
    .style("stroke", function(d){
      return d.target.name == "Raleigh" ? "green" : "gray";
    });

运行代码:

&#13;
&#13;
<!DOCTYPE html>
<html>

  <head>
    <script src="https://d3js.org/d3.v4.js"></script>
    <script src="https://unpkg.com/d3-sankey@0.7.1/build/d3-sankey.js"></script>
  </head>

  <body>
    <svg width="500" height="500"></svg>
    <script>
      var svg = d3.select("svg").attr("style", "outline: thin solid grey;"),
    width = +svg.attr("width"),
    height = +svg.attr("height");

var formatNumber = d3.format(",.0f"),
    format = function(d) { return formatNumber(d) + " TWh"; },
    color = d3.scaleOrdinal(d3.schemeCategory10);

var school = {"nodes": [




{"name":"Happiness Index"}, // 0
{"name":"Business Cost Index"}, // 1
{"name":"Home Price to Income"}, // 2
{"name":"Population Growth"}, // 3  
{"name":"3 Year GDP Growth"}, // 4
{"name":"Percent with Degree"}, // 5


{"name":"Austin"}, // 6
{"name":"Nashville"}, // 7
{"name":"Atlanta"}, // 8
{"name":"Raleigh"}, // 9
{"name":"Washington DC"}, // 10

],
"links":[


// From Happiness
    {"source":0,"target":6,"value":97},
    {"source":0,"target":9,"value":100},
    {"source":0,"target":10,"value":96},


// From Business Cost
    {"source":1,"target":9,"value":87},
    {"source":1,"target":8,"value":88},
    {"source":1,"target":7,"value":99},

// From PTI
    {"source":2,"target":8,"value":86},



// From Pop Growth
    {"source":3,"target":9,"value":87},
    {"source":3,"target":6,"value":94},

// From 3yrgdp
    {"source":4,"target":9,"value":100},
    {"source":4,"target":6,"value":88},
    {"source":4,"target":7,"value":96},

// From percent undergrad
    {"source":5,"target":9,"value":85},
    {"source":5,"target":10,"value":100},
]};

var sankey = d3.sankey()
    .nodeWidth(15)
    .nodePadding(10)
    .extent([[1, 1], [width - 1, height - 6]]);

var link = svg.append("g")
    .attr("class", "links")
    .attr("fill", "none")
    .attr("stroke", "#000")
    .attr("stroke-opacity", 0.2)
  .selectAll("path");

var node = svg.append("g")
    .attr("class", "nodes")
    .attr("font-family", "sans-serif")
    .attr("font-size", 15)
  .selectAll("g");

sankey(school);

link = link
    .data(school.links)
    .enter().append("path")
    .attr("d", d3.sankeyLinkHorizontal())
    .attr("stroke-width", function(d) { return Math.max(1, d.width); })
    .style("stroke", function(d){
      return d.target.name == "Raleigh" ? "green" : "gray";
    })


// link hover values
link.append("title")
      .text(function(d) { return d.source.name + " → " + d.target.name + "\n" + format(d.value); });

node = node
    .data(school.nodes)
    .enter().append("g");

node.append("rect")
      .attr("x", function(d) { return d.x0; })
      .attr("y", function(d) { return d.y0; })
      .attr("height", function(d) { return d.y1 - d.y0; })
      .attr("width", function(d) { return d.x1 - d.x0; })
      .attr("fill", function(d) { return color(d.name.replace(/ .*/, "")); })
      .attr("stroke", "#000");

node.append("text")
      .attr("x", function(d) { return d.x0 - 6; })
      .attr("y", function(d) { return (d.y1 + d.y0) / 2; })
      .attr("dy", "0.35em")
      .attr("text-anchor", "end")
      .text(function(d) { return d.name; })
    .filter(function(d) { return d.x0 < width / 2; })
      .attr("x", function(d) { return d.x1 + 6; })
      .attr("text-anchor", "start");

svg.append("text")
        .attr("x", 10)
        .attr("y", 30)
        .attr("class", "graphTitle")
        .text(" ");

svg.append("text")
        .attr("x", width - 80)
        .attr("y", height - 10)
    </script>
  </body>

</html>
&#13;
&#13;
&#13;