score:1
Accepted answer
You should be able to just add a stroke color with a function when you define the links:
link = link
.data(school.links)
.enter().append("path")
.attr("d", d3.sankeyLinkHorizontal())
.attr("stroke-width", function(d) { return Math.max(1, d.width); })
.attr("stroke", d => d.value > 30 ? 'red': 'gray')
Here's a working snippet:
var width = 500,
height =300
var xscale = d3.scaleLinear()
.range([0, width])
.domain([0, 100])
var yscale = d3.scaleLinear()
.range([height, 0])
.domain([0, 6])
var data = [
{student_percentile: 90, rank:1},
{student_percentile: 40, rank:3},
{student_percentile: 30, rank:4},
{student_percentile: 20, rank:5}
]
var svg = d3.select("#chart")
.append("svg")
.attr("style", "outline: thin solid grey;")
.attr("width", width)
.attr("height", height)
.append("g")
/*
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":"High School"}, // 0
{"name":"Community College"}, // 1
{"name":"Finance"}, // 2
{"name":"Accounting"}, // 3
{"name":"ITS"}, // 4
{"name":"Marketing"}, // 5
{"name":"Analytics"}, // 6
{"name":"Security"}, // 7
{"name":"Consulting"}, // 8
{"name":"Banking"}, // 9
{"name":"Internal"}, // 10
{"name":"Securities"}, // 11
{"name":"Public"}, // 12
{"name":"Audting"}, // 13
{"name":"Internal"}, // 14
{"name":"Retail"}, // 15
{"name":"Technology"}, // 16
{"name":"Strategy"} // 17
],
"links":[
// From HS
{"source":0,"target":2,"value":33},
{"source":0,"target":3,"value":42},
{"source":0,"target":4,"value":74},
{"source":0,"target":5,"value":60},
// From Community College
{"source":1,"target":2,"value":7},
{"source":1,"target":3,"value":13},
{"source":1,"target":4,"value":11},
{"source":1,"target":5,"value":9},
// From Finance
{"source":2,"target":9,"value":16},
{"source":2,"target":10,"value":14},
{"source":2,"target":11,"value":10},
// From Accounting
{"source":3,"target":12,"value":20},
{"source":3,"target":13,"value":12},
{"source":3,"target":7,"value":8},
{"source":3,"target":14,"value":15},
// From Marketing
{"source":5,"target":6,"value":30},
{"source":5,"target":15,"value":39},
// From ITS
{"source":4,"target":8,"value":19},
{"source":4,"target":6,"value":40},
{"source":4,"target":7,"value":20},
{"source":4,"target":12,"value":6},
// From ITS Consulting to Tech and Strat
{"source":8,"target":16,"value":10},
{"source":8,"target":17,"value":9},
]};
var sankey = d3.sankey()
.nodeWidth(15)
.nodePadding(10)
.extent([[1, 1], [width - 1, height - 6]])
.nodeAlign(d3.sankeyLeft);
var link = svg.append("g")
.attr("class", "links")
.attr("fill", "none")
//.attr("stroke", "#000")
.attr("stroke", function(d) {
if (d3.value > 30)
{return "red"}
else {return "#000"};
})
.attr("stroke-opacity", 0.2)
.selectAll("path");
var node = svg.append("g")
.attr("class", "nodes")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.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); })
.attr("stroke", d => d.value > 30 ? 'red': '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("STUDENT CHOICES");
svg.append("text")
.attr("x", width - 80)
.attr("y", height - 10)
.attr("class", "footnote")
.text("data is fictitious");
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://unpkg.com/d3-sankey@0"></script>
<div id="chart"></div>
Source: stackoverflow.com
Related Query
- Change Color of Links for Sankey Diagram in D3 / Javascript
- Color links same as origin nodes for a d3 sankey plot
- Change node color in D3 sankey diagram
- Gradient along links in D3 Sankey diagram
- Own colour range for Sankey Diagram with networkD3 package in R
- Change node colors in d3 Sankey Diagram based on additional column in data
- How to change alignment of nodes in a Sankey diagram using D3?
- Color Specific Links on D3 Sankey Chart
- How to select and deselect a svg line and change its color on selection and deselection using d3.js and javascript , jquery?
- How to fix unwanted circle on / break down of SVG path element for Sankey links with d3?
- d3 javascript clicking a line to change line color - changes all lines
- D3.js / Javascript Node-Lines pop-up window and color change
- Javascript find a word in text and change color dynamically
- dc.js Change default color for undefined data in choropleth map
- How to convert table data to nodes and links for d3js Sankey
- How to add a second value to the links of a Sankey diagram in d3.jS :
- Making a Sankey diagram in R for printing
- Brighter node color in D3 sankey diagram
- Change the color of the legend text in forceNetwork for networkD3
- Sankey Diagram (D3) - How to use multiple units for link values and how to add notes to mouseover popup box?
- how to give different color for the lines in the Sankey plot to show different groups?
- Change rectangle color using if else for a Power BI Custom Visual
- NetworkD3 Sankey diagram in R: How to calculate value for each link?
- How to change color only for data grid or x and y axis in chart in D3.js
- d3.js sankey diagram - change particle density and shape
- Add text within links in d3.js sankey diagram
- Converting an array to an object of nested objects for a tree diagram in Javascript
- Change single chord color in chord diagram using D3.js
- How to set text color for my d3 chart title?
- Sankey diagram transition
More Query from same tag
- Magnifier in d3
- How do I get an even spread between my bubbles?
- Responsive Parallel Coordinates chart in D3js
- geoChoroplethChart map with different fill opacity for the selected item
- change the color of chart d3
- D3.js not showing labels on first SVG after second SVG created
- How do I create this shape in SVG?
- d3 draw once on mouseover
- Building d3.js on Windows (Cygwin) - good workaround for 'npm install' path issue?
- force directed graph change color of all connected node on mouseover
- How can I position text inside bubble chart?
- D3 transform scale and translate
- D3 circular chart scale
- Can't get JSON data from file from local domain URL (Django static file)
- Merging arrays of objects from different CSVs
- Reading DOT files in javascript/d3
- AngularJS directive not updating D3 circle pack chart
- How to programmatically zoom to Elements in D3 in responsive map?
- multipart post request with d3.json()/d3.xhr()
- d3js: data/column required for time series chart with dropdown list
- Change a color of a link using JSON
- change range for date in d3 graph
- D3.js dynamically scaled and position image in node
- Brush selection in Box plot(d3.js)
- Animating circles getting clipped using D3.js with Ruby on Rails
- d3.js scaling and translating a force layout to the center of a viewport
- d3 calendar view: how to put all into one svg instead of several svgs
- Inverting bars on d3.js bar chart
- Errors Using `d3.legend`: Uncaught TypeError: node.getAttribute is not a function
- d3 bring elements to front by class from selector