Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CSS 3/HTML 5 - pie chart questions and help

Posted on 2014-01-06
9
Medium Priority
?
874 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
[X]
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
  • 3
  • 3
  • 2
9 Comments
 
LVL 53

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 53

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

722 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