cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <?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">&FilledSmallSquare; 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 = "&FilledSmallSquare; "+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;
};
            
          
!
999px
Loading ..................

Console