Chartist.js, SVG circle element is not showing up in the container - svg

I'm using Chartist.js to work on a pie chart.
I am trying to add a simple circle in the background, but for some reason the circle element just ends up in the top left corner with 0x0 dimensions.
Here is the SVG:
<div class="contents" id="chart-container">
<svg xmlns:ct="http://gionkunz.github.com/chartist-js/ct" width="100%" height="100%" class="ct-chart-pie" style="width: 100%; height: 100%;" viewBox="0 0 359 219">
<g class="basecircle">
<cirlce class="basecircleelement" cx="10" cy="10" fill="black" cr="181.76999999999998">
</cirlce>
</g>
<g ct:series-name="value" class="ct-series ct-series-a">
<path d="M147.071,10.108A104.375,104.375,0,1,0,179.325,5L179.325,109.375Z" class="ct-slice-pie" ct:value="95" style="fill: gray; stroke: white; stroke-width: #fff;"></path>
</g>
<g ct:series-name="rest" class="ct-series ct-series-b">
<path d="M179.325,5A104.375,104.375,0,0,0,146.725,10.222L179.325,109.375Z" class="ct-slice-pie" ct:value="5" style="fill: transparent;"></path>
</g>
</svg>
</div>
I expected the "g.basecircle" and "circle.basecircleelement" should be showing up(It's supposed to be in the center of the container but I didn't calculate the cx and cy yet) with radius of 181.76999999999998 -this value is calculated to be half of the clientWidth-, but it ends up in the upper left corner of the SVG element with dimension of 0 x 0
Is there anything I am missing?

this value is calculated to be half of the clientWidth-, but it ends
up in the upper left corner of the SVG element with dimension of 0 x 0
Typos must be fixed:
circle instead ofcirlce
Radius designation -r instead ofcr
Svg canvas borders are bounded by a red square style = "border: 1px solid red;"
It is very convenient to visually see the borders of SVG. When you finish debugging your application, this line can be removed.
To place the black circle in the center of the svg canvas, you need to set the coordinates cx =" 359/2 " cy =" 219/2 "
<div class="contents" id="chart-container">
<svg xmlns:ct="http://gionkunz.github.com/chartist-js/ct" width="100%" height="100%" class="ct-chart-pie" viewBox="0 0 359 219" style="border:1px solid red;">
<g class="basecircle">
<circle class="basecircleelement" cx="179.5" cy="109.5" fill="black" r="179.5">
</circle>
</g>
<g ct:series-name="value" class="ct-series ct-series-a">
<path d="M147.071,10.108A104.375,104.375,0,1,0,179.325,5L179.325,109.375Z" class="ct-slice-pie" ct:value="95" style="fill: gray; stroke: white; stroke-width: #fff;"></path>
</g>
<g ct:series-name="rest" class="ct-series ct-series-b">
<path d="M179.325,5A104.375,104.375,0,0,0,146.725,10.222L179.325,109.375Z" class="ct-slice-pie" ct:value="5" style="fill: transparent;"></path>
</g>
</svg>
</div>
Update
If you want the black circle to not form and completely fit on the canvas svg you need to set its radius equal to half the height of the canvas r= 219 / 2 = 109.5
<div class="contents" id="chart-container">
<svg xmlns:ct="http://gionkunz.github.com/chartist-js/ct" width="100%" height="100%" class="ct-chart-pie" viewBox="0 0 359 219" style="border:1px solid red;">
<g class="basecircle">
<circle class="basecircleelement" cx="179.5" cy="109.5" fill="black" r="109.5">
</circle>
</g>
<g ct:series-name="value" class="ct-series ct-series-a">
<path d="M147.071,10.108A104.375,104.375,0,1,0,179.325,5L179.325,109.375Z" class="ct-slice-pie" ct:value="95" style="fill: gray; stroke: white; stroke-width: #fff;"></path>
</g>
<g ct:series-name="rest" class="ct-series ct-series-b">
<path d="M179.325,5A104.375,104.375,0,0,0,146.725,10.222L179.325,109.375Z" class="ct-slice-pie" ct:value="5" style="fill: transparent;"></path>
</g>
</svg>
</div>

Related

<svg> <use> only displays in positive quadrant?

Failing to understand why only one quadrant is showing red color rather than the entire viewable area of the SVG? The same rect outside of use covers the entire area, what am I missing?
svg {
height: 50vmin;
border: solid 1px black;
transform: scale(1, -1);
}
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-1 -1 2 2">
<defs>
<symbol id="quadrant">
<rect x="-1" y="-1" width="2" height="2" fill="#f00"/>
</symbol>
</defs>
<rect x="-1" y="-1" width="2" height="2" fill="#0f0"/>
<use xlink:href="#quadrant" />
</svg>
The use element doesn't have x or y attributes so it starts at 0,0 and not -1, -1 as the prior rect does.
The symbol doesn't have a viewBox so the symbol displays from 0, 0 to 100%, 100% i.e. 1, 1 (since that's where the root viewBox says 100% maps to).
svg {
height: 50vmin;
border: solid 1px black;
transform: scale(1, -1);
}
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-1 -1 2 2">
<defs>
<symbol id="quadrant" viewBox="-1 -1 2 2">
<rect x="-1" y="-1" width="2" height="2" fill="#f00"/>
</symbol>
</defs>
<rect x="-1" y="-1" width="2" height="2" fill="#0f0"/>
<use x="-1" y="-1" xlink:href="#quadrant" />
</svg>
The alternative would be to just make the symbol's overflow: visible but I wouldn't recommend it as overflow: visible has terrible performance.

SVG Use -- Unable to repostion polygon after rotation

I am trying to create a 16 point star inside a circle using SVG and pure CSS -- no JS!
My strategy is to create 16 equilateral triangles (via Defs and Use, to keep it DRY), rotating each Use iteration by 22.5 degrees.
My problem is that when I apply the rotate() transform to the second triangle, SVG changes the center point of the triangle -- which CSS3 does not (it rotates around a fixed axis).
I have tried adding x and y parameters, adding a class and doing a translate() transform, doing that inline... nothing works -- I just cant figure out how to move the triangle back into position (with a rotation) inside the circle (centered at 150, 150 I reckon).
Any help would be appreciated. Here is the SVG line of code that I am having trouble with.
<use xlink:href="#triangle" style="transform: rotate(22.5deg);" />
You can see it in action here.
<style > .toile {
display: flex;
flex-direction: column;
max-width: 400px;
max-height: 800px;
justify-content: center;
align-items: center;
/* centers outer containing element (the circle) horizontally & vertically */
border: 5px #009000;
/* green */
border-style: groove;
background-color: #f9e4b7;
margin: 0 auto;
/* centers surface on a page */
}
<div class="toile">
<svg>
<defs>
<pattern id="grid" width="15" height="15" patternUnits="userSpaceOnUse">
<rect fill="white" x="0" y="0" width="14" height="14"/>
<rect fill="#009000" x="14" y="0" width="1" height="14"/>
<rect fill="#009000" x="0" y="14" width="14" height="5"/>
</pattern>
<g id="triangle">
<svg>
<polygon points="150,18 200,100 100,100"
style="stroke:#009000;stroke-width:1; fill:#afeeee; opacity:.7" />
</svg>
</g>
</defs>
<rect fill="url(#grid)" x="0" y="0" width="100%" height="100%" />
<svg viewBox="0 100 400 400" stroke="#009000" stroke-width=".5" width="300" height="300" class="cercle">
<circle cx="50%" cy="50%" r="75" fill="transparent" /> </svg>
<svg viewBox="0 100 400 400" stroke="#ce2029" stroke-width=".5" width="300" height="300">
<circle cx="50%" cy="50%" r="2" fill="#ce2029" /> </svg>
<use xlink:href="#triangle" />
<use xlink:href="#triangle" style="transform: rotate(22.5deg);" />
</svg>
</div>
Thank you for any solution to this problem; I just can't figure it out! Please no JS solutions!
UPDATE:
I've changed the 16-point gon to a 15 point one, as for some reason a series of 22.5 degree rotations create an unbalanced hexadecagon. I got rid of the red circle center point, and the background grid, and added SVG animation. Here is the (final) working example.
Sorry about the CodePen but I am trying to figure out how to make snippets work for an entire HTML/CSS/SVG program.
This is one way of doing it:
First I simplified your code. Unless you have a good reason to do it like this, it's always better to keep things simple.
I calculated the points tor the triangle around the center of the svg canvas:
<polygon id="triangle" points="200,125 264.95,237.5 135.05,237.5"
I rotate the triangle using svg transforms: transform="rotate(22.5,200,200)"
The first value is the rotation in degs and next you have the x and y of the rotation center.
As it comes out with SVG transforms you don't have IE issues. Please read this article about Transforms on SVG Elements
.toile {
display: flex;
flex-direction: column;
max-width: 400px;
max-height: 800px;
justify-content: center;
align-items: center;
/* centers outer containing element (the circle) horizontally & vertically */
border: 5px #009000;
/* green */
border-style: groove;
background-color: #f9e4b7;
margin: 0 auto;
/* centers surface on a page */
}
<div class="toile">
<svg viewBox="0 0 400 400" stroke="#009000" stroke-width=".5" width="300" height="300" >
<defs>
<pattern id="grid" width="15" height="15" patternUnits="userSpaceOnUse">
<rect fill="white" x="0" y="0" width="14" height="14"/>
<rect fill="#009000" x="14" y="0" width="1" height="14"/>
<rect fill="#009000" x="0" y="14" width="14" height="5"/>
</pattern>
<polygon id="triangle" points="200,125 264.95,237.5 135.05,237.5"
style="stroke:#009000;stroke-width:1; fill:#afeeee; opacity:.7" />
</defs>
<rect fill="url(#grid)" x="0" y="0" width="100%" height="100%" />
<circle class="cercle" cx="50%" cy="50%" r="75" fill="transparent" />
<circle cx="50%" cy="50%" r="2" fill="#ce2029" />
<use xlink:href="#triangle" />
<use xlink:href="#triangle" transform="rotate(22.5,200,200)" />
</svg>
</div>
UPDATE
To calculate the points for the triangle you may use javascript. In the case of a regular polygon like a triangle all 3 vertices are on a circumscribed circle at a 2*Math.PI/3 angle one from each other. I'm starting with an offset of -Math.PI/2 (-90 degs) for the first vertex.
// the center of the SVG canvas calculated from the values of the viewBox attribute. Alternatively you can choose a different point
let c = {x:200,y:200}
let radius = 75;
let points = [];
for(let a = -Math.PI/2; a < 3*Math.PI/2; a+= 2*Math.PI/3){
let x = c.x + radius*Math.cos(a);
let y = c.y + radius*Math.sin(a);
points.push(x);
points.push(y);
}
tri.setAttributeNS(null, "points", points.join());
svg{border:1px solid;height:90vh}
<svg viewBox="0 0 400 400">
<polygon id="tri" />
</svg>

Centering text in SVG path

I am trying to center horizontally and vertically text in path with the
startOffset and text-anchor attributes, but centering doesn't work.
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
<svg id="line-vis" width="640" height="300" viewBox="0 0 640 300" preserveAspectRatio="none" style="cursor: pointer; overflow: visible; position: relative;">
<g>
<g class="line" pointer-events="none" transform="translate(365.1979064941406,134.5977783203125)">
<path id="tooltip-line-vis" d="M120 60 L120 0 L0 0 L0 60 L54 60 L60 66 66 60" fill="#FFFFFF" stroke="#D2DBE9" transform="translate(-60,-75)" style="opacity: 1;"></path>
<text text-anchor="middle" style="opacity: 1;">
<textPath class="yLabelVal" xlink:href="#tooltip-line-vis" startOffset="50%" style="opacity: 1;font-size: 16px;font-weight: 500;line-height: 20px;fill: rgb(8, 40, 101);">$5,104.90</textPath>
</text>
</g>
<rect width="640" height="300" fill="none" pointer-events="all"></rect>
</g>
</svg>
In order to get what you need I've changed the points of the path. Now the path begins at 0,0. Also I've removed all the transforms. If you need your path in a different position you may use transforms on #tooltip as I did in my code.
Please note the attribute dy="35" for the text. This is moving your text upward & in the center.
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
<svg id="line-vis" width="640" height="300" viewBox="0 0 640 300" preserveAspectRatio="none" style="cursor: pointer; overflow: visible; position: relative;">
<g id="tooltip" transform="translate(50,10)">
<g class="line" pointer-events="none" >
<!--<path id="tooltip-line-vis" d="M0,60 L54,60 60,66 66,60 120,60 120,0 0,0 0,60" stroke="#D2DBE9" style="opacity: 1;"></path>-->
<path id="tooltip-line-vis" d="M0,0 L120,0 120,60 66,60 60,66 54,60 0,60 0,0" stroke="#D2DBE9" style="opacity: 1;"></path>
<text text-anchor="middle" style="opacity: 1;" fill="gold" dy="35">
<textPath class="yLabelVal" xlink:href="#tooltip-line-vis" startOffset="60" >$5,104.90</textPath>
</text>
</g>
<rect width="640" height="300" fill="none" pointer-events="all"></rect>
</g>
</svg>

How to set the common background In SVG?

How to set the common background for all svg?
<svg>
<g id="dsfdf">
-----------
</g>
<g id="dsfgfdgdf">
-----------
</g>
</svg>
How to set the common background for G tags. Please any one help
By specifying the fill in the svg element itself, you can always specify another color in the containing elements, like the red circle on the first position in the sample.
<svg fill="green" id="svg1" xmlns="http://www.w3.org/2000/svg" style="width: 3.5in; height: 1in">
<circle id="circle1" r="30" cx="100" cy="34" style="fill: red; stroke: blue; stroke-width: 2"/>
<circle id="circle2" r="30" cx="200" cy="34"/>
<circle id="circle3" r="30" cx="300" cy="34"/>
</svg>

SVG Scaling for Line Chart

I would like to have a responsive SVG for a line chart. This should have three areas that combine fixed and scalable elements.
The pink element should be a scaling child svg (I need to define a child coordinate system for drawing a polyline), the grey bits are used for drawing axis, and should have limited scaling, since text should render in a relativaly simple font.
Finally, I would like to do this without JavaScript.
Some of the problems I have faced and which might help with a solution:
If I could give the <svg> element that is the pink area the following x="20px" width="calc(100% - 20px)" height="calc(100% - 20px)", this would largely solve my problem, but this triggers a parse error. I haven't managed to get any variations using CSS to work either.
(Another answer would be well worth it if someone can do this with "pure" svg).
I hacked it using HTML layout and multiple SVGs. This is the code I am using:
<div style="width: 100%; height: 300px">
<!-- The Y axis -->
<svg style="width: 40px; height: calc(100% - 20px); float: left;">
<g class="yaxis">
<line x1="99%" x2="99%" y1="0%" y2="100%" stroke="black" vector-effect="non-scaling-stroke"></line>
<g class="tick tick-yaxis">
<text x="99%" y="100%" text-anchor="end">$0.0</text>
<line x1="98%" x2="100%" y1="100%" y2="100%" stroke="black"></line>
</g>
<!-- ... -->
</g>
</g>
</svg>
<!-- The actual chart -->
<svg viewBox="0 0 100 100" preserveAspectRatio="none" style="width: calc(100% - 40px); height: calc(100% - 20px); float: left;">
<polyline points="0,50 45.46574414322495,0 65,100 75,100 89.52839708894965,0 91.10141646672459,100 98.21939424857649,0 100,100"
vector-effect="non-scaling-stroke" stroke="red" fill="none"></polyline>
</svg>
<!-- x axis -->
<svg style="clear: both; margin-left: 40px; width: calc(100% - 40px); height: 20px;">
<g class="xaxis">
<line x1="0%" x2="100%" y1="1%" y2="1%" stroke="black" vector-effect="non-scaling-stroke"></line>
<g class="tick tick-xaxis">
<text x="5%" y="100%" text-anchor="middle">28 Feb 2011</text>
<line x1="5%" x2="5%" y1="94%" y2="96%" stroke="black"></line>
</g>
</svg>
</div>
Add vector-effect="non-scaling-stroke" to your path
div {
width: 100%;
height: 180px;
background: #f5f5f5;
background: none;
}
svg > path{
stroke: teal;
stroke-width: 3;
fill: none;
}
#SVGgrid {opacity: 0.15;}
.SVGLines{filter: drop-shadow(0.9px 4px 0.6px rgba(0,0,0,0.15));}
.red {stroke: #fa7a17;}
.blue {stroke: #4185f5;}
.yellow {stroke: #fbbd04;}
<div>
<svg viewBox="0 0 52 100" preserveAspectRatio="none" style="width: 100%; height: 100%;">
<defs>
<pattern id="SVGGT" patternUnits="userSpaceOnUse" width="1" height="4">
<line x1="1" y1="0" x2="1" y2="10" vector-effect="non-scaling-stroke" stroke="black"/>
<line x1="0" y1="4" x2="4" y2="4" vector-effect="non-scaling-stroke" stroke="black"/>
</pattern>
</defs>
<rect id="SVGgrid" width="100%" height="100%" fill="url(#SVGGT)" />
<path class="SVGLines red" vector-effect="non-scaling-stroke" d="
M 0.1,59
S 0.5,60 1,59
S 1.5,59 2,57
S 2.5,58 3,55
S 3.5,56 4,54
S 4.5,55 5,53
S 5.5,53 6,52
S 6.5,51 7,51
S 7.5,50 8,49
S 8.5,47 9,48
S 9.5,46 10,46
S 10.5,42 11,43
S 11.5,43 12,42
S 12.5,40 13,39
S 13.5,36 14,36
S 14.5,36 15,35
S 15.5,33 16,33
S 16.5,33 17,30
S 17.5,25 18,27
S 18.5,28 19,26
S 19.5,26 20,24
S 20.5,23 21,21
S 21.5,22 22,20
S 22.5,22 23,23
S 23.5,25 24,26
S 24.5,28 25,27
S 25.5,27 26,28
S 26.5,29 27,29
S 27.5,34 28,31
S 28.5,35 29,34
S 29.5,37 30,35
S 30.5,38 31,37
S 31.5,41 32,38
S 32.5,38 33,40
S 33.5,41 34,41
S 34.5,44 35,44
S 35.5,50 36,47
S 36.5,53 37,50
S 37.5,53 38,53
S 38.5,57 39,56
S 39.5,56 40,55
S 40.5,52 41,53
S 41.5,54 42,51
S 42.5,50 43,50
S 43.5,47 44,47
S 44.5,49 45,46
S 45.5,46 46,45
S 46.5,45 47,42
S 47.5,41 48,41
S 48.5,43 49,40
S 49.5,36 50,38
S 50.5,34 51,36
S 51.5,34 52,33
" stroke="black" fill="transparent" stroke-linecap="round"/>
<path class="SVGLines blue" vector-effect="non-scaling-stroke" d="
M 0.1,25
S 0.5,26 1,25
S 1.5,26 2,27
S 2.5,31 3,30
S 3.5,31 4,31
S 4.5,32 5,34
S 5.5,36 6,35
S 6.5,38 7,36
S 7.5,38 8,38
S 8.5,41 9,41
S 9.5,39 10,39
S 10.5,34 11,36
S 11.5,35 12,33
S 12.5,33 13,32
S 13.5,33 14,30
S 14.5,29 15,28
S 15.5,26 16,27
S 16.5,25 17,26
S 17.5,23 18,25
S 18.5,25 19,22
S 19.5,23 20,21
S 20.5,20 21,19
S 21.5,17 22,16
S 22.5,17 23,14
S 23.5,14 24,11
S 24.5,14 25,13
S 25.5,18 26,16
S 26.5,18 27,19
S 27.5,25 28,22
S 28.5,17 29,19
S 29.5,19 30,17
S 30.5,14 31,16
S 31.5,14 32,14
S 32.5,13 33,13
S 33.5,11 34,10
S 34.5,13 35,11
S 35.5,10 36,12
S 36.5,15 37,14
S 37.5,17 38,16
S 38.5,14 39,13
S 39.5,16 40,16
S 40.5,19 41,18
S 41.5,21 42,19
S 42.5,19 43,20
S 43.5,20 44,21
S 44.5,24 45,23
S 45.5,22 46,24
S 46.5,28 47,27
S 47.5,28 48,28
S 48.5,29 49,29
S 49.5,34 50,31
S 50.5,36 51,33
S 51.5,34 52,34
" stroke="black" fill="transparent" stroke-linecap="round"/>
<path class="SVGLines yellow" vector-effect="non-scaling-stroke" d="
M 0.1,75
S 0.5,78 1,75
S 1.5,76 2,78
S 2.5,84 3,81
S 3.5,81 4,82
S 4.5,86 5,85
S 5.5,86 6,87
S 6.5,89 7,90
S 7.5,91 8,88
S 8.5,85 9,86
S 9.5,85 10,83
S 10.5,79 11,81
S 11.5,79 12,78
S 12.5,78 13,75
S 13.5,72 14,72
S 14.5,68 15,70
S 15.5,65 16,67
S 16.5,65 17,66
S 17.5,68 18,65
S 18.5,66 19,63
S 19.5,62 20,61
S 20.5,60 21,60
S 21.5,55 22,57
S 22.5,59 23,56
S 23.5,52 24,53
S 24.5,57 25,54
S 25.5,59 26,56
S 26.5,60 27,59
S 27.5,63 28,60
S 28.5,62 29,62
S 29.5,58 30,60
S 30.5,57 31,57
S 31.5,52 32,54
S 32.5,55 33,52
S 33.5,51 34,51
S 34.5,50 35,48
S 35.5,46 36,46
S 36.5,47 37,44
S 37.5,42 38,41
S 38.5,41 39,39
S 39.5,37 40,36
S 40.5,37 41,34
S 41.5,31 42,32
S 42.5,31 43,30
S 43.5,28 44,27
S 44.5,25 45,26
S 45.5,27 46,24
S 46.5,23 47,21
S 47.5,21 48,18
S 48.5,20 49,17
S 49.5,13 50,15
S 50.5,16 51,14
S 51.5,11 52,12
" stroke="black" fill="transparent" stroke-linecap="round"/>
</svg>
</div>

Resources