JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
HTML Settings
Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.
<?xml version="1.0" standalone="no" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/PR-SVG-20010719/DTD/svg10.dtd">
<html>
<body>
<center>
<h1>Calculating a pie | umbrella chart in SVG</h1>
<div class="wrapper"> <!-- wrapper start -->
<div class="controller"> <!-- controller start -->
<form id="cform">
<label>No. of slices (<span id="myNoOfSlices" class="lblNoSlices">9 pcs</span>)</label><br>
<input id="rns" type="range" min="1" max="60" step="1" value="9" language="javascript" onchange="fCalculatePie()" oninput="fCalculatePie()"/><br>
<label>Radius (<span id="myRadius" class="lblRadius">180°</span>)</label><br>
<input id="rradius" type="range" min="10" max="199" step="1" value="180" language="javascript" onchange="fCalculatePie()" oninput="fCalculatePie()"/><br>
<label>Fill-opacity (<span id="myOpacity" class="lblOpacity">100%</span>)</label><br>
<input id="ropacity" type="range" min="0" max="1" step="0.1" value="1" language="javascript" onchange="fCalculatePie()" oninput="fCalculatePie()"/><br>
<label>Stroke-width (<span id="myStrokeWidth" class="lblStrokeWidth">1px</span>)</label><br>
<input id="rstrokewidth" type="range" min="0" max="3" step="0.25" value="1" language="javascript" onchange="fCalculatePie()" oninput="fCalculatePie()"/><br>
<label>Stroke-color (<span id="myStrokeColor" class="lblStrokeColor">◼ hsl(0,100%,50%)</span>)</label><br>
<input id="rstrokecolor" type="range" min="0" max="360" step="1" value="0" language="javascript" onchange="fCalculatePie()" oninput="fCalculatePie()"/><br><br>
<label>Rotation (off | clockwise | counterclockwise)</label><br>
<input id="chkRotation" type="range" min="0" max="2" step="1" value="1" language="javascript" onchange="fCalculatePie()" oninput="fCalculatePie()" style="width:20%;"/>
<br>
<label>Grid (off | on)</label><br>
<input id="chkGrid" type="range" min="0" max="1" step="1" value="1" language="javascript" onchange="fCalculatePie()" oninput="fCalculatePie()" style="width:10%;"/>
<br>
<label>Arc (convex | concave)</label><br>
<input id="chkArc" type="range" min="0" max="1" step="1" value="0" language="javascript" onchange="fCalculatePie()" oninput="fCalculatePie()" style="width:10%;"/><br>
<label id="sb" class="sb"></label>
</form>
</div> <!-- controller end -->
<!-- preview start -->
<div id="preview" class="preview">
<!-- copy from https://codepen.io/itsnorbat/full/bVYoBa -->
<svg xmlns="http://www.w3.org/2000/svg" width="402" height="402">
<defs>
<pattern id="karo" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse">
<rect x="0" y="0" width="20" height="20" fill="none" stroke="#fff" stroke-width="0.5"/>
</pattern>
</defs>
<rect id="grid" x="0" y="0" width="401" height="401" fill="url(#karo)" stroke="#fff" stroke-width="0"/>
<g id="piechart" class="piechart">
<path d="M200,200L380,200 A180,180 1 0,0 337.9,315.7z" style="fill: rgb(255, 0, 0); fill-opacity: 1; stroke: rgb(255, 0, 0); stroke-width: 0px;"></path>
<path d="M200,200L337.9,315.7 A180,180 1 0,0 231.3,377.3z" style="fill: rgb(255, 170, 0); fill-opacity: 1; stroke: rgb(255, 0, 0); stroke-width: 0px;"></path>
<path d="M200,200L231.3,377.3 A180,180 1 0,0 110,355.9z" style="fill: rgb(170, 255, 0); fill-opacity: 1; stroke: rgb(255, 0, 0); stroke-width: 0px;"></path>
<path d="M200,200L110,355.9 A180,180 1 0,0 30.9,261.6z" style="fill: rgb(0, 255, 0); fill-opacity: 1; stroke: rgb(255, 0, 0); stroke-width: 0px;"></path>
<path d="M200,200L30.9,261.6 A180,180 1 0,0 30.9,138.4z" style="fill: rgb(0, 255, 170); fill-opacity: 1; stroke: rgb(255, 0, 0); stroke-width: 0px;"></path>
<path d="M200,200L30.9,138.4 A180,180 1 0,0 110,44.1z" style="fill: rgb(0, 170, 255); fill-opacity: 1; stroke: rgb(255, 0, 0); stroke-width: 0px;"></path>
<path d="M200,200L110,44.1 A180,180 1 0,0 231.3,22.7z" style="fill: rgb(0, 0, 255); fill-opacity: 1; stroke: rgb(255, 0, 0); stroke-width: 0px;"></path>
<path d="M200,200L231.3,22.7 A180,180 1 0,0 337.9,84.3z" style="fill: rgb(170, 0, 255); fill-opacity: 1; stroke: rgb(255, 0, 0); stroke-width: 0px;"></path>
<path d="M200,200L337.9,84.3 A180,180 1 0,0 380,200z" style="fill: rgb(255, 0, 170); fill-opacity: 1; stroke: rgb(255, 0, 0); stroke-width: 0px;"></path>
<!-- <animateTransform attributeType="xml" attributeName="transform" type="rotate" from="0,200,200" to="360,200,200" begin="0s" dur="6s" repeatCount="indefinite"/> -->
</g>
<g id="calcPie" class="piechartcw"></g>
</svg>
</div> <!-- preview end -->
<div style="clear:both;"></div>
<p class="myp">I uncomment the SMIL animations and replace them with CSS transforms/keyframes animations because of the intent of deprecation of SMIL in future browsers (<a href="https://www.chromestatus.com/feature/5371475380928512" target="_blank">further details</a>).</p>
<p class="myp">Therefore I've updated the <em>addRotation</em> function to toggle between the three rotation states of the calculated pie (#calcPie).</p>
<p class="myp2">If you like your custom pie you can select and copy the code from the following textarea but please keep in mind that the external CSS especially the animation is not included.<br>
You can copy the classes <strong>piechartcw</strong> and <strong>piechartccw</strong> and the <strong>keyframes</strong> from the CSS in this pen to your inline or external stylesheet and your animations will work instantly.</p>
<textarea id="svgsource" rows="4" cols="120"></textarea><br><br>
</div> <!-- wrapper end -->
</center>
</body>
</script>
</html>
body{
background-color:#000;
padding:0px;
margin:0px;
font-family: Raleway, sans-serif;
}
h1{
font-weight: 200;
color: #fc3;
}
h2{
font-weight: 500;
}
a{
color: white;
font-style: italic;
text-decoration: none;
}
input{
width: 100%;
}
.myp{
color: lightgrey;
text-align: left;
}
.myp2{
color: grey;
font-style: italic;
text-align: left;
}
/* controller */
.wrapper{
width: 846px;
height: 405px;
}
.controller{
width: 382px;
height: 382px;
background-color: #000;
border: 1px solid white;
margin-right: 40px;
float:left;
text-align: left;
padding: 10px;
color: lightgrey;
}
.preview{
width: 402px;
height: 402px;
background-color: #000;
border: 0px solid white;
float:left;
}
.lblNoSlices{
color: white;
font-weight: 500;
}
.lblStrokeColor{
color: hsl(0,100%,50%);
}
.sb{
color: yellow;
opacity: 0.6;
}
/* using CSS instead of SMIL for the animations */
.piechart{
-webkit-animation: rotatecw 15s 0s linear infinite;
animation: rotatecw 15s 0s linear infinite;
}
.piechartcw{
-webkit-animation: rotatecw 3s 0s linear infinite;
animation: rotatecw 3s 0s linear infinite;
}
.piechartccw{
-webkit-animation: rotateccw 3s 0s linear infinite;
animation: rotateccw 3s 0s linear infinite;
}
/* rotation clockwise (cw) */
@-webkit-keyframes rotatecw {
0% {
-webkit-transform-origin: 50% 50%;
-webkit-transform: rotate(0deg);
transform-origin: 50% 50%;
transform: rotate(0deg); }
100% {
-webkit-transform-origin: 50% 50%;
-webkit-transform: rotate(360deg);
transform-origin: 50% 50%;
transform: rotate(360deg);
}
}
/* rotation counterclockwise (ccw) */
@-webkit-keyframes rotateccw {
0% {
-webkit-transform-origin: 50% 50%;
-webkit-transform: rotate(360deg);
transform-origin: 50% 50%;
transform: rotate(360deg);
}
100% {
-webkit-transform-origin: 50% 50%;
-webkit-transform: rotate(0deg);
transform-origin: 50% 50%;
transform: rotate(0deg);
}
}
function fCalculatePie() {
var originX = 200;
var originY = 200;
var radius = document.getElementById('rradius').value;
var nos = document.getElementById('rns').value;
var myOpacity = document.getElementById('ropacity').value;
var myStrokeWidth = document.getElementById('rstrokewidth').value;
var myStrokeColor = document.getElementById('rstrokecolor').value;
var hslStokeColor = "hsl("+myStrokeColor+", 100%, 50%)";
var rotationonoff = document.getElementById('chkRotation').value;
var myGrid = document.getElementById('grid');
var gridonoff = document.getElementById('chkGrid').value;
var acc = document.getElementById('chkArc').value;
document.getElementById('myRadius').innerHTML = radius + '°';
document.getElementById('myNoOfSlices').innerHTML = nos + ' pcs';
document.getElementById('myOpacity').innerHTML = myOpacity*100 + '%';
document.getElementById('myStrokeWidth').innerHTML = myStrokeWidth + 'px';
document.getElementById('myStrokeColor').innerHTML = "◼ "+hslStokeColor
document.getElementById('myStrokeColor').style.color = hslStokeColor;
var angle = 360/nos;
var sa = rou(angle, 10); //sa for shorten angle
//document.getElementById('calcAngle').value = sa;
remAllPieces('piechart');
remAllPieces('calcPie');
for (var i = 0; i < nos; i++) {
addPiece(originX, originY, radius, i*sa, ((i+1)*sa), myOpacity, hslStokeColor, myStrokeWidth, acc);
};
addRotation(rotationonoff);
// former version for SMIL in SVG
/*if(rotationonoff==1){
addRotation(rotationonoff);
} else if(rotationonoff==2){
addRotation(rotationonoff);
}*/
if(gridonoff==1){
myGrid.setAttribute("fill","url(#karo)");
} else{
myGrid.setAttribute("fill","none");
}
csvg2ta("Norbert Mesch");
};
function addPiece(ox, oy, r, sa, ea, opacity, strokecolor, strokewidth, arccc){
var myPiece = document.getElementById('calcPie');
var newElement = document.createElementNS("http://www.w3.org/2000/svg", 'path'); //Create a path in SVG's namespace
// starting point calculation
var spx = ox + rou(r*Math.cos(sa * Math.PI / 180),10)*(1);
var spy = oy + rou(r*Math.sin(sa * Math.PI / 180),10)*(1);
// ending point calculation
var epx = ox + rou(r*Math.cos(ea * Math.PI / 180),10)*(1);
var epy = oy + rou(r*Math.sin(ea * Math.PI / 180),10)*(1);
// creating hsl color attribute
var mycolor = "hsl("+sa+", 100%, 50%)";
// create path element
var myPath = "M"+ ox +","+ oy + "L"+ spx +","+ spy +" A"+r+","+r+" 1 0,"+ arccc +" "+ epx +","+ epy +"z";
//a little bit styling
newElement.setAttribute("d", myPath);
newElement.style.fill = mycolor;
newElement.style['fill-opacity'] = opacity;
newElement.style.stroke = strokecolor;
newElement.style['stroke-width'] = strokewidth;
//Append the element to the DOM
myPiece.appendChild(newElement);
};
/* remomes all pieces of the calculated pie */
function remAllPieces(pelem){
var myNode = document.getElementById(pelem);
while (myNode.firstChild) {
myNode.removeChild(myNode.firstChild);
}
};
/* toggle between the rotation states no(0), clockwise(1) and counterclockwise(2) */
function addRotation(direction){
//document.getElementById("sb").innerHTML = "Ausgabe: " + direction;
if(direction==0){
document.getElementById("calcPie").setAttribute("class","");
}else if(direction==1){
document.getElementById("calcPie").setAttribute("class","piechartcw");
}else if(direction==2) {
document.getElementById("calcPie").setAttribute("class","piechartccw");
}
};
/*function addRotation(direction){
var myRotation = document.getElementById('calcPie');
var newRotation = document.createElementNS("http://www.w3.org/2000/svg", 'animateTransform');
newRotation.setAttribute("attributeType", "xml");
newRotation.setAttribute("attributeName", "transform");
newRotation.setAttribute("type", "rotate");
if(direction==1){
newRotation.setAttribute("from", "0,200,200");
newRotation.setAttribute("to", "360,200,200");
}else{
newRotation.setAttribute("from", "360,200,200");
newRotation.setAttribute("to", "0,200,200");
}
newRotation.setAttribute("begin", "1s");
newRotation.setAttribute("dur", "6s");
newRotation.setAttribute("repeatCount", "indefinite");
myRotation.appendChild(newRotation);
};*/
// function to round the number (n) with the number of decimal places (dp) e.g.
// 10 for one decimal places 106.1 or
// 100 for two decimal places 106.13 and so on
function rou(n,dp) {
n = (Math.round(n * dp) / dp);
return n;
};
// copy pie svg into textarea
function csvg2ta(quelle){
var svgs = document.getElementById('preview').innerHTML;
//document.getElementById("sb").innerHTML = svgs;
document.getElementById('svgsource').value = svgs;
};
Also see: Tab Triggers