Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

CSS 3/HTML 5 - pie chart questions and help

Posted on 2014-01-06
9
845 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how can i turn the array into an object 4 39
function parameter and using that as array 15 22
Make footer stick to bottom 6 15
send email part1 9 22
Find out what you should include to make the best professional email signature for your organization.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

792 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