D3.js V5 tree in svg layout - d3.js

I create three svg paint board in body element, and i want create a tree layout in first svg. But the tree layout always show at last one. I can't find some error in my codes. 3Q
result in html:
html result
my d3.js(v5) code shown below:
var svg = body.append('svg')
.attr('width', width)
.attr('height', height)
svg.append('rect')
.attr('width', '100%')
.attr('height', '100%')
.attr('fill', 'rgb(255, 100, 100)')
d3.json('map_tree.json').then(function(datas){
var root = d3.hierarchy(datas)
var tree = d3.tree()
.size([height, width-120])
.separation(function separation(a, b){
return a.parent == b.parent ? 1:2
})
tree(root)
svg.append('g')
.selectAll('circle')
.data(root.descendants())
.enter()
.append('circle')
.attr('r', 4)
.attr('transform', 'translate(50, 0)')
.attr('fill', 'white')
.attr('stroke', 'blue')
.attr('stroke-width', 1)
.attr('cx', function(d, i){
return d.y
})
.attr('cy', function(d, i){
return d.x
})
.attr('cx', function(d, i){
return d.y
})
.attr('cy', function(d, i){
return d.x
})
var svg = body.append('svg')
.attr('width', width)
.attr('height', height)
svg.append('rect')
.attr('width', '100%')
.attr('height', '100%')
.attr('fill', 'rgb(100, 255, 100)')
var svg = body.append('svg')
.attr('width', width)
.attr('height', height)
svg.append('rect')
.attr('width', '100%')
.attr('height', '100%')
.attr('fill', 'rgb(100, 100, 255)')

I hava found the reason. I created more than one d3.select('doby').append('svg') named svg, and the problem has gone when i named others with svg1、svg2

Related

D3 updates every path only when select() is running instead of selectAll()

I'm trying to update chord diagram according to the changes in data.
I created groups for each element and updated the data binding.
However, for some reason, it updates the data accordingly, only when I go with 'select' instead of 'selectAll' which feels quite odd for me
because everytime I update data binding I have used selectAlll only to update every element related.
My code is as below.
-Creating initial diagram-
var g = svg.selectAll('g.groups')
.data(figureCalculation.groups)
.join('g')
.attr('class', (d, i) => { return `group ${nameArray[i]}` })
g.append('path')
.attr('class', (d) => { return `arc ${d.value}` })
.attr('d', arc)
.style('fill', 'grey')
.style('stroke', 'pink')
.style("opacity", 0)
.transition().duration(1000)
.style("opacity", 0.8);
var chords = svg.append('g')
.selectAll('path')
.data(figureCalculation.chords)
.join('path')
.attr('class', 'chords')
.attr('d', d3.ribbon().radius(innerRadius))
.style('fill', 'green')
.style('stroke', 'red')
-update the data binding-
setTimeout(updateData, 2500)
function updateData() {
figureCalculation = d3.chord()
.padAngle(0.05)
.sortSubgroups(d3.descending)(matrix1)
figureCalculation.chords = [];
figureCalculation.forEach((d) => {
figureCalculation.chords.push(d)
})
g.select('path').data(figureCalculation.groups)
.join('path').attr('d', arc)
.style('fill', 'grey')
.style('stroke', 'pink')
chords.select('path').data(figureCalculation.chords)
.join('path')
.attr('d', d3.ribbon().radius(innerRadius))
.style('fill', 'green')
.style('stroke', 'red')
}
The full code is in the following link.
https://codepen.io/jotnajoa/pen/qBaXKVW
Your SVG is structured strangely.
First, for your groups, you create a g with one child of path. Your update doesn't work because you do a selectAll of paths on the g with only one child.
Then for your chords that variable is already a collection of path. You are treating it like it's the g element holding the path.
I'd rewrite your code like this:
let margin = { top: 50, bottom: 50, left: 20, right: 20 }
let width = 600 - margin.left - margin.right;
let height = 600 - margin.top - margin.bottom;
let innerRadius = Math.min(width, height) * 0.4;
let outterRadius = innerRadius * 1.2
let svg = d3.select('#graph').append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr('transform', `translate(${width / 2 + margin.left}, ${height / 2 + margin.top})`)
var nameArray = ['A', 'B', 'C', 'D'];
var matrix = [
[11975, 5871, 8916, 2868],
[1951, 10048, 2060, 6171],
[8010, 16145, 8090, 8045],
[1013, 990, 940, 6907]
];
var matrix1 = [
[175, 571, 916, 868],
[1951, 1248, 2060, 5471],
[8010, 14145, 4390, 4245],
[1213, 990, 540, 1207]
];
let figureCalculation = d3.chord()
.padAngle(0.05)
.sortSubgroups(d3.descending)(matrix)
figureCalculation.chords = [];
figureCalculation.forEach((d) => {
figureCalculation.chords.push(d)
})
var arc = d3.arc().innerRadius(innerRadius).outerRadius(outterRadius)
svg
.append('g')
.attr('class', 'groups')
.selectAll('path')
.data(figureCalculation.groups)
.join('path')
.attr('class', (d, i) => { return `group ${nameArray[i]}` })
.attr('d', arc)
.style('fill', 'grey')
.style('stroke', 'pink')
.style("opacity", 0)
.transition().duration(1000)
.style("opacity", 0.8);
svg
.append('g')
.attr('class', 'chords')
.selectAll('path')
.data(figureCalculation.chords)
.join('path')
.attr('class', 'chords')
.attr('d', d3.ribbon().radius(innerRadius))
.style('fill', 'green')
.style('stroke', 'red')
function updateData() {
figureCalculation = d3.chord()
.padAngle(0.05)
.sortSubgroups(d3.descending)(matrix1)
figureCalculation.chords = [];
figureCalculation.forEach((d) => {
figureCalculation.chords.push(d)
})
svg.select('.groups')
.selectAll('path').data(figureCalculation.groups)
.join('path').attr('d', arc)
.style('fill', 'grey')
.style('stroke', 'pink')
svg.select('.chords')
.selectAll('path').data(figureCalculation.chords)
.join('path')
.attr('d', d3.ribbon().radius(innerRadius))
.style('fill', 'green')
.style('stroke', 'red')
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/6.3.1/d3.min.js"></script>
<button onclick="updateData()">Update Data</button>
<div id="graph"></div>
I think you shouldn't be sub-selecting in updateData at all:
g.data(figureCalculation.groups).join(...)
chords.data(figureCalculation.chords).join(...)

How to fix SVG path masking problem when too many parts of path are cut off when a mask is applied

I have a "Source," a "Target," and a "Spotlight" pointing from the Source to the Target. The Spotlight is a triangular mask over top of a very, very fat line pointing from Source to Target.
In my real project, I have many sources, targets, and spotlights, but I have simplified the problem to one movable source pointing at one static target, to make it easier to ask for help.
function svg(whichone, x, y) {
let svgid = '#svg' + whichone,
// Sorry, no getting around pasting this massive thing...
blobd = "M489.6108513752572,207.11284015473436L489.4560294659143,206.9252152620039C489.30120755657146,206.73759036927336,488.99156373788566,206.36234058381243,488.60876325021906,205.58436508858713C488.2259627625524,204.8063895933618,487.77000560590477,203.6256883883722,487.53814428083814,202.43576304448177C487.30628295577145,201.24583770059135,487.2985174622857,200.0466882178002,487.4753050845048,198.83780557999933C487.6520927067238,197.62892294219844,488.01343344464766,196.4103071493879,488.4001166790572,195.42843725138627C488.7867999134667,194.4465673533846,489.19882564436193,193.70144335019185,489.5453871410477,193.08186110615307C489.8919486377334,192.46227886211432,490.1730459002095,191.96823837722957,490.7544062595619,191.09078512981716C491.3357666189143,190.21333188240473,492.2173900751429,188.95246587246461,492.8629693907238,188.02079535449502C493.5085487063048,187.08912483652543,493.91808388123815,186.4866498105263,494.3375744423906,185.84475101145395C494.7570650035429,185.20285221238169,495.1865109509143,184.52152964023617,495.6640036014667,183.5268463021932C496.14149625201907,182.5321629641502,496.66703560575235,181.22411886020973,496.99924042579045,179.9032473462372C497.33144524582855,178.5823758322647,497.47031553217147,177.24867690826008,497.40718583283814,175.90139490796335C497.3440561335048,174.55411290766668,497.0789264484952,173.19324783107797,496.63195568902864,171.81798706991256C496.18498492956195,170.4427263087472,495.5561730956381,169.05306986300525,495.141810144419,168.19565798406748C494.7274471932,167.33824610512968,494.52753312468576,167.01307879299608,494.0608920729048,166.30297918050618C493.5942510211239,165.5928795680163,492.8608829860762,164.49784765517012,492.07475503925724,163.27798753005956C491.28862709243816,162.05812740494903,490.44973923384765,160.7134390675741,489.9960870379429,159.9871222682472C489.5424348420381,159.26080546892032,489.47401830881904,159.1528602076415,489.14064121819996,158.3553297160253C488.80726412758094,157.55779922440908,488.20892647956185,156.07068350245552,487.85584660613335,154.56521566185057C487.50276673270474,153.05974782124562,487.3949446338667,151.53592786198928,487.56689027001903,149.99254700962464C487.73883590617146,148.44916615726,488.19054927731423,146.88622441178703,488.5778374173524,145.8545335814745C488.9651255573904,144.82284275116197,489.28798846632384,144.3224028360099,489.91628471140956,143.52005249962178C490.5445809564953,142.71770216323367,491.4783105377333,141.6134414056095,492.26443848455233,140.80502784793575C493.0505664313714,139.996614290262,493.6890927437715,139.48404793253863,494.79448384679046,138.8539183938318C495.8998749498096,138.22378885512498,497.4721308434476,137.4760961354347,499.04438673280947,136.95355273301087C500.6166426221714,136.43100933058705,502.1888985072571,136.13361524542967,503.761154396619,135.9767626493178C505.33341028598096,135.81991005320592,506.905666179619,135.80359894613957,508.4779220732571,135.82025242706987C510.0501779668952,135.83690590800018,511.62243386053325,135.88652397692712,512.9165219815047,135.8488339118953C514.2106101024762,135.81114384686342,515.226530450781,135.68614564787276,516.0126583976,135.57603974211614C516.7987863444191,135.46593383635948,517.3551218897524,135.37072022383688,518.4194176092382,134.74471589739423C519.4837133287239,134.1187115709516,521.0559692223619,132.96191653058892,521.9951495139334,132.1609467199919C522.9343298055047,131.35997690939482,523.2404344950095,130.9148323285634,523.8950921373142,129.81803720655554C524.549749779619,128.7212420845477,525.5529603747237,126.9727964213635,526.1860359767905,125.89881105856631C526.8191115788571,124.82482569576912,527.0820521878858,124.42530063335899,527.6871636765428,123.5235081058853C528.2922751652001,122.62171557841161,529.2395575334857,121.21765558587434,530.0256854760286,120.19699114320576C530.8118134185714,119.17632670053716,531.4367869353714,118.53905780773722,532.5354016405905,117.66893805573754C533.6340163458095,116.79881830373785,535.2062722394476,115.6958476925384,536.150326785219,115.08298823169652C537.0943813309905,114.47012877085467,537.4102345288953,114.34738046037042,538.1963624757144,114.0768080262402C538.9824904225334,113.80623559211001,540.2388931182667,113.38783903433387,541.6532224129523,113.09537076587522C543.067551707638,112.80290249741658,544.6398076012762,112.63636251827545,546.2120634949143,112.61224951964807C547.7843193885524,112.58813652102073,549.3565752821904,112.70645050290717,550.9288311758286,112.92661413998724C552.5010870694667,113.14677777706727,554.0733429631047,113.46879106934091,555.1508909731809,113.70210234685634C556.2284389832571,113.93541362437175,556.8112791097715,114.08002288712896,557.5974070523142,114.27653244415835C558.3835349948571,114.47304200118772,559.3729507534285,114.72145185248932,560.6537865795333,114.98809431542468C561.934622405638,115.25473677836,563.5068782992761,115.5396118529291,565.0791341929142,115.72350011679096C566.6513900865524,115.90738838065282,568.2236459801903,115.99028983380744,569.7959018738285,115.99576066255129C571.3681577674666,116.00123149129514,572.9404136611047,115.92927169562824,574.5126695547428,115.84491552500155C576.084925448381,115.76055935437489,577.657181342019,115.66380680878844,579.2294372356572,115.61963727531504C580.8016931292954,115.57546774184162,582.3739490229335,115.58388122048122,583.9462049122953,115.69739747962039C585.5184608016573,115.81091373875957,587.090716686743,116.02953277839832,588.6629725761048,116.40346913433363C590.2352284654668,116.7774054902689,591.8074843591047,117.30665916250075,593.1580542768571,117.90089979171744C594.5086241946095,118.4951404209341,595.6375081364762,119.15436800713559,596.4236360832953,119.63517765343336C597.2097640301143,120.11598729973115,597.6531359818856,120.41837900612519,598.6599038457714,121.32017153359891C599.6666717096572,122.22196406107258,601.2368354856571,123.72315740962593,602.0229634324761,124.47510780086206C602.8090913792952,125.22705819209818,602.8111834969333,125.22976562601711,603.4235604299143,126.10398845760922C604.0359373628953,126.97821128920134,605.258599111219,128.72394951846664,606.0447270580381,129.98760092062153C606.8308550048572,131.25125232277642,607.1804491501715,132.03281689782088,607.5714651789906,132.88139351467927C607.9624812078096,133.7299701315376,608.3949191201333,134.64555879020986,608.8345405064857,135.92802531939654C609.2741618928382,137.2104918485832,609.7209667532192,138.8598362482843,610.0338799418859,140.48685878452295C610.3467931305524,142.1138813207616,610.5258146475048,143.7185819935378,610.6277130765144,145.30240320266242C610.7296115055238,146.88622441178703,610.7543868465906,148.44916615726,610.7476428452096,149.99254700962464C610.7408988438287,151.53592786198928,610.7026355000002,153.05974782124562,610.6528540489714,154.56521566185054C610.6030725979429,156.07068350245552,610.5417730397143,157.55779922440908,610.4552189163047,159.02767388471275C610.3686647928952,160.49754854501643,610.2568561043047,161.9501821436702,610.0782833649904,163.38659855548696C609.8997106256761,164.82301496730372,609.6543738356381,166.24321419228352,609.2882841102381,167.64814202764438C608.9221943848381,169.05306986300525,608.4353517240762,170.4427263087472,608.1221860990572,171.26108246219437C607.8090204740382,172.0794386156415,607.669531884762,172.32649447679375,607.3266711144953,173.0069270150881C606.9838103442286,173.6873595533825,606.4375773929714,174.80116876881894,605.6916086433048,176.0249228385395C604.945639893638,177.24867690826008,603.999935345562,178.5823758322647,603.4869238743714,179.2972881947285C602.9739124031811,180.01220055719236,602.8935940088762,180.1083263581154,602.3918298893333,180.7623484100856C601.8900657697906,181.41637046205582,600.9668559250096,182.62828876507322,600.1807279781905,183.6334068946769C599.3946000313714,184.6385250242806,598.7455539825143,185.43684298047052,598.2307167447619,186.07874177954284C597.7158795070095,186.72064057861516,597.3352510803619,187.2061202205699,596.6658822694952,188.07929304651725C595.9965134586286,188.95246587246461,595.0384042635429,190.21333188240473,594.4425905300477,191.00520592713477C593.8467767965525,191.79707997186483,593.6132585246477,192.11996205138485,593.1173658113333,192.7395442954236C592.621473098019,193.35912653946238,591.8632059432952,194.27540894801987,591.0770779964761,195.17598699268603C590.2909500496572,196.0765650373522,589.4769613107429,196.96143871812706,588.8348638233906,197.57074661453234C588.1927663360383,198.1800545109376,587.7225601002477,198.51379662297327,586.9364321577049,199.0147322866279C586.150304215162,199.51566795028248,585.0482545658668,200.183797165556,583.7111017944002,200.72498480630097C582.3739490229335,201.26617244704593,580.8016931292954,201.68041851326242,579.404376970581,201.94592864702125C578.0070608118667,202.2114387807801,576.7846843880761,202.32821298208134,575.9985564412572,202.40362872259922C575.2124284944381,202.4790444631171,574.8625490245904,202.51310174285163,573.9014813428475,202.59136637554604C572.9404136611047,202.66963100824046,571.3681577674666,202.79210299389476,569.7959018738285,202.9662909502825C568.2236459801903,203.1404789066703,566.6513900865524,203.36638283379156,565.0791341929142,203.64736636341243C563.5068782992761,203.92834989303333,561.934622405638,204.26441302515386,560.3623665162761,204.58867110090966C558.7901106269142,204.91292917666541,557.2178547418285,205.22538219605644,555.6455988524666,205.4318486861571C554.0733429631047,205.63831517625775,552.5010870694667,205.73879513706802,550.9288311758286,205.6732352194946C549.3565752821904,205.60767530192118,547.7843193885524,205.37607550596408,546.2120634949142,204.99567017250527C544.6398076012762,204.61526483904643,543.067551707638,204.08605396808593,541.8027075467809,203.63613921364853C540.5378633859237,203.1862244592111,539.5804309578476,202.81560582129683,538.7943030110285,202.53429690910642C538.0081750642096,202.25298799691606,537.3933515986477,202.06098881044954,536.2998119190476,201.8190199103766C535.2062722394476,201.57705101030365,533.6340163458095,201.2851123966242,532.0617604564476,201.16803717811686C530.4895045670856,201.05096195960957,528.9172486819999,201.10875013627424,527.394423377962,201.35071903634721C525.8715980739238,201.59268793642016,524.3982033509334,202.01883755990136,523.6120754041143,202.24899684074543C522.8259474572952,202.47915612158945,522.7270862866477,202.51332505979641,521.8915277545049,202.98509925704502C521.0559692223619,203.45687345429363,519.4837133287239,204.36625291058394,518.5233307910477,204.9395190439753C517.5629482533715,205.51278517736668,517.2144390716571,205.74993798785906,516.4283111248382,206.25924476774995C515.6421831780191,206.76855154764084,514.4184364660952,207.5500122969302,513.4396472076286,208.1316183333211C512.4608579491619,208.71322436971204,511.7270261441524,209.09497569320445,510.94089819733335,209.4731574987721C510.1547702505143,209.85133930433975,509.31634616188575,210.22595159198264,508.1110061707523,210.60077868520963C506.905666179619,210.9756057784366,505.33341028598096,211.3506476772477,503.7611543966191,211.54178213301086C502.1888985072571,211.73291658877397,500.6166426221714,211.7401436014891,499.04438673280947,211.54061670247242C497.4721308434476,211.34108980345573,495.8998749498096,210.93480899270722,494.79314112725723,210.55636839312265C493.6864073047048,210.1779277935381,493.0451955532382,209.82732740511747,492.2590676064192,209.25804606724674C491.47293965960006,208.68876472937606,490.54189551742866,207.9008024420552,490.07637344634287,207.50682129839478L489.6108513752572,207.11284015473436",
lightd = 'L499.0443867370857,211.74737061420427L546.2120634949142,112.46982253913428L';
let svg = d3.select('body')
.append('svg')
.attr('id', 'svg' + whichone)
.attr("width", 700)
.attr("height", 365);
let border = svg.append('rect')
.attr('x', 0)
.attr('y', 0)
.attr('width', d3.select(svgid).attr('width'))
.attr('height', d3.select(svgid).attr('height'))
.attr('stroke', 'red')
.attr('fill', 'none');
let clippy = d3.select(svgid)
.append('mask')
.attr('id', 'clippyBoi' + whichone);
clippy.append('path')
.attr('class', 'mask-Spotlight-pt1')
.attr('d', 'M' + x + ',' + y + lightd + x + ',' + y)
.attr('stroke', 'white')
.attr('fill', 'white');
clippy.append('path')
.attr('class', 'mask-Spotlight-pt2')
.attr('d', blobd)
.attr('stroke', 'white')
.attr('fill', 'white');
svg.append('path')
.attr('class', 'Target')
.attr('d', blobd)
.attr('stroke', 'blue')
.attr('fill', 'blue');
svg.append('path')
.attr('d', 'M' + x + ',' + y + 'L847.2378366701346,48.24435029368803')
.attr('stroke-width', 580)
.attr('stroke-opacity', 0.5)
.attr('stroke', 'black')
.attr('mask', 'url(#clippyBoi' + whichone + ')');
svg.append('circle')
.attr('class', 'Source')
.attr('cx', x)
.attr('cy', y)
.attr('r', 10)
.attr('fill', 'blue');
svg.append('circle')
.attr('class', 'Spotlight-Endpoint')
.attr('cx', 499.0443867370857)
.attr('cy', 211.74737061420427)
.attr('r', 3)
.attr('fill', 'red')
svg.append('circle')
.attr('class', 'Spotlight-Endpoint')
.attr('cx', 546.2120634949142)
.attr('cy', 112.46982253913428)
.attr('r', 3)
.attr('fill', 'red')
}
// Now let's make a couple of examples with the Source at different heights.
svg('1', 245.1862903196939, 176.69529478458054);
svg('2', 216.59826693715638, 264.39370748299325);
Here it also in action on CodePen
When the Source starts within the same y-axis range as the Target, as in the first SVG, the mask goes into overdrive and too much of the path is cut off.
But, when the Source starts outside the y-axis range as the Target, as in the second SVG, the mask works just fine.
Does anyone out there know a) what's causing this overdrive problem and b) how to fix it? Please?
It seems that there is an issue masking strokes. Changing from a stroke to masking a <rect> seems to work okay though....
svg.append('rect')
.attr('width', '100%')
.attr('height', '100%')
.attr('fill-opacity', 0.5)
.attr('fill', 'black')
.attr('mask', 'url(#clippyBoi' + whichone + ')');
function svg(whichone, x, y) {
let svgid = '#svg' + whichone,
// Sorry, no getting around pasting this massive thing...
blobd = "M489.6108513752572,207.11284015473436L489.4560294659143,206.9252152620039C489.30120755657146,206.73759036927336,488.99156373788566,206.36234058381243,488.60876325021906,205.58436508858713C488.2259627625524,204.8063895933618,487.77000560590477,203.6256883883722,487.53814428083814,202.43576304448177C487.30628295577145,201.24583770059135,487.2985174622857,200.0466882178002,487.4753050845048,198.83780557999933C487.6520927067238,197.62892294219844,488.01343344464766,196.4103071493879,488.4001166790572,195.42843725138627C488.7867999134667,194.4465673533846,489.19882564436193,193.70144335019185,489.5453871410477,193.08186110615307C489.8919486377334,192.46227886211432,490.1730459002095,191.96823837722957,490.7544062595619,191.09078512981716C491.3357666189143,190.21333188240473,492.2173900751429,188.95246587246461,492.8629693907238,188.02079535449502C493.5085487063048,187.08912483652543,493.91808388123815,186.4866498105263,494.3375744423906,185.84475101145395C494.7570650035429,185.20285221238169,495.1865109509143,184.52152964023617,495.6640036014667,183.5268463021932C496.14149625201907,182.5321629641502,496.66703560575235,181.22411886020973,496.99924042579045,179.9032473462372C497.33144524582855,178.5823758322647,497.47031553217147,177.24867690826008,497.40718583283814,175.90139490796335C497.3440561335048,174.55411290766668,497.0789264484952,173.19324783107797,496.63195568902864,171.81798706991256C496.18498492956195,170.4427263087472,495.5561730956381,169.05306986300525,495.141810144419,168.19565798406748C494.7274471932,167.33824610512968,494.52753312468576,167.01307879299608,494.0608920729048,166.30297918050618C493.5942510211239,165.5928795680163,492.8608829860762,164.49784765517012,492.07475503925724,163.27798753005956C491.28862709243816,162.05812740494903,490.44973923384765,160.7134390675741,489.9960870379429,159.9871222682472C489.5424348420381,159.26080546892032,489.47401830881904,159.1528602076415,489.14064121819996,158.3553297160253C488.80726412758094,157.55779922440908,488.20892647956185,156.07068350245552,487.85584660613335,154.56521566185057C487.50276673270474,153.05974782124562,487.3949446338667,151.53592786198928,487.56689027001903,149.99254700962464C487.73883590617146,148.44916615726,488.19054927731423,146.88622441178703,488.5778374173524,145.8545335814745C488.9651255573904,144.82284275116197,489.28798846632384,144.3224028360099,489.91628471140956,143.52005249962178C490.5445809564953,142.71770216323367,491.4783105377333,141.6134414056095,492.26443848455233,140.80502784793575C493.0505664313714,139.996614290262,493.6890927437715,139.48404793253863,494.79448384679046,138.8539183938318C495.8998749498096,138.22378885512498,497.4721308434476,137.4760961354347,499.04438673280947,136.95355273301087C500.6166426221714,136.43100933058705,502.1888985072571,136.13361524542967,503.761154396619,135.9767626493178C505.33341028598096,135.81991005320592,506.905666179619,135.80359894613957,508.4779220732571,135.82025242706987C510.0501779668952,135.83690590800018,511.62243386053325,135.88652397692712,512.9165219815047,135.8488339118953C514.2106101024762,135.81114384686342,515.226530450781,135.68614564787276,516.0126583976,135.57603974211614C516.7987863444191,135.46593383635948,517.3551218897524,135.37072022383688,518.4194176092382,134.74471589739423C519.4837133287239,134.1187115709516,521.0559692223619,132.96191653058892,521.9951495139334,132.1609467199919C522.9343298055047,131.35997690939482,523.2404344950095,130.9148323285634,523.8950921373142,129.81803720655554C524.549749779619,128.7212420845477,525.5529603747237,126.9727964213635,526.1860359767905,125.89881105856631C526.8191115788571," +
"124.82482569576912,527.0820521878858,124.42530063335899,527.6871636765428,123.5235081058853C528.2922751652001,122.62171557841161,529.2395575334857,121.21765558587434,530.0256854760286,120.19699114320576C530.8118134185714,119.17632670053716,531.4367869353714,118.53905780773722,532.5354016405905,117.66893805573754C533.6340163458095,116.79881830373785,535.2062722394476,115.6958476925384,536.150326785219,115.08298823169652C537.0943813309905,114.47012877085467,537.4102345288953,114.34738046037042,538.1963624757144,114.0768080262402C538.9824904225334,113.80623559211001,540.2388931182667,113.38783903433387,541.6532224129523,113.09537076587522C543.067551707638,112.80290249741658,544.6398076012762,112.63636251827545,546.2120634949143,112.61224951964807C547.7843193885524,112.58813652102073,549.3565752821904,112.70645050290717,550.9288311758286,112.92661413998724C552.5010870694667,113.14677777706727,554.0733429631047,113.46879106934091,555.1508909731809,113.70210234685634C556.2284389832571,113.93541362437175,556.8112791097715,114.08002288712896,557.5974070523142,114.27653244415835C558.3835349948571,114.47304200118772,559.3729507534285,114.72145185248932,560.6537865795333,114.98809431542468C561.934622405638,115.25473677836,563.5068782992761,115.5396118529291,565.0791341929142,115.72350011679096C566.6513900865524,115.90738838065282,568.2236459801903,115.99028983380744,569.7959018738285,115.99576066255129C571.3681577674666,116.00123149129514,572.9404136611047,115.92927169562824,574.5126695547428,115.84491552500155C576.084925448381,115.76055935437489,577.657181342019,115.66380680878844,579.2294372356572,115.61963727531504C580.8016931292954,115.57546774184162,582.3739490229335,115.58388122048122,583.9462049122953,115.69739747962039C585.5184608016573,115.81091373875957,587.090716686743,116.02953277839832,588.6629725761048,116.40346913433363C590.2352284654668,116.7774054902689,591.8074843591047,117.30665916250075,593.1580542768571,117.90089979171744C594.5086241946095,118.4951404209341,595.6375081364762,119.15436800713559,596.4236360832953,119.63517765343336C597.2097640301143,120.11598729973115,597.6531359818856,120.41837900612519,598.6599038457714,121.32017153359891C599.6666717096572,122.22196406107258,601.2368354856571,123.72315740962593,602.0229634324761,124.47510780086206C602.8090913792952,125.22705819209818,602.8111834969333,125.22976562601711,603.4235604299143,126.10398845760922C604.0359373628953,126.97821128920134,605.258599111219,128.72394951846664,606.0447270580381,129.98760092062153C606.8308550048572,131.25125232277642,607.1804491501715,132.03281689782088,607.5714651789906,132.88139351467927C607.9624812078096,133.7299701315376,608.3949191201333,134.64555879020986,608.8345405064857,135.92802531939654C609.2741618928382,137.2104918485832,609.7209667532192,138.8598362482843,610.0338799418859,140.48685878452295C610.3467931305524,142.1138813207616,610.5258146475048,143.7185819935378,610.6277130765144,145.30240320266242C610.7296115055238,146.88622441178703,610.7543868465906,148.44916615726,610.7476428452096,149.99254700962464C610.7408988438287,151.53592786198928,610.7026355000002,153.05974782124562,610.6528540489714,154.56521566185054C610.6030725979429,156.07068350245552,610.5417730397143,157.55779922440908,610.4552189163047,159.02767388471275C610.3686647928952,160.49754854501643,610.2568561043047,161.9501821436702,610.0782833649904," +
"163.38659855548696C609.8997106256761,164.82301496730372,609.6543738356381,166.24321419228352,609.2882841102381,167.64814202764438C608.9221943848381,169.05306986300525,608.4353517240762,170.4427263087472,608.1221860990572,171.26108246219437C607.8090204740382,172.0794386156415,607.669531884762,172.32649447679375,607.3266711144953,173.0069270150881C606.9838103442286,173.6873595533825,606.4375773929714,174.80116876881894,605.6916086433048,176.0249228385395C604.945639893638,177.24867690826008,603.999935345562,178.5823758322647,603.4869238743714,179.2972881947285C602.9739124031811,180.01220055719236,602.8935940088762,180.1083263581154,602.3918298893333,180.7623484100856C601.8900657697906,181.41637046205582,600.9668559250096,182.62828876507322,600.1807279781905,183.6334068946769C599.3946000313714,184.6385250242806,598.7455539825143,185.43684298047052,598.2307167447619,186.07874177954284C597.7158795070095,186.72064057861516,597.3352510803619,187.2061202205699,596.6658822694952,188.07929304651725C595.9965134586286,188.95246587246461,595.0384042635429,190.21333188240473,594.4425905300477,191.00520592713477C593.8467767965525,191.79707997186483,593.6132585246477,192.11996205138485,593.1173658113333,192.7395442954236C592.621473098019,193.35912653946238,591.8632059432952,194.27540894801987,591.0770779964761,195.17598699268603C590.2909500496572,196.0765650373522,589.4769613107429,196.96143871812706,588.8348638233906,197.57074661453234C588.1927663360383,198.1800545109376,587.7225601002477,198.51379662297327,586.9364321577049,199.0147322866279C586.150304215162,199.51566795028248,585.0482545658668,200.183797165556,583.7111017944002,200.72498480630097C582.3739490229335,201.26617244704593,580.8016931292954,201.68041851326242,579.404376970581,201.94592864702125C578.0070608118667,202.2114387807801,576.7846843880761,202.32821298208134,575.9985564412572,202.40362872259922C575.2124284944381,202.4790444631171,574.8625490245904,202.51310174285163,573.9014813428475,202.59136637554604C572.9404136611047,202.66963100824046,571.3681577674666,202.79210299389476,569.7959018738285,202.9662909502825C568.2236459801903,203.1404789066703,566.6513900865524,203.36638283379156,565.0791341929142," +
"203.64736636341243C563.5068782992761,203.92834989303333,561.934622405638,204.26441302515386,560.3623665162761,204.58867110090966C558.7901106269142,204.91292917666541,557.2178547418285,205.22538219605644,555.6455988524666,205.4318486861571C554.0733429631047,205.63831517625775,552.5010870694667,205.73879513706802,550.9288311758286,205.6732352194946C549.3565752821904,205.60767530192118,547.7843193885524,205.37607550596408,546.2120634949142,204.99567017250527C544.6398076012762,204.61526483904643,543.067551707638,204.08605396808593,541.8027075467809,203.63613921364853C540.5378633859237,203.1862244592111,539.5804309578476,202.81560582129683,538.7943030110285,202.53429690910642C538.0081750642096,202.25298799691606,537.3933515986477,202.06098881044954,536.2998119190476,201.8190199103766C535.2062722394476,201.57705101030365,533.6340163458095,201.2851123966242,532.0617604564476,201.16803717811686C530.4895045670856,201.05096195960957,528.9172486819999,201.10875013627424,527.394423377962,201.35071903634721C525.8715980739238,201.59268793642016,524.3982033509334,202.01883755990136,523.6120754041143,202.24899684074543C522.8259474572952,202.47915612158945,522.7270862866477,202.51332505979641,521.8915277545049,202.98509925704502C521.0559692223619,203.45687345429363,519.4837133287239,204.36625291058394,518.5233307910477,204.9395190439753C517.5629482533715,205.51278517736668,517.2144390716571,205.74993798785906,516.4283111248382,206.25924476774995C515.6421831780191,206.76855154764084,514.4184364660952,207.5500122969302,513.4396472076286,208.1316183333211C512.4608579491619,208.71322436971204,511.7270261441524,209.09497569320445,510.94089819733335,209.4731574987721C510.1547702505143,209.85133930433975,509.31634616188575,210.22595159198264,508.1110061707523,210.60077868520963C506.905666179619,210.9756057784366,505.33341028598096,211.3506476772477,503.7611543966191,211.54178213301086C502.1888985072571,211.73291658877397,500.6166426221714,211.7401436014891,499.04438673280947,211.54061670247242C497.4721308434476,211.34108980345573,495.8998749498096,210.93480899270722,494.79314112725723,210.55636839312265C493.6864073047048,210.1779277935381,493.0451955532382,209.82732740511747,492.2590676064192,209.25804606724674C491.47293965960006,208.68876472937606,490.54189551742866,207.9008024420552,490.07637344634287,207.50682129839478L489.6108513752572,207.11284015473436",
lightd = 'L499.0443867370857,211.74737061420427L546.2120634949142,112.46982253913428L';
let svg = d3.select('body')
.append('svg')
.attr('id', 'svg' + whichone)
.attr("width", 700)
.attr("height", 365);
let border = svg.append('rect')
.attr('x', 0)
.attr('y', 0)
.attr('width', d3.select(svgid).attr('width'))
.attr('height', d3.select(svgid).attr('height'))
.attr('stroke', 'red')
.attr('fill', 'none');
let clippy = d3.select(svgid)
.append('mask')
.attr('id', 'clippyBoi' + whichone);
clippy.append('path')
.attr('class', 'mask-Spotlight-pt1')
.attr('d', 'M' + x + ',' + y + lightd + x + ',' + y)
.attr('stroke', 'white')
.attr('fill', 'white');
clippy.append('path')
.attr('class', 'mask-Spotlight-pt2')
.attr('d', blobd)
.attr('stroke', 'white')
.attr('fill', 'white');
svg.append('path')
.attr('class', 'Target')
.attr('d', blobd)
.attr('stroke', 'blue')
.attr('fill', 'blue');
svg.append('rect')
.attr('width', '100%')
.attr('height', '100%')
.attr('fill-opacity', 0.5)
.attr('fill', 'black')
.attr('mask', 'url(#clippyBoi' + whichone + ')');
svg.append('circle')
.attr('class', 'Source')
.attr('cx', x)
.attr('cy', y)
.attr('r', 10)
.attr('fill', 'blue');
svg.append('circle')
.attr('class', 'Spotlight-Endpoint')
.attr('cx', 499.0443867370857)
.attr('cy', 211.74737061420427)
.attr('r', 3)
.attr('fill', 'red')
svg.append('circle')
.attr('class', 'Spotlight-Endpoint')
.attr('cx', 546.2120634949142)
.attr('cy', 112.46982253913428)
.attr('r', 3)
.attr('fill', 'red')
}
// Now let's make a couple of examples with the Source at different heights.
svg('1', 245, 156);
svg('2', 245, 264);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>

Load SVG shapes over multiple lines in D3

Data
MP,Party,Constituency,Status
"Abbott, Diane",Labour,Hackney North and Stoke Newington,Remain
"Abrahams, Debbie",Labour,Oldham East and Saddleworth,Remain
"Adams, Nigel",Conservative,Selby and Ainsty,Leave
I have created a svg group for each 'Party' populated with many circles to represent each 'MP' belonging to that Party.
The problem I have is that some of the parties are so large that they run right off the screen. Ideally I would like to set the width at about 10 circles before they return to the next 'line'.
I have found examples for setting the width of SVG text but not SVG shapes. Is it possible to create multiple lines of SVG shapes using D3?
Plunker
var svg = d3.select('body')
.append('svg')
.attr('width', width)
.attr('height', height)
var chart = svg
.append('g')
.attr('transform', 'translate(50,100)')
var party = chart.selectAll(".party")
.data(mps)
.enter()
.append('g')
.attr('transform', function(d, i) {
return "translate(0," + (i * (height / mps.length) + ")")
})
party
.append('text')
.text(function(d) {
return d.key
})
party.selectAll('.members')
.data(function(d) {
return d.values
})
.enter()
.append('circle')
.attr('class', 'chart')
.attr('r', 5)
.attr('cy', '10')
.style('fill', function(d) {
return '#' + d.Color
})
.attr("transform", function(d, i) {
return "translate(" + i * 13 + ",20)"
});
Something like this perhaps? You'll likely have to adjust the values to suit.
.attr('transform', function(d, i) {
return "translate(" + ((i / 10) * 20)) + "," + ((i % 10) * (height / mps.length)) + ")")

D3 - Add background fill to rect

Is it possible to add a background fill to a rect element? I am using rect elements to create a bar graph. Each rect element has a width and fill set. I want to fill the remaining width with a color.
See here: http://codepen.io/jesouhaite08/pen/fhvzA
Thanks!
For best flexibility I would use other rectangles to draw the background for your bars, check the forked example: http://codepen.io/anon/pen/JnlAE
// Bars
svg_fun.selectAll('rect.background')
.data(dataset)
.enter()
.append('rect')
.classed('background', true)
.attr('y', function(d, i) {return i * h_line; })
.attr('x', 0)
.attr('height', 20)
.attr('width', function(d) {return scale_x(max_x);} )
.attr('fill', 'red')
svg_fun.selectAll('rect.bar')
.data(dataset)
.enter()
.append('rect')
.classed('bar', true)
.attr('y', function(d, i) {return i * h_line; })
.attr('x', 0)
.attr('height', 20)
.attr('width', function(d) {return scale_x(d);} )
.attr('fill', 'teal')

Cannot get d3 svg tutorial to work in jsfiddle

I'm trying to follow a d3 tutorial and I've created a JSFiddle for the following code
var dataset = [1,2,3,4,5];
var sampleSVG = d3.select("#viz")
.append("svg")
.attr("width", 400)
.attr("height", 75);
sampleSVG.selectAll("circle")
.data(dataset)
.enter().append("circle")
.style("stroke", "gray")
.style("fill", "red")
.attr("height", 40)
.attr("width", 75)
.attr("x", function(d, i){return i*80})
.attr("y", 20);
However, I see the generated circles in the svg but I can't see them on the screen. Can anyone see what I'm missing?
Here is a FIDDLE:
var dataset = [1,2,3,4,5];
sampleSVG.selectAll("circle")
.data(dataset)
.enter().append("circle")
.style("stroke", "gray")
.style("fill", "red")
.attr("cx", function(d, i){return (i + 1 ) *60})
.attr("cy", 30)
.attr("r", 20);
I just focused on the main parts that needed change. You can study the differences. Basically, you had the wrong attributes for a circle (x and y, instead of cx and cy) and was missing the radius attribute. Finally, height and width are not circle attributes.

Resources