Solved

CSS 3/HTML 5 - pie chart questions and help

Posted on 2014-01-06
9
842 Views
Last Modified: 2014-01-06
See the attached file. I have a simple pie chart on a canvas, but I can't seem to get the percentages (the ones in text) to appear correctly next to their respective "slices" of pie.

Also, I was hoping for some kind of "fill" effect or animation that fills each slice. Any suggestions or help here would be awesome, too. I'm still trying learn HTML5 and CSS3 and am stuck on this one.
piechart.html
0
Comment
Question by:saturation
  • 3
  • 3
  • 2
9 Comments
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39760208
Instead of reinventing the wheel, there are plenty of great js and jquery charting options that can get you going very quickly.

http://www.chartjs.org/

http://www.flotcharts.org/
http://www.flotcharts.org/flot/examples/series-pie/index.html

http://www.fusioncharts.com/
http://www.highcharts.com/

Would these make your life easier or do you want to continue on your current solution?
0
 

Author Comment

by:saturation
ID: 39760242
Was really wanting to create my own from scratch. Any help?
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39760293
I only use the chart libraries.  Others have made my life easier so I can concentrate on other aspects of my applications.    Let's see if there is any other input.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39760410
Here's a start:
<html>
<head>
<style>
body {
   background: #333;
}

#canvas {
   display: block;
   width: 600px;
   margin: 100px auto;
   background: #555555;
}

@font-face {
   font-family: "bebas";
   src: url("http://thecodeplayer.com/uploads/fonts/BebasNeue.otf");
}

</style>

<script>

var canvas, context, W, H;

   // Variables
   var degrees = 0;
   var new_degrees = 0;
   var color = "yellow";
   var bgcolor = "#222";

   var barfill = 0;
   var new_barfill = 0;

   var difference = 0;
   var text;
   var animation_loop_gauge, animation_loop_bar, redraw_loop;

   var data = [120, 100, 140];
   var labels = ["120", "100", "140"];
   var colors = ["pink", "orange", "skyblue"];
   var radius;


window.onload = function(){

   // Canvas initialization
   canvas = document.getElementById("canvas");
   //var ctx = canvas.getContext("2d"); // removed - not used

   // Dimensions
   W = canvas.width;
   H = canvas.height;

   // Start the drawing
   //drawGauge();

   /////////////////////////////////
   // PIE CHART FUNCTIONS
   ////////////////////////////////
   //canvas = document.getElementById("canvas"); // removed - duplicate
   context = canvas.getContext("2d");

   for (var i=0; i < data.length; i++) {
      drawSegment(canvas, context, i);
   }

   function degreesToRadians(degrees) {
     return (degrees * Math.PI)/180;
   }

   function sumTo(a, i) {
     var sum = 0;
     for (var j = 0; j < i; j++) {
       sum += a[j];
     }
     return sum;
   }

   function drawSegment(canvas, context, i) {
     context.save();
     var centerX = Math.floor(canvas.width / 2);
     var centerY = Math.floor(canvas.height / 2);
     radius = 50;

     var startingAngle = degreesToRadians(sumTo(data, i));
     var arcSize = degreesToRadians(data[i]);
     var endingAngle = startingAngle + arcSize;

     context.beginPath();
     context.moveTo(centerX, centerY);
       //ctx.arc(W-200, H-200, 40, 0, Math.PI*2, false);
     context.arc(centerX, centerY, radius, startingAngle, endingAngle, false);
     context.closePath();

     context.fillStyle = colors[i];
     context.fill();
     context.restore();

     drawSegmentLabel(canvas, context, i);
   }

   function drawSegmentLabel(canvas, context, i) {
      context.save();
      var x = Math.floor(canvas.width / 2);
      var y = Math.floor(canvas.height / 2);
      var angle = degreesToRadians(sumTo(data, i));

      context.translate(x, y);
      //context.rotate(angle);
      var dx = Math.cos(angle+degreesToRadians(data[i]/2)) * (radius + 30); // Math.floor(canvas.width * 0.1) - 5;
      var dy = Math.sin(angle+degreesToRadians(data[i]/2)) * (radius + 30); // Math.floor(canvas.height * 0.18);

      context.textAlign = "right";
      context.font = "20px bebas";
      context.fillStyle = colors[i];
      //context.fillText(labels[i] + "%", dx, dy);
      var labelval = String(Math.round(data[i]*100/sumTo(data, data.length)));
      context.fillText(labelval + "%", dx + 10 * labelval.length, dy + 10);
      context.restore();
   }
}

</script>

</head>

<body>

<canvas id="canvas" width="600" height="300">Your browser does not support the canvas element.</canvas>

</body>
</html>

Open in new window

0
 

Author Comment

by:saturation
ID: 39760426
Awesome, Robert--that's looking good. I'll give you the points and ask about animation in a second question. Thanks!
0
 

Author Closing Comment

by:saturation
ID: 39760428
Perfect.
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39760434
Here's a version that does some animation.
<html>
<head>
<style>
body {
   background: #333;
}

#canvas {
   display: block;
   width: 600px;
   margin: 100px auto;
   background: #555555;
}

@font-face {
   font-family: "bebas";
   src: url("http://thecodeplayer.com/uploads/fonts/BebasNeue.otf");
}

</style>

<script>

var canvas, context, W, H, animSpeed = 50, animPerc = 0;

   // Variables
   var degrees = 0;
   var new_degrees = 0;
   var color = "yellow";
   var bgcolor = "#222";

   var barfill = 0;
   var new_barfill = 0;

   var difference = 0;
   var text;
   var animation_loop_gauge, animation_loop_bar, redraw_loop;

   var data = [120, 100, 140];
   var labels = ["120", "100", "140"];
   var colors = ["pink", "orange", "skyblue"];
   var radius;


window.onload = function(){

   // Canvas initialization
   canvas = document.getElementById("canvas");

   // Dimensions
   W = canvas.width;
   H = canvas.height;

   // Start the drawing
   //drawGauge();

   /////////////////////////////////
   // PIE CHART FUNCTIONS
   ////////////////////////////////
   context = canvas.getContext("2d");

   for (var i=0; i < data.length; i++) {
      drawSegment(canvas, context, i);
   }

   setTimeout(anim, animSpeed);
}

function degreesToRadians(degrees) {
  return (degrees * Math.PI)/180;
}

function sumTo(a, i) {
  var sum = 0;
  for (var j = 0; j < i; j++) {
    sum += a[j];
  }
  return sum;
}

function drawSegment(canvas, context, i) {
  context.save();
  var centerX = Math.floor(canvas.width / 2);
  var centerY = Math.floor(canvas.height / 2);
  radius = 50;

  var startingAngle = degreesToRadians(sumTo(data, i));
  var arcSize = degreesToRadians(data[i]);
  var endingAngle = startingAngle + arcSize * animPerc / 100;

  context.beginPath();
  context.moveTo(centerX, centerY);
  context.arc(centerX, centerY, radius, startingAngle, endingAngle, false);
  context.closePath();

  context.fillStyle = colors[i];
  context.fill();
  context.restore();

  drawSegmentLabel(canvas, context, i);
}

function drawSegmentLabel(canvas, context, i) {
   context.save();
   var x = Math.floor(canvas.width / 2);
   var y = Math.floor(canvas.height / 2);
   var angle = degreesToRadians(sumTo(data, i));

   context.translate(x, y);
   //context.rotate(angle);
   var dx = Math.cos(angle+degreesToRadians(data[i]/2)) * (radius + 30);
   var dy = Math.sin(angle+degreesToRadians(data[i]/2)) * (radius + 30);

   context.textAlign = "right";
   context.font = "20px bebas";
   context.fillStyle = colors[i];
   //context.fillText(labels[i] + "%", dx, dy);
   var labelval = String(Math.round(data[i]*100/sumTo(data, data.length)));
   context.fillText(labelval + "%", dx + 10 * labelval.length, dy + 10);
   context.restore();
}

function anim() {
   if (animPerc < 100) {
      animPerc += 5;
      context.clearRect(0, 0, canvas.width, canvas.height);
      for (var i=0; i < data.length; i++) {
         drawSegment(canvas, context, i);
      }

      setTimeout(anim, animSpeed);
   }
}

</script>

</head>

<body>

<canvas id="canvas" width="600" height="300">Your browser does not support the canvas element.</canvas>

</body>
</html>

Open in new window

0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39760437
oops... I was already working on that, you had included that in your post so I thought I would add it ;-)
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question