moving child nodes to another <g> element mess up the design - javascript

I have a working static svg graph with root node and 4 child nodes. I wanted to group the child nodes separately but that way the design gets broken up. Here is the working code
<svg id="svg" width="800" height="600" viewbox="0 0 800 600" xmlns="http://www.w3.org/2000/svg">
<g id="root_node" transform="matrix(1 0 0 1 250 100)">
<rect width="200" height="60" rx="5" ry="5">
</rect>
<text x="100" y="30" font-size="14" text-anchor="middle" fill="white">
Root Node
</text>
<line x1="0" y1="0" x2="100" y2="0" transform="translate(0, 0) rotate(-135)" stroke="black" stroke-width="3"/>
<line x1="0" y1="0" x2="100" y2="0" transform="translate(200 0) rotate(-45)" stroke="black" stroke-width="3"/>
<line x1="0" y1="0" x2="100" y2="0" transform="translate(0 60) rotate(135)" stroke="black" stroke-width="3"/>
<line x1="0" y1="0" x2="100" y2="0" transform="translate(200 60) rotate(45)" stroke="black" stroke-width="3"/>
</g>
<g id="child_node">
<rect width="180" height="60" x="0" y="0" rx="5" ry="5">
</rect>
<rect width="180" height="60" x="500" y="0" rx="5" ry="5">
</rect>
<rect width="180" height="60" x="0" y="200" rx="5" ry="5">
</rect>
<rect width="180" height="60" x="500" y="200" rx="5" ry="5">
</rect>
<text x="100" y="30" font-size="14" fill="white" text-anchor="middle">Child Node 1</text>
<text x="600" y="30" font-size="14" fill="white" text-anchor="middle">Child Node 2</text>
<text x="100" y="230" font-size="14" fill="white" text-anchor="middle">Child Node 3</text>
<text x="600" y="230" font-size="14" fill="white" text-anchor="middle">Child Node 4</text>
</g>
<g id="edges">
</g>
</svg>
when i changed the above code to following where I wanted to group the child node in one place, i dont get the above design
<svg id="svg" width="800" height="600" viewbox="0 0 800 600" xmlns="http://www.w3.org/2000/svg">
<g id="root_node" transform="matrix(1 0 0 1 250 100)">
<rect width="200" height="60" rx="5" ry="5">
</rect>
<text x="100" y="30" font-size="14" text-anchor="middle" fill="white">
Root Node
</text>
</g>
<g id="child_node">
<line x1="0" y1="0" x2="100" y2="0" transform="translate(0, 0) rotate(-135)" stroke="black" stroke-width="3"/>
<line x1="0" y1="0" x2="100" y2="0" transform="translate(200 0) rotate(-45)" stroke="black" stroke-width="3"/>
<line x1="0" y1="0" x2="100" y2="0" transform="translate(0 60) rotate(135)" stroke="black" stroke-width="3"/>
<line x1="0" y1="0" x2="100" y2="0" transform="translate(200 60) rotate(45)" stroke="black" stroke-width="3"/>
<rect width="180" height="60" x="0" y="0" rx="5" ry="5">
</rect>
<rect width="180" height="60" x="500" y="0" rx="5" ry="5">
</rect>
<rect width="180" height="60" x="0" y="200" rx="5" ry="5">
</rect>
<rect width="180" height="60" x="500" y="200" rx="5" ry="5">
</rect>
<text x="100" y="30" font-size="14" fill="white" text-anchor="middle">Child Node 1</text>
<text x="600" y="30" font-size="14" fill="white" text-anchor="middle">Child Node 2</text>
<text x="100" y="230" font-size="14" fill="white" text-anchor="middle">Child Node 3</text>
<text x="600" y="230" font-size="14" fill="white" text-anchor="middle">Child Node 4</text>
</g>
</svg>
I want to know the mechanism behind this. Can anyone explain me, please? Do i have to use transformation in child_nodes group to make it work?

Moving child notes to other group in your example messes up output because you are using matrix transformation on one group and not on other - original output it therefore result of child nodes' attributes and transformation on parent node (g).
If you exported SVG from somewhere (fe. Illustrator) it will be easier if you group nodes as required in original file. Or get rid of transform and set child notes accordingly.
I guess it's easier to open SVG in illustrator, do required changes and then export it again, as I did to get the attached snippet.
Other options is to combine groups matrix transformation and line's transformation. Basically your #root_node is just translated (x: 250px, y: 100px). In your case you would just needed to translate line x += 250 & y += 100.
<svg id="svg" width="800" height="600" viewbox="0 0 800 600" xmlns="http://www.w3.org/2000/svg">
<g id="root_node" transform="matrix(1 0 0 1 250 100)">
<rect width="200" height="60" rx="5" ry="5">
</rect>
<text x="100" y="30" font-size="14" text-anchor="middle" fill="white">
Root Node
</text>
</g>
<g id="child_node">
<rect width="180" height="60" x="0" y="0" rx="5" ry="5">
</rect>
<rect width="180" height="60" x="500" y="0" rx="5" ry="5">
</rect>
<rect width="180" height="60" x="0" y="200" rx="5" ry="5">
</rect>
<rect width="180" height="60" x="500" y="200" rx="5" ry="5">
</rect>
<text x="100" y="30" font-size="14" fill="white" text-anchor="middle">Child Node 1</text>
<text x="600" y="30" font-size="14" fill="white" text-anchor="middle">Child Node 2</text>
<text x="100" y="230" font-size="14" fill="white" text-anchor="middle">Child Node 3</text>
<text x="600" y="230" font-size="14" fill="white" text-anchor="middle">Child Node 4</text>
<!-- option 1 -->
<!-- use transform in lines (x1, y1) and (x2, y2) -->
<line class="st3" x1="250" y1="100" x2="179.3" y2="29.3" stroke="black" stroke-width="3"/>
<line class="st3" x1="450" y1="100" x2="520.7" y2="29.3" stroke="black" stroke-width="3"/>
<line class="st3" x1="250" y1="160" x2="179.3" y2="230.7" stroke="black" stroke-width="3"/>
<line class="st3" x1="450" y1="160" x2="520.7" y2="230.7" stroke="black" stroke-width="3"/>
<!-- option 2 -->
<!-- combine group transform (translate) and line's transform -->
<line x1="0" y1="0" x2="100" y2="0" transform="translate(250, 100) rotate(-135)" stroke="black" stroke-width="3"/>
<line x1="0" y1="0" x2="100" y2="0" transform="translate(450, 100) rotate(-45)" stroke="black" stroke-width="3"/>
<line x1="0" y1="0" x2="100" y2="0" transform="translate(250, 160) rotate(135)" stroke="black" stroke-width="3"/>
<line x1="0" y1="0" x2="100" y2="0" transform="translate(450, 160) rotate(45)" stroke="black" stroke-width="3"/>
</g>
<g id="edges">
</g>
</svg>
<svg id="svg" width="800" height="600" viewbox="0 0 800 600" xmlns="http://www.w3.org/2000/svg">
<g id="root_node" transform="matrix(1 0 0 1 250 100)">
<rect width="200" height="60" rx="5" ry="5">
</rect>
<text x="100" y="30" font-size="14" text-anchor="middle" fill="white">
Root Node
</text>
</g>
<g id="child_node">
<line x1="0" y1="0" x2="100" y2="0" transform="translate(250, 100) rotate(-135)" stroke="black" stroke-width="3"/>
<line x1="0" y1="0" x2="100" y2="0" transform="translate(450, 100) rotate(-45)" stroke="black" stroke-width="3"/>
<line x1="0" y1="0" x2="100" y2="0" transform="translate(250, 160) rotate(135)" stroke="black" stroke-width="3"/>
<line x1="0" y1="0" x2="100" y2="0" transform="translate(450, 160) rotate(45)" stroke="black" stroke-width="3"/>
<rect width="180" height="60" x="0" y="0" rx="5" ry="5">
</rect>
<rect width="180" height="60" x="500" y="0" rx="5" ry="5">
</rect>
<rect width="180" height="60" x="0" y="200" rx="5" ry="5">
</rect>
<rect width="180" height="60" x="500" y="200" rx="5" ry="5">
</rect>
<text x="100" y="30" font-size="14" fill="white" text-anchor="middle">Child Node 1</text>
<text x="600" y="30" font-size="14" fill="white" text-anchor="middle">Child Node 2</text>
<text x="100" y="230" font-size="14" fill="white" text-anchor="middle">Child Node 3</text>
<text x="600" y="230" font-size="14" fill="white" text-anchor="middle">Child Node 4</text>
</g>
</svg>

Related

Variable text in SVG defined text tag

I would like to re-use def-ined shapes in SVG but with variable text.
Is the following somehow possibel ?
<svg width="1000pt" height="1000pt" viewBox="0.00 0.00 1000.00 1000.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<g id="shape">
<rect x="10" y="0" rx="5" ry="5" width="80" height="40" style="fill:lightblue;stroke-width:0,opacity:0.5" />
<text text-anchor="middle" x="40" y="20" font-family="Helvetica,sans-Serif" font-size="8.00">variable_text</text>
</g>
</defs>
<g transform="translate(0 0)">
<use xlink:href="#shape" text="test" />
</g>
<g transform="translate(100 0)">
<use xlink:href="#shape" text="test2" />
</g>
</svg>
EDIT: since the solution would probably involve some javascript I have added the tag.
Clone the template and adjust as necessary.
let shape = document.getElementById("shape");
Array.from(document.getElementsByTagName("use")).forEach((use) => {
let text = use.getAttribute("text");
let clone = shape.cloneNode(true);
// might want to do something more robust here
clone.children[1].textContent = text;
use.parentNode.appendChild(clone);
use.parentNode.removeChild(use);
})
<svg width="1000pt" height="1000pt" viewBox="0.00 0.00 1000.00 1000.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<g id="shape">
<rect x="10" y="0" rx="5" ry="5" width="80" height="40" style="fill:lightblue;stroke-width:0,opacity:0.5" />
<text text-anchor="middle" x="40" y="20" font-family="Helvetica,sans-Serif" font-size="8.00">variable_text</text>
</g>
</defs>
<g transform="translate(0 0)">
<use xlink:href="#shape" text="test" />
</g>
<g transform="translate(100 0)">
<use xlink:href="#shape" text="test2" />
</g>
</svg>

Calculate SVG.Text Length before drawing [duplicate]

I want to color the background of svg text similar to background-color in css
I was only able to find documentation on fill, which colors the text itself
Is it even possible?
You could use a filter to generate the background.
<svg width="100%" height="100%">
<defs>
<filter x="0" y="0" width="1" height="1" id="solid">
<feFlood flood-color="yellow" result="bg" />
<feMerge>
<feMergeNode in="bg"/>
<feMergeNode in="SourceGraphic"/>
</feMerge>
</filter>
</defs>
<text filter="url(#solid)" x="20" y="50" font-size="50">solid background</text>
</svg>
No this is not possible, SVG elements do not have background-... presentation attributes.
To simulate this effect you could draw a rectangle behind the text attribute with fill="green" or something similar (filters). Using JavaScript you could do the following:
var ctx = document.getElementById("the-svg"),
textElm = ctx.getElementById("the-text"),
SVGRect = textElm.getBBox();
var rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
rect.setAttribute("x", SVGRect.x);
rect.setAttribute("y", SVGRect.y);
rect.setAttribute("width", SVGRect.width);
rect.setAttribute("height", SVGRect.height);
rect.setAttribute("fill", "yellow");
ctx.insertBefore(rect, textElm);
The solution I have used is:
<svg>
<line x1="100" y1="100" x2="500" y2="100" style="stroke:black; stroke-width: 2"/>
<text x="150" y="105" style="stroke:white; stroke-width:0.6em">Hello World!</text>
<text x="150" y="105" style="fill:black">Hello World!</text>
</svg>
A duplicate text item is being placed, with stroke and stroke-width attributes. The stroke should match the background colour, and the stroke-width should be just big enough to create a "splodge" on which to write the actual text.
A bit of a hack and there are potential issues, but works for me!
Instead of using a <text> tag, the <foreignObject> tag can be used, which allows for XHTML content with CSS.
No, you can not add background color to SVG elements. You can do it programmatically with d3.
var text = d3.select("text");
var bbox = text.node().getBBox();
var padding = 2;
var rect = self.svg.insert("rect", "text")
.attr("x", bbox.x - padding)
.attr("y", bbox.y - padding)
.attr("width", bbox.width + (padding*2))
.attr("height", bbox.height + (padding*2))
.style("fill", "red");
Answer by Robert Longson (#RobertLongson) with modifications:
<svg width="100%" height="100%">
<defs>
<filter x="0" y="0" width="1" height="1" id="solid">
<feFlood flood-color="yellow"/>
<feComposite in="SourceGraphic" operator="xor"/>
</filter>
</defs>
<text filter="url(#solid)" x="20" y="50" font-size="50"> solid background </text>
<text x="20" y="50" font-size="50">solid background</text>
</svg>
and we have no bluring and no heavy "getBBox" :)
Padding is provided by white spaces in text-element with filter.
It's worked for me
Going further with #dbarton_uk answer, to avoid duplicating text you can use paint-order=stroke style:
<svg>
<line x1="100" y1="100" x2="350" y2="100" style="stroke:grey; stroke-width: 100"/>
<text x="150" y="105" style="stroke:white; stroke-width:0.5em; fill:black; paint-order:stroke; stroke-linejoin:round">Hello World!</text>
</svg>
Note the stroke-linejoin:round which is needed to avoid seeing spikes for the W sharp angle.
You can combine filter with the text.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>SVG colored patterns via mask</title>
</head>
<body>
<svg viewBox="0 0 300 300" xmlns="http://www.w3.org/2000/svg">
<defs>
<filter x="0" y="0" width="1" height="1" id="bg-text">
<feFlood flood-color="white"/>
<feComposite in="SourceGraphic" operator="xor" />
</filter>
</defs>
<!-- something has already existed -->
<rect fill="red" x="150" y="20" width="100" height="50" />
<circle cx="50" cy="50" r="50" fill="blue"/>
<!-- Text render here -->
<text filter="url(#bg-text)" fill="black" x="20" y="50" font-size="30">text with color</text>
<text fill="black" x="20" y="50" font-size="30">text with color</text>
</svg>
</body>
</html>
For those wondering how to apply padding to a text element when it has a background like in the Robert's answer, do the following:
<svg>
<defs>
<filter x="-0.1" y="-0.1" width="1.2" height="1.2" id="solid">
<feFlood flood-color="#171717"/>
<feComposite in="SourceGraphic" operator="xor" />
</filter>
</defs>
<text filter="url(#solid)" x="20" y="50" font-size="50">Hello</text>
</svg>
In the example above, filter's x and y positions can be used as transform: translate(-10%, -10%) would, and width and height values can be read as 120% and 120%. So we made background 20% bigger, and offsetted it -10%, so background is now 10% bigger on each side of the text.
this is my favorite hack (not sure it should work). It refer an element that is not yet displayed, and it works pretty well
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 620 40" preserveAspectRatio="xMidYMid meet">
<defs>
<filter x="-0.02" y="0" width="1.04" height="1.1" id="removebackground">
<feFlood flood-color="#00ffff"/>
</filter>
</defs>
<!--Draw the text-->
<use xlink:href="#mygroup" filter="url(#removebackground)" />
<g id="mygroup">
<text id="text1" x="9" y="20" style="text-anchor:start;font-size:14px;">custom text with background</text>
<line x1="200" y1="18" x2="200" y2="36" stroke="#000" stroke-width="5"/>
<line x1="120" y1="27" x2="203" y2="27" stroke="#000" stroke-width="5"/>
</g>
</svg>
The previous answers relied on doubling up text and lacked sufficient whitespace.
By using atop and I was able to get the results I wanted.
This example also includes arrows, a common use case for SVG text labels:
<svg viewBox="-105 -40 210 234">
<title>Size Guide</title>
<defs>
<filter x="0" y="0" width="1" height="1" id="solid">
<feFlood flood-color="white"></feFlood>
<feComposite in="SourceGraphic" operator="atop"></feComposite>
</filter>
<marker id="arrow" viewBox="0 0 10 10" refX="5" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse">
<path d="M 0 0 L 10 5 L 0 10 z"></path>
</marker>
</defs>
<g id="garment">
<path id="right-body" fill="none" stroke="black" stroke-width="1" stroke-linejoin="round" d="M0 0 l30 0 l0 154 l-30 0"></path>
<path id="right-sleeve" d="M30 0 l35 0 l0 120 l-35 0" fill="none" stroke-linejoin="round" stroke="black" stroke-width="1"></path>
<use id="left-body" href="#right-body" transform="scale(-1,1)"></use>
<use id="left-sleeve" href="#right-sleeve" transform="scale(-1,1)"></use>
<path id="collar-right-top" fill="none" stroke="black" stroke-width="1" stroke-linejoin="round" d="M0 -6.5 l11.75 0 l6.5 6.5"></path>
<use id="collar-left-top" href="#collar-right-top" transform="scale(-1,1)"></use>
<path id="collar-left" fill="white" stroke="black" stroke-width="1" stroke-linejoin="round" d="M-11.75 -6.5 l-6.5 6.5 l30 77 l6.5 -6.5 Z"></path>
<path id="front-right" fill="white" stroke="black" stroke-width="1" d="M18.25 0 L30 0 l0 154 l-41.75 0 l0 -77 Z"></path>
<line x1="0" y1="0" x2="0" y2="154" stroke="black" stroke-width="1" stroke-dasharray="1 3"></line>
<use id="collar-right" href="#collar-left" transform="scale(-1,1)"></use>
</g>
<g id="dimension-labels">
<g id="dimension-sleeve-length">
<line marker-start="url(#arrow)" marker-end="url(#arrow)" x1="85" y1="0" x2="85" y2="120" stroke="black" stroke-width="1"></line>
<text font-size="10" filter="url(#solid)" fill="black" x="85" y="60" class="dimension" text-anchor="middle" dominant-baseline="middle"> 120 cm</text>
</g>
<g id="dimension-length">
<line marker-start="url(#arrow)" marker-end="url(#arrow)" x1="-85" y1="0" x2="-85" y2="154" stroke="black" stroke-width="1"></line>
<text font-size="10" filter="url(#solid)" fill="black" x="-85" y="77" text-anchor="middle" dominant-baseline="middle" class="dimension"> 154 cm</text>
</g>
<g id="dimension-sleeve-to-sleeve">
<line marker-start="url(#arrow)" marker-end="url(#arrow)" x1="-65" y1="-20" x2="65" y2="-20" stroke="black" stroke-width="1"></line>
<text font-size="10" filter="url(#solid)" fill="black" x="0" y="-20" text-anchor="middle" dominant-baseline="middle" class="dimension"> 130 cm </text>
</g>
<g title="Back Width" id="dimension-back-width">
<line marker-start="url(#arrow)" marker-end="url(#arrow)" x1="-30" y1="174" x2="30" y2="174" stroke="black" stroke-width="1"></line>
<text font-size="10" filter="url(#solid)" fill="black" x="0" y="174" text-anchor="middle" dominant-baseline="middle" class="dimension"> 60 cm </text>
</g>
</g>
</svg>
An obvious workaround to the problem of the blur produced by the filter effect is to render the <text> two times: once for the background (with transparent characters) and once for the characters (without a background filter).
For me, this was the only way to make the text readable in Safari.
<svg width="100%" height="100%">
<filter x="0" y="0" width="1" height="1" id="solid">
<feFlood flood-color="yellow" />
</filter>
<g transform="translate(20, 50)" font-size="50">
<text aria-hidden="true" fill="none" filter="url(#solid)">solid background</text>
<text fill="blue">solid background</text>
</g>
</svg>
The aria-hidden="true" attribute is there to prevent screen readers from speaking the text twice, if the user uses a screen reader.
You can add style to your text:
style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
text-shadow: rgb(255, 255, 255) -2px -2px 0px, rgb(255, 255, 255) -2px 2px 0px,
rgb(255, 255, 255) 2px -2px 0px, rgb(255, 255, 255) 2px 2px 0px;"
White, in this example.
Does not work in IE :)

Adding image to SVG circles

I'm trying to add some background image to SVG nodes "circle".
I have read a lot of stackoverflow answer and everybody is saying that we need to add a node into a to define our image.
I tried this but my image is not displaying at all.
Here is a fiddle of my code: https://jsfiddle.net/baapu6wz/ .
What did I miss ?
<svg baseProfile="full" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" height="390" width="1629">
<g transform="translate(40,0)">
<g>
<line y2="149.3060251652327" x2="819.8567597731511" y1="222.22245513917517" x1="854.7332277213098" style="stroke: #999;" stroke-width="5"></line>
<line y2="213.47136779636722" x2="768.9096407109324" y1="213.47136779636722" x1="768.9096407109324" style="stroke: #999;" stroke-width="5"></line>
<line y2="213.47136779636722" x2="768.9096407109324" y1="149.3060251652327" x1="819.8567597731511" style="stroke: #999;" stroke-width="5"></line>
</g>
<g>
<g transform="translate(854.7332277213098, 222.22245513917517)">
<circle fill="url(#image1);" fillOpacity="0.5" r="16"></circle>
<text x="20" dy="3">pagx</text>
</g>
<g transform="translate(768.9096407109324, 213.47136779636722)">
<circle fill="url(#image1);" fillOpacity="0.5" r="10"></circle>
<text x="20" dy="3">xzreds</text>
</g>
<g transform="translate(819.8567597731511, 149.3060251652327)">
<circle fill="url(#image1);" fillOpacity="0.5" r="14"></circle>
<text x="20" dy="3">jzkcwv</text>
</g>
</g>
</g>
<defs>
<pattern width="16" height="16" patternUnits="userSpaceOnUse" y="0" x="0" id="image1">
<image xlink:href="https://www.google.fr/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" height="16" width="16" y="0" x="0"></image>
</pattern>
</defs>
You have a typo.
fill="url(#image1);"
should be
fill="url(#image1)"
Remove the semicolon.
https://jsfiddle.net/baapu6wz/1/

On Chrome SVG chart arrowhead marker-mid is viewed 3 times instead 1

I have created simple force chart in D3.js.
There are nodes and between nodes there are links with the markers in the middle (using marked-mid).
The problem is that for some reason on Chrome this marker is rendered 3 times instead 1. On IE and FF it works good.
Below I'm posting code only in SVG as this is I think path issue not connected to D3.js.
<svg class="baseSVG" style="width: 100%; height: 90%;">
<g class="chart_wrapper">
<g class="links">
<g class="link_wrapper">
<path class="link hate" marker-mid="url(#arrow)" d="M 342.9891277180959 320.7671874251745 A 190.7182256881292 190.7182256881292 0 0 1 225.76595293193114 281.82896265692517 A 190.7182256881292 190.7182256881292 0 0 1 156.98831902253903 179.22732948826612"></path>
</g>
</g>
<g id="person0" class="node person Adam" transform="translate(156.98831902253903,179.22732948826612)">
<g class="node_inner" transform="translate(-50,-30)">
<rect width="100" height="60" rx="15" ry="15" class="node_background"></rect>
<text text-anchor="middle" transform="translate(50,70)">Adam</text>
</g>
</g>
<g id="person1" class="node person Eve" transform="translate(342.9891277180959,320.7671874251745)">
<g class="node_inner" transform="translate(-50,-30)">
<rect width="100" height="60" rx="15" ry="15" class="node_background"></rect>
<text text-anchor="middle" transform="translate(50,70)">Eve</text>
</g>
</g>
<g id="person2" class="node person Kazik" transform="translate(179.355374155828,342.83615430106596)">
<g class="node_inner" transform="translate(-50,-30)">
<rect width="100" height="60" rx="15" ry="15" class="node_background"></rect>
<text text-anchor="middle" transform="translate(50,70)">Kazik</text>
</g>
</g>
<g id="person3" class="node person Janina" transform="translate(320.66071665952427,157.09136472857676)">
<g class="node_inner" transform="translate(-50,-30)">
<rect width="100" height="60" rx="15" ry="15" class="node_background"></rect>
<text text-anchor="middle" transform="translate(50,70)">Janina</text>
</g>
</g>
</g>
<defs>
<marker id="double-arrow" refY="0" refX="0" viewBox="0 -20 40 40" markerWidth="40" markerHeight="40" markerUnits="userSpaceOnUse" class="double-arrow" orient="auto">
<path d="M0,-5L10,0L0,5" transform="translate(20)"></path>
<path d="M0,0L10,-5L10,5" transform="translate(0)"></path>
</marker>
</defs>
<defs></defs>
<defs>
<marker id="arrow" viewBox="0 -5 10 10" refX="5" refY="0" markerWidth="10" markerHeight="10" orient="auto" markerUnits="userSpaceOnUse" class="arrow">
<path d="M0,-5L10,0L0,5"></path>
</marker>
</defs>
<defs></defs>
</svg>
Example in JSFiddle
If someone is looking for an answer.
#theOneGuy and #echonax were right. To fix is that I can't use marker-mid and instead use marker-start.
So instead of one path definition that show line with marker-mid I have two paths that meet on the chart creating one line. Second path have marker-start.
Marker start because on second path because it need to be on top in terms of z-index.
<svg class="baseSVG" style="width: 100%; height: 90%;">
<g class="chart_wrapper">
<g class="links">
<g class="link_wrapper">
<path class="link love half1" half1="true" d="M 346.8046341906862 223.99666940803763 L 242.21936721772218 252.41406759336707"></path>
<path class="link love half2" marker-start="url(#arrow)" half1="false" d="M 242.21936721772218 252.41406759336707 L 137.63410024475814 280.8314657786965"></path>
</g>
<g class="link_wrapper">
<path class="link love half1" half1="true" d="M 346.8046341906862 223.99666940803763 A 303.64066468574316 303.64066468574316 0 0 1 247.4635305967095 271.71429428774377"></path>
<path class="link love half2" marker-start="url(#arrow)" half1="false" d="M 247.4635305967095 271.71429428774377 A 303.64066468574316 303.64066468574316 0 0 1 137.63410024475814 280.8314657786965"></path>
</g>
<g class="link_wrapper">
<path class="link hate half1" half1="true" d="M 137.63410024475814 280.8314657786965 A 166.82033234287158 166.82033234287158 0 0 1 231.73104045974543 213.81361420460564"></path>
<path class="link hate half2" marker-start="url(#double-arrow)" half1="false" d="M 231.73104045974543 213.81361420460564 A 166.82033234287158 166.82033234287158 0 0 1 346.8046341906862 223.99666940803763"></path>
</g>
</g>
<g id="person0" class="node person Adam" transform="translate(346.8046341906862,223.99666940803763)">
<g class="node_inner" transform="translate(-50,-30)">
<rect width="100" height="60" rx="15" ry="15" class="node_background"></rect>
<text text-anchor="middle" transform="translate(50,70)">Adam</text>
</g>
</g>
<g id="person1" class="node person Eve" transform="translate(137.63410024475814,280.8314657786965)">
<g class="node_inner" transform="translate(-50,-30)">
<rect width="100" height="60" rx="15" ry="15" class="node_background"></rect>
<text text-anchor="middle" transform="translate(50,70)">Eve</text>
</g>
</g>
<g id="person2" class="node person Kazik" transform="translate(289.2970047078608,367.65475135214126)">
<g class="node_inner" transform="translate(-50,-30)">
<rect width="100" height="60" rx="15" ry="15" class="node_background"></rect>
<text text-anchor="middle" transform="translate(50,70)">Kazik</text>
</g>
</g>
<g id="person3" class="node person Janina" transform="translate(226.0636547128077,127.42597160431035)">
<g class="node_inner" transform="translate(-50,-30)">
<rect width="100" height="60" rx="15" ry="15" class="node_background"></rect>
<text text-anchor="middle" transform="translate(50,70)">Janina</text>
</g>
</g>
</g>
<defs>
<marker id="double-arrow" refY="0" refX="0" viewBox="0 -20 40 40" markerWidth="40" markerHeight="40" markerUnits="userSpaceOnUse" class="double-arrow" orient="auto">
<path d="M0,-5L10,0L0,5" transform="translate(20)"></path>
<path d="M0,0L10,-5L10,5" transform="translate(0)"></path>
</marker>
</defs>
<defs>
<marker id="arrow" viewBox="0 -5 10 10" refX="5" refY="0" markerWidth="10" markerHeight="10" orient="auto" markerUnits="userSpaceOnUse" class="arrow">
<path d="M0,-5L10,0L0,5"></path>
</marker>
</defs>
</svg>
Also I have updated Js Fiddle

Uncaught TypeError: Cannot read property 'childNodes' of null

Not sure why I get this error. Read about the root of the problem seems to exist when JavaScript executes before the element is loaded however in my case I use onload function on body tag. also the JavaScript is just before the body tag closes.Not sure what is causing this problem. **The error correspondes to second line of initLegend() function
this is the line the throws error
legendGroup = svgDoc.getElementById("legendGroup").childNodes;
Code
<body onload="init()">
<h1>Co2 Emissions</h1>
<div id="viz" style='margin-top: -5px;'></div>
<script type="text/javascript">
var changeArray = [-80,-60,-40,-20,0,20,40,60,80];
var colorArray = ["#053061", "#2166ac", "#4393c3", "#92c5de", "#F1F7FA", "#fddbc7", "#f4a582", "#d6604d", "#b2182b", "#67001f"];
var legend;
var legendGroup;
svgDoc = document;
var svg_0 = d3.xml("svg_drawing.svg", "image/svg+xml", function(xml) {
var importedNode = document.importNode(xml.documentElement, true);
d3.select("#viz").node().appendChild(importedNode)});
function initLegend()
{
legend = svgDoc.getElementById("legend");
legendGroup = svgDoc.getElementById("legendGroup").childNodes;
// set text for headline
var node = legend.childNodes.item(1);
node.firstChild.nodeValue = currentMonth + currentYear;
node = node.nextSibling.nextSibling;
node.firstChild.nodeValue = legendHeadline;
node = node.nextSibling.nextSibling;
node.firstChild.nodeValue = legendLabelDecrease;
node = node.nextSibling.nextSibling;
node.firstChild.nodeValue = legendLabelIncrease1;
node = node.nextSibling.nextSibling;
node.firstChild.nodeValue = legendLabelIncrease2;
// set legend items
if(debug) { alert("legendGroup.length: " + legendGroup.length); }
var rectInvariant = 0;
var textInvariant = 0;
for(var i=0; i<legendGroup.length; i++)
{
if(legendGroup.item(i))
{
if(legendGroup.item(i).nodeName == "rect")
{
legendGroup.item(i).setAttribute("fill",colorArray[rectInvariant++]);
} else
{
if(legendGroup.item(i).nodeName == "text")
{
legendGroup.item(i).firstChild.nodeValue = changeArray[textInvariant++] + "%";
}
}
}
}
}
function init(){
initLegend()
};
</script>
</body>
</html>
SVG og legend
<g id="legend" transform="matrix(1 0 0 1 685 28)">
<text id="legendHeadline" class="legendHeadline" x="87" y="-7">...</text>
<text id="legendSubheadline" class="legendSubheadline" x="95" y="-7">...</text>
<text id="decrease" class="legendLabel" x="-25" y="17">...</text>
<text id="increase1" class="legendLabel" x="379" y="10">...</text>
<text id="increase2" class="legendLabel" x="379" y="22">...</text>
<g id="legendGroup" transform="matrix(1 0 0 1 0 8)">
<rect x="0" y="0" width="35" height="10" stroke="#000000" stroke-width="1px" fill="#FFFFFF" />
<text x="35" y="27" class="legendStyle">...</text>
<rect x="35" y="0" width="35" height="10" stroke="#000000" stroke-width="1" fill="#FFFFFF" />
<text x="70" y="27" class="legendStyle">...</text>
<rect x="70" y="0" width="35" height="10" stroke="#000000" stroke-width="1px" fill="#FFFFFF" />
<text x="105" y="27" class="legendStyle">...</text>
<rect x="105" y="0" width="35" height="10" stroke="#000000" stroke-width="1px" fill="#FFFFFF" />
<text x="140" y="27" class="legendStyle">...</text>
<rect x="140" y="0" width="35" height="10" stroke="#000000" stroke-width="1px" fill="#FFFFFF" />
<text x="175" y="27" class="legendStyle">...</text>
<rect x="175" y="0" width="35" height="10" stroke="#000000" stroke-width="1px" fill="#FFFFFF" />
<text x="210" y="27" class="legendStyle">...</text>
<rect x="210" y="0" width="35" height="10" stroke="#000000" stroke-width="1px" fill="#FFFFFF" />
<text x="245" y="27" class="legendStyle">...</text>
<rect x="245" y="0" width="35" height="10" stroke="#000000" stroke-width="1px" fill="#FFFFFF" />
<text x="280" y="27" class="legendStyle">...</text>
<rect x="280" y="0" width="35" height="10" stroke="#000000" stroke-width="1px" fill="#FFFFFF" />
<text x="315" y="27" class="legendStyle">...</text>
<rect x="315" y="0" width="35" height="10" stroke="#000000" stroke-width="1px" fill="#FFFFFF" />
</g>
</g>
You need simply to move the initLegend() call into the callback handler, right after the point at which you append it. Loading the SVG is asynchronous, and the window "load" event will not wait for that to finish.
edit like this:
var svg_0 = d3.xml("svg_drawing.svg", "image/svg+xml", function(xml) {
var importedNode = document.importNode(xml.documentElement, true);
d3.select("#viz").node().appendChild(importedNode);
initLegend();
});

Categories