Solved

CSS 3/HTML 5 - pie chart questions and help

Posted on 2014-01-06
9
833 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
In this Micro Tutorial viewers will learn how to create a CSS image sprite (In a later tutorial, viewers will learn how to use CSS and HTML to create a navigation menu using this sprite) Open a new Photoshop document with a width of (Icon width)x(N…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now