Display progress in a circle with JQuery and PHP

On my unspontan-site, I use some Javascript stuff as published by kottenator. See the linked GitHub page for examples.

At unspontan.com (which is used to count attendance of people, e.g. for an upcoming soccer training), I use it to display the numbers of participants that have given positive feedback; if that total number is below a certain treshold, the number is red; and above it’s green. Click here to see a demo.
Additionally, there is a maximum number of allowed positive feedbacks (i.e. maximum number of attendees); if that is reached, the circle reaches 100% (and is full). No further feedback is possible then.

The interesting part in my implementation is the dynamic creation of these progress markers, based on available positive feedback and minimum and maximum treshold.

Additionally, the font-size varies depending on the actual number, so the number doesn’t run outside of the circle.
Let’s look at some code (which is CSS whith inline PHP – very dirty, but it explains the concept).

These two styles (“greennumber” and “rednumber“) define the appearance of the actual number. $positivefeedbackfornextdate is the number of positive feedbacks, i.e. the number to be displayed.

<style type="text/css">
.greennumber {
  margin-top: 0.05em;
  <?php if ($positivefeedbackfornextdate <= 9) {print "font-size: 3em;position:relative; left:-1px; top:-64px;\n";} else {print "font-size: 2em;position:relative; left:0px; top:-55px;\n";}?>
  font-weight: bold;
  font-family: "Arial Black", sans-serif;
  color: #31B404;
  text-shadow: 0 0 0.1em #888, 0 0 0.1em #888, 0 0 0.1em #888; 
           }
.rednumber {
  margin-top: 0.05em;
<?php if ($positivefeedbackfornextdate <= 9) {print "font-size: 3em;position:relative; left:-1px; top:-64px;\n";} else {print "font-size: 2em;position:relative; left:0px; top:-55px;\n";}?>
  font-weight: bold;
  font-family: "Arial Black", sans-serif;color: #a33537;
  text-shadow: 0 0 0.1em #888, 0 0 0.1em #888, 0 0 0.1em #888;
         }

Now we draw the circle. $fulfillmentvalue is the number in percent that is reached; calculate it based on your numbers (I calculate it as $fulfillmentvalue = ($positivefeedbackfornextdate / $nextdatemaxpeople); with $nextdatemaxpeople being the allowed number of positive feedbacks).

The PHP code with embedded Javascript looks like this:

print "<div id=\"circle\" style='position:relative; left:0px; top:26px;'>\n";
print " <div class='greennumber'><strong></strong></div>";
print "</div>\n";
print "<script>\n";
print "$('#circle').circleProgress({\n";
print "    value: $fulfillmentvalue,\n";
print "    startAngle: -1.552,\n";
print "    thickness: 8,\n";
print "    size: 70,\n";
print "    fill: {\n";
print "        gradient: [\"#33FF00\", \"#006600\"]\n";
print "    },\n";
print "    animation: { duration: 2000}\n";
print "})\n";
print ".on('circle-animation-progress', function(event, progress) {\n";
print "$(this).find('strong').html(parseInt(" . $positivefeedbackfornextdate . " * progress));\n";
print "});\n";
print "</script>\n";

This is the positive case. For the negative case (i.e. red number) your would exchange the class of the number:

print " <div class='rednumber'><strong></strong></div>";

Play around with the CSS settings (e.g. size of the font) to find the best way for your site.

On my site, the final screen (including feedback details) looks like this:

This article was written by Frank

Corporate Banking expert at coconet.de and occasional hobby website creator...

Leave a Reply

Your email address will not be published. Required fields are marked *