Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Semicircular chart to represent percentages.</h1>
<p>A simple solution without SVG.</p>
    
    
<div id="list"></div><!-- Container for the legend -->
<div id="semiCircle"></div><!-- Container for the chart -->
<div id="corseCircle"><div id="circle" ></div></div><!-- Fake chart mask -->
    
    
    
    
<div style="text-align: center;margin-top:20px"><input type="button" onclick="pulsedButtonEffect(this);randomValues(false)" value="Random values"><input type="button" onclick="pulsedButtonEffect(this);randomValues(true)" value="Random values sorted"></div>
<em>You can enter or modify values manually</em>   
<div id="inputs"></div>










<author style="margin:10px;display:block"> 
    <img style="width: 40px; border-radius: 50%;vertical-align: middle; background-color:black;transform:scale(-1,1)" src="https://s.cdpn.io/profiles/user/3103547/512.jpg?1555236072" alt="Avatar of Jomohop"> by 
    <a href="https://codepen.io/jomohop"  target="_blank" style="color:black; font-size: 18px;vertical-align: middle"> Jomohop</a> 
</author>   
              
            
!

CSS

              
                body,html{
margin:0;
min-height: 300px;    
}
body{
font-family: sans-serif;
font-weight: normal;
height: 100%;
font-size: 16px;
background-attachment: fixed;
background-image: linear-gradient(75deg,lightgray, lightgray 15%, white 40% , white 60%,lightgray);
}
h1{
font-weight: normal;
line-height: 24px;
margin:0;
}

#semiCircle{
background-color: white;
position:relative;
height: 45vmin;
margin:auto;       
width: 90vmin;
overflow: hidden;
border-top-left-radius: 90vmin;
border-top-right-radius: 90vmin;    
z-index: 0;
box-sizing: border-box;
}
#corseCircle{
height: 15vmin; 
margin-top:-15vmin;
overflow: hidden;
position:relative;
}
#circle{
height: 30vmin;
width: 30vmin;
background-color: white;
margin: auto;
border-radius: 50%;
border-color: darkslategray  transparent  transparent darkslategray;
transform: rotate(45deg);
box-sizing: border-box;
}
 /*So far the necessary CSS to draw the  chart */   
 
    
    
    
    
    
    
#list{
position: absolute;
padding:5px;
z-index: 2;
  
}
#list>div{
position: absolute;
line-height: 30px;
overflow: hidden;
width: 150px;
color: white;
margin-left:5px;
transition: margin-top .6s;
  
}
#list>div>span{
display: inline-block;
vertical-align: top;
width: 60px;
line-height: inherit;
text-align:right;
}
#list>div>div{       
line-height: inherit;
display: inline-block;
vertical-align: top;
padding-left: 10px;
text-transform: capitalize;
}    
/* So far the necessary CSS to draw the legend of the chart.*/ 
    
    
    
    

    
    
/* The following CSS only serve to make this page more attractive in Codepen */

.suporInput{
display:inline-block;
position:relative;
margin:5px;
text-align:right;
width: 140px;
transition:.4s;
border:1px solid white; 
padding: 5px 0;
transition:.4s;
}
    
.suporInput>div{
background-color: antiquewhite;
padding-right: 3px;
margin-right: 5px;
display: inline-block;
}   
.suporInput>div>span{
display:inline-block;
cursor:pointer;
vertical-align: middle;
margin-top:-7px;
}

.suporInput>div>input{
width: 60px;
border:none;
line-height: 25px;
padding: 0;
font-size: 18px;
text-align: center;
 vertical-align: middle;
transition:.20s;
}

.panel{
position:absolute;
height: 90vmin; 
width: 45vmin;
transform-origin: right center; 
transform:rotate(-90deg);
transition:transform .8s;
}
[inactive]{
background-color: lightgray!important;
}
[inactive]>div span{
display:none;        
}
[inactive] div{
text-shadow:none;        
}
[type=button]{
margin:10px;
display:inline-block;
padding:7px 20px;
border:1px solid black;
background-color: white;
cursor: pointer;
font-size: 16px;
transition:.05s;
}
 

p{
margin: 0;
font-size: 18px;
} 
              
            
!

JS

              
                 document.addEventListener("DOMContentLoaded", function() {
    var colors=["red", "orange", "yellow", "green", "blue", "indigo", "violet"]
    var initialValue=[0, 45, 0, 30, 20, 5, ""]
    var concept = Array.from(colors)
    concept[0]="Tomato"
    concept[2]="Lemon"
    concept[3]="Grass"
    concept[4]="Cobalt" 
   // Two concepts are changed to show that colors and concepts do not have to be the same
    createObjectsChart(colors)
    createList(concept,colors)
    createInputs(colors)   
    setValuesInputs(initialValue)
    setTimeout(function(){valueChart(initialValue)},1)

})

function createObjectsChart(colors){
    colors.forEach(function(color,i){
        var p = document.createElement("DIV")
        p.className = "panel"
        p.style.backgroundColor = color        
        p.style.zIndex = colors.length - i + 10        
        document.getElementById("semiCircle").appendChild(p)        
    })    
}

function valueChart(values){
    var total = values.reduce((a, b) => Number(a) + Number(b), 0)
    var angle = -90
    values.forEach(function(value,i){        
        if (total != 0){    
            angle += 180 * value / total 
        }        
        document.querySelectorAll(".panel")[i].style.transform="rotate("+angle+"deg)"
    }) 
    valuesList(values)
}
/*So far the necessary functions to draw the  chart */







function createList(concept,colors){
    concept.forEach(function(concept, i){
        var li = document.createElement("DIV")
        li.style.backgroundColor = colors[i]
        li.innerHTML = '<span></span>' +
                       '<div>'+concept+'</div>'
        document.getElementById("list").appendChild(li)
    })
}

function valuesList(values){
    var total = values.reduce((a, b) => Number(a) + Number(b), 0)
    var percentage = document.querySelectorAll("#list>div>span")
    var line = document.querySelectorAll("#list>div")
    var lineHeight = line[0].scrollHeight
    var order = []
    values.forEach(function(value,i){
         percentage[i].innerHTML=(Number(value) * 100 /total).toFixed(2) + "%"
        if (value == ""){
            line[i].style.height = 0
            line[i].style.marginTop = 0
        }else{
            line[i].style.height = lineHeight+"px"
            order[i] = {line:line[i],value:Number(value)}
        }
        
    })
    order.sort(function(a, b){return b.value - a.value})
    order.forEach(function(order,i){
        order.line.style.zIndex = i        
        order.line.style.marginTop = (lineHeight +5) * i + "px"        
    })
}
/* So far the necessary functions to draw the legend of the chart.*/














/* The following functions only serve to make this page more attractive in Codepen */

function createInputs(colors){
    colors.forEach(function(color,i){        
        var e = document.createElement("DIV")
        e.className = "suporInput"
        e.style.backgroundColor = color
        e.setAttribute("inactive",true)
        e.innerHTML =   '<div><input type="text" placeholder="value" value="' +            
                        '" oninput="changeValue(this)">' +
                        '<span onclick="deleteValue(this)">&#9587;</span></div>'
        document.getElementById("inputs").appendChild(e)              
    })
}

function deleteValue(button){     
     var input = button.previousSibling
     input.value = ""
     changeValue(input)     
}

function readValuesInputs(){
    var values=[]
    document.querySelectorAll("#inputs input").forEach(function(input){
        values.push(input.value)
     })
    return values
}
function setValuesInputs(values){
    var total = values.reduce((a, b) => Number(a) + Number(b), 0)
    var suport = document.querySelectorAll("#inputs>div")
    var textBox = document.querySelectorAll("#inputs input")
    values.forEach(function(value,i){ 
        if (value == 0) value = ""
        value == "" ? suport[i].setAttribute("inactive",true) : suport[i].removeAttribute("inactive")
        textBox[i].value = value       
    }) 
}

function pulsedButtonEffect(button){
    button.setAttribute("style","transform:scale(.98);box-shadow: inset 1px 1px 4px black")
    setTimeout(function(){
        button.setAttribute("style","")
    },80)
}
function randomValues(sort){
    var numElements = document.querySelectorAll("#inputs>div").length
    var values = Array.from({length: numElements}, () => parseInt(Math.random() * 190+10))
    if (sort) values.sort(function(a, b){return b - a})
    var inactive = Math.floor(Math.random()*5)
    for (var i = 0; i < inactive; i++){
        values[Math.floor(Math.random()*numElements)] = ""
    }
    setValuesInputs(values)
    valueChart(values)
}

function changeValue(input){
    if(isNaN(parseInt(input.value)) || input.value == "0"){
        input.value=""
    }else{
        input.value=Math.abs(parseFloat(input.value))
    }
    setValuesInputs(readValuesInputs())
    valueChart(readValuesInputs())
}  
              
            
!
999px

Console