9

I'm trying to show the progress of a fundraiser using some kind of donut chart. Many people use a thermometer style or progress bar for that but I really would like to get as closer as possible of something like this:

After I spent a lot of time researching I came up with a very easy solution to plot the donut using flot, here is the demo: http://jsfiddle.net/6b7nZ

$(function () {
    var data = [
    { label: "Donated", data: 20, color: '#f00' },
    { label: "Goal", data: 78, color: '#D3D3D3' }
    ];    
    $.plot($("#donut"), data,
    {
        series: {
            pie: {
                innerRadius: 0.7,
                show: true,
                label: { show: false }
            }
        },
        legend: { show: false }
    });
});

What I'm missing now is a way to add the centered label. I don't have to say that if anyone can point me to a jQuery plugin or something like that it will be even better.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
CodeMaster2008
  • 921
  • 2
  • 13
  • 25

2 Answers2

9

Well, something like this could work. I would think that you would want to set your goal to 100, however, to make the donut look like it is reflecting the correct percentage.

HTML

<div id="donutHolder">
    <div id="donut"></div>
    <span id="donutData"></span>   
</div>

CSS

#donutHolder {
    width: 350px;
    height: 350px;
    text-align: center;
    line-height: 350px;
    font-size: 80px;
    color: #f00;
    position: relative;
}

#donut {
    width: 100%;
    height: 100%;
}

#donutData {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 1;
    width: 100%;
}

Added JQuery

$("#donutData").text(Math.round(data[0].data/data[1].data*100)+"%");
ScottS
  • 71,703
  • 13
  • 126
  • 146
  • @Michal: Sorry, but I'm not sure what you mean by your comment. There are no "label" elements involved in this example, so it is unclear what your issue is. – ScottS Jul 07 '16 at 15:42
  • My problem was that i wanted to use the example together with labels – Michal Jul 10 '16 at 08:31
  • @Michal: Perhaps you should post another question, referencing this question as a cross-ref, but in yours, show exactly what you are trying to achieve with label elements (your html structure) that does not appear to be working. – ScottS Jul 11 '16 at 18:00
  • This is fork of working code with labels active: http://jsfiddle.net/h4oL3dv1/ ...nevertheless, i've already found another solution – Michal Jul 11 '16 at 18:40
0

I created this one useful (for me) cause this solution lets you set degrees values inside class by using less

<div class="radial_meter radial-222">
  <span class="first"><em></em></span>
  <span class="second"><em></em></span>
</div>

you can see a DEMO

Marco Allori
  • 3,198
  • 33
  • 25