0

first of all, i tried this progress bar: http://jsfiddle.net/Aapn8/3410/ and i encountered 2 problems:

  1. It was messing my WHOLE page's css because the progress bar's elements didnt have ids and i couldnt really fix it

  2. I wasnt able to edit the percentage via javascript.

I need a circular progress bar that i can control with javascript to add 1% to the percent value every second

i tried to do this via JS to set the progress bar's percentage but it didnt work

var x=0;
function update(y){
document.getElementById("percent").setAttribute("data-percent",y)
}

setInterval(function(){ if (x < 10){x = x+1; update(x);} else {exit;} }, 1000);
Mike
  • 41
  • 2
  • 9
  • Doesn't exactly look like the progress bar in the fiddle but you can have a look at [this answer](http://stackoverflow.com/questions/31198304/count-down-timer-with-circular-progress-bar/31199281#31199281) (mine) for some ideas. – Harry Jul 06 '15 at 14:30
  • that's what i kinda need, but i'd like to keep the aspect of that progress bar (the one in the jsfiddle), could you help me? – Mike Jul 06 '15 at 14:35
  • 1
    I was supposed to share that link.@Harry – Alaksandar Jesus Gene Jul 06 '15 at 14:35
  • @Mike: Actually [this one](http://stackoverflow.com/questions/29350504/circular-percent-progress-bar?lq=1) is pretty much the same as what you need. Don't know why I didn't think of this at first. Also, have a look at the second fiddle in [this question](http://stackoverflow.com/questions/31232449/border-based-triangles-not-rendering-as-expected). It is also very similar. – Harry Jul 06 '15 at 14:38

1 Answers1

1

Try this, it should not mess with your css.

var el = document.getElementById('graph'); // get canvas

var options = {
    percent:  el.getAttribute('data-percent') || 25,
    size: el.getAttribute('data-size') || 220,
    lineWidth: el.getAttribute('data-line') || 15,
    rotate: el.getAttribute('data-rotate') || 0
}

var canvas = document.createElement('canvas');
var span = document.createElement('span');
span.textContent = options.percent + '%';
    
if (typeof(G_vmlCanvasManager) !== 'undefined') {
    G_vmlCanvasManager.initElement(canvas);
}

var ctx = canvas.getContext('2d');
canvas.width = canvas.height = options.size;

el.appendChild(span);
el.appendChild(canvas);

ctx.translate(options.size / 2, options.size / 2); // change center
ctx.rotate((-1 / 2 + options.rotate / 180) * Math.PI); // rotate -90 deg

//imd = ctx.getImageData(0, 0, 240, 240);
var radius = (options.size - options.lineWidth) / 2;

var drawCircle = function(color, lineWidth, percent) {
  percent = Math.min(Math.max(0, percent || 1), 1);
  ctx.beginPath();
  ctx.arc(0, 0, radius, 0, Math.PI * 2 * percent, false);
  ctx.strokeStyle = color;
        ctx.lineCap = 'round'; // butt, round or square
  ctx.lineWidth = lineWidth
  ctx.stroke();
};

drawCircle('#efefef', options.lineWidth, 100 / 100);
drawCircle('#555555', options.lineWidth, options.percent / 100);

var handle = setInterval(function() {
  options.percent++;
  if(options.percent > 100) {
     clearInterval(handle);
     return;  
  }
  drawCircle('#555555', options.lineWidth, options.percent / 100);
  span.textContent = options.percent + '%';
}, 1000);
#graph div {
    position:relative;
    margin:80px;
    width:220px; height:220px;
}
#graph canvas {
    display: block;
    position:absolute;
    top:0;
    left:0;
}
#graph span {
    color:#555;
    display:block;
    line-height:220px;
    text-align:center;
    width:220px;
    font-family:sans-serif;
    font-size:40px;
    font-weight:100;
    margin-left:5px;
}

#graph input {
    width: 200px;
}
<div class="chart" id="graph" data-percent="1"></div>
ben
  • 3,558
  • 1
  • 15
  • 28
  • btw, after the progress bar reaches 100%; some random things will be called and after some time i'll have to reset the progress bar back to 0 and continue, how would i go about doing this? – Mike Jul 06 '15 at 14:45
  • Empty the div#graph and start all the js again – ben Jul 06 '15 at 14:47
  • apparently my css is still messed up. please refer to this image: http://i.imgur.com/wMgOHIH.png – Mike Jul 06 '15 at 14:53
  • Mike, posting a link to an image of a webpage is not very useful. Far better to show the page itself. – Lee Taylor Jul 06 '15 at 22:06
  • @Mike I guess the problem is from the css of your page, not from this one. This one is constraint to the div#graph – ben Jul 07 '15 at 07:25
  • how can i fix it? i paid a guy for the css and he barely replies now, so im on my own – Mike Jul 07 '15 at 08:44