Trouble creating a closed line chart in svg - svg

I've got a react component which is creating an svg line chart (I'm not using a library, just creating the svg itself).
Problem is, when I add a final point to the path to return to the starting point, I have a strange 45degree angle showing up at the end of the chart.
Can anybody explain why this is not nicely closed?
Here's an example https://jsfiddle.net/7svavrmu/1/
From what I understand, the final L 0 300 should be returning the path to the origin.
Here's the code
<svg width="300" height="67.40652464075235">
<path fill="blue" stroke="black"
d="M 0 40.32613081539207
L 0.15306122448979592 40.990776224724726
L 0.25510204081632654 41.834373941621585
L 0.30612244897959184 62.31225269212592
L 299.0816326530612 45.84534164491692
L 299.33673469387753 65.256033885832
L 299.48979591836735 45.314084715607414
L 300 45.27080004137377 L 0 300 "></path>
</svg>

In SVG paths, each letter is an instruction and the following numbers are the coordinates for that instruction.
Your path ends at a strange location, L 0 300 is the bottom left location but way off the viewport, you need to "draw" the bottom part of your graph by removing the last instruction and adding L 300 67 (bottom right corner) and L 0 67 (bottom left corner). Putting it all together your path needs to look like this:
d="M 0 40.32613081539207
L 0.15306122448979592 40.990776224724726
L 0.25510204081632654 41.834373941621585
L 0.30612244897959184 62.31225269212592
L 299.0816326530612 45.84534164491692
L 299.33673469387753 65.256033885832
L 299.48979591836735 45.314084715607414
L 300 45.27080004137377 L 300 67 L 0 67"

Related

change 0,0 position of svg path

I have a svg path as below:
'M12,2 C 8.13,2 5,5.13 5,9 c 0,5.25 7,13 7,13s7,-7.75 7,-13C19,5.13 15.87,2 12,2zM7,9c0,-2.76 2.24,-5 5,-5s5,2.24 5,5c0,2.88 -2.88,7.19 -5,9.88C9.92,16.21 7,11.85 7,9z M12,9m-2.5,0a2.5,2.5 0,1 1,5 0a2.5,2.5 0,1 1,-5 0'
Now the tip point at (12,22), I would like to keep shape the same but the tip point at (0,0).
Which tool can do such position shift?
One possible solution would be using this tool to convert the path to to all-relative path commands.
For example convert:
M12,2 C 8.13,2 5,5.13 5,9 c 0,5.25 7,13 7,13s7,-7.75 7,-13C19,5.13 15.87,2 12,2zM7,9c0,-2.76 2.24,-5 5,-5s5,2.24 5,5c0,2.88 -2.88,7.19 -5,9.88C9.92,16.21 7,11.85 7,9z M12,9m-2.5,0a2.5,2.5 0,1 1,5 0a2.5,2.5 0,1 1,-5 0
to:
M12,2c-3.87,0,-7,3.13,-7,7c0,5.25,7,13,7,13s7,-7.75,7,-13c0,-3.87,-3.13,-7,-7,-7zm-5,7c0,-2.76,2.24,-5,5,-5s5,2.24,5,5c0,2.88,-2.88,7.19,-5,9.88c-2.08,-2.67,-5,-7.03,-5,-9.88zm5,0m-2.5,0a2.5,2.5,0,1,1,5,0a2.5,2.5,0,1,1,-5,0
Now in order to move it in the 0,0 just change the first two values after the initial M command from 12,2 to 0,-18.
The first value is 0 and represents the x value of both tue top and the tip. The second value is -18 and represents the y value of the starting point of the path located almost at the top. In order to get this value I did: 2 (actual y coordinate of the point) - 20 (height of the path).
In order to get the height of the path I'm using getBBox()
console.log(g.getBBox())
svg{width:30vh;border:solid}
<svg viewBox="-7 -18 14 20">
<path id="g" d="M0,-18c-3.87,0,-7,3.13,-7,7c0,5.25,7,13,7,13s7,-7.75,7,-13c0,-3.87,-3.13,-7,-7,-7zm-5,7c0,-2.76,2.24,-5,5,-5s5,2.24,5,5c0,2.88,-2.88,7.19,-5,9.88c-2.08,-2.67,-5,-7.03,-5,-9.88zm5,0m-2.5,0a2.5,2.5,0,1,1,5,0a2.5,2.5,0,1,1,-5,0"/>
</svg>

Fill color is not applied to the SVG path

To draw some shape based on the data, I am using the SVG path, but with a particular height and width, fill color is not applied to the path element properly. Just copied my path direction and created a sample below, if you change the width for SVG, the path is filled properly
<svg height="450" width="450">
<path d="M 0 118.65730685696555 L 0 0 L 46.4375 335.9090909090909 L 92.875 0 L 139.3125 0 L 185.75 0 L 278.625 0 L 325.0625 0 L 371.5 0 L 371.5 12.864603481624718 L 325.0625 13.465478183788035 L 278.625 28.056480133139154 L 185.75 64.31377161001998 L 139.3125 130.11162255466053 L 92.875 335.9090909090909 L 46.4375 335.9090909090909 L 0 118.65730685696555" />
</svg>
what will be the issue, how to fix this?
If you tweak the path just slightly so that the two points at the bottom left aren't coincident, Chrome can properly fill it.
<svg height="450" width="450">
<path d="M 0 118.65730685696555 L 0 0 L 46.4375 335.9090909090909 L 92.875 0 L 139.3125 0 L 185.75 0 L 278.625 0 L 325.0625 0 L 371.5 0 L 371.5 12.864603481624718 L 325.0625 13.465478183788035 L 278.625 28.056480133139154 L 185.75 64.31377161001998 L 139.3125 130.11162255466053 L 92.875 335.87 L 46.4375 335.9090909090909 L 0 118.65730685696555" />
</svg>

Border radius in SVG d3

I have following svg:
` svg.append('g')
.append("path")
.attr("d", "M 0 150 L 0 0 L 190 0 L 190 150 Z")
.attr("fill", "#f6f6f6"); `
Look like this:
But I want like this:
I don't have much knowledge about svg curve. How can I set border radius like this?
Done with Quadratic Bezier path:
svg {
background-color: #aaa;
}
<svg width="300" height="180">
<g transform="translate(10,10)">
<path d="M 60,0 H 180 Q 190,0 190,10 V 140 Q 190,150 180,150 H 10 Q 0,150 0,140 V 60 Q 0,0 60,0 Z" fill="#d6d6d6"/>
</g>
</svg>
You can find a detailed description of path here:
https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths
The simplest way to round a corner is Quadratic Bezier path.
For example, if you need to round a top-right corner with approximate radius r, and the coordinate of the corner are x and y, The path will look like this:
Move to (M) / Line to (L) / Horizontal line (H) to x - r, y
Quadratic Bezier (Q) to x, y + r via x, y
Line to (L) / Vertical line (V) to the next point
For example, if the radius is 20 and the corner is in the point 100,100 the path will be H 80,100 Q 100,100 100,120 V ...

Interpreting svg paths created in inkscape

I am trying to regenerate svg paths in a script using xml files created by inkscape. I understand how to interpret the d tag in the xml file, but I am having trouble with the transformations baked into the file.
My test file has a path on it that I simplified down to a small triangle to make it easier to work with. The simple svg file from inkscape looks like this:
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
version="1.1"
id="svg6530"
viewBox="0 0 33 134"
height="134"
width="33">
<defs
id="defs6532" />
<metadata
id="metadata6535">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
style="display:inline"
transform="translate(0,-918.36216)"
id="layer1">
<path
id="path7149"
d="m 0.10475398,1040.2201 1.79370822,0 1.4230759,-1.6612"
style="fill:#000000" />
</g>
</svg>
The path is relative (lowercase m)
It has a transform:translate of (0,-918.36216) applied to it.
The view box is 0 0 33 134
height = 134
width = 33
the path is: "m 0.10475398,1040.2201 1.79370822,0 1.4230759,-1.6612"
All units are pixels.
If I look at the path in inkscape I see the following 3 points in the document:
0.105,12.142
1.898,12.142
3.322,13.80
These are the three points I'd like to recreate in a script.
If I start with
"M 0.105,12.142 1.898,12.142 3.322,13.80"
I get exactly what I want, but I can't figure out how to get to this from:
d="m 0.10475398,1040.2201 1.79370822,0 1.4230759,-1.6612"
The path string:
m 0.10475398,1040.2201 1.79370822,0 1.4230759,-1.6612
is equivalent to
M 0.10475398,1040.2201 l 1.79370822,0 l 1.4230759,-1.6612
or a move and two relative lines.
If we convert the relative line coordinates to absolute (and round to three decimal places for simplicity) we get:
M 0.105,1040.220 L (0.105+1.794),(1040.220+0) L 0.105+1.794+1.423),(1040.220+0-1.661)
or
M 0.105,1040.220 L 1.899,1040.220 L 3.322,1038.559
Now you are also applying a transform to the path. It's a translation by (0,-918.362). If if we now apply that to your path, we get:
M 0.105,(1040.220 - 918.362) L 1.899,(1040.220 - 918.362) L 3.322,(1038.559 - 918.362)
or:
M 0.105,121.858 L 1.899,121.858 L 3.322,120.197
In its UI, Inkscape is flipping the Y coordinates from the SVG convention, where Y=0 is at the top, to the cartesian convention where Y=0 is at the bottom.
So for the final step, if we subtract all the Y coordinates from 134 (the height of the document), we get:
M 0.105,12.142 L 1.899,12.142 L 3.322,13.807
Which is the string you were after (with minor rounding differences).
tl;dr: Convert to absolute coordinates, apply the transform, flip the Y coords.

Line chart animation in svg using jquery

I want to do line chart (path animation) individually not whole chart area. Please refer below JS fiddle
http://jsfiddle.net/2LEyb/
currently i have implemented the animation for whole chart area i.e. area rect initially zero and then i will increase the width step by step. it will affect other charts because column chart also with in chart area but it will do animation link line. Please refer below code snippet currently i have done.
doAnimation: function () {
var clipRect = $(this.chartObj.gSeriesEle).find("#" + this.chartObj.svgObject.id + "_ChartAreaClipRect");
$(clipRect).animate(
{ width: this.chartObj.model.m_AreaBounds.Width },
{
duration: 2000,
step: function (now, fx) {
$(clipRect).attr("width", now);
}
});
this.chartObj.model.Animation=false;
},
i need to get the individual "line" path rectangle and then i want to increase the width of individual line path rectangle step by step in jquery animate instead increasing the width of entire chart area.
how to calculate the rectangle for individual path and then increase the width of rectangle.
<g id="container_svg_SeriesGroup_0" transform="translate(144,432)"><path id="container_svg_John_0" fill="none" stroke-width="3" stroke="url(#container_svg_John0Gradient)" stroke-linecap="butt" stroke-linejoin="round" d="M -2454.7999999999997 -125.8888888888889 L 0 0 M 0 0 L 258.40000000000003 -45.77777777777778 M 258.40000000000003 -45.77777777777778 L 516.8000000000001 -11.444444444444445 M 516.8000000000001 -11.444444444444445 L 646 -183.11111111111111 "/></g>
Thanks,
Siva

Resources