Jump to content
  • Advertisement
Sign in to follow this  
gretty

Pie Graph Generator: Values of >180 degrees are incorrect?

This topic is 2532 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello

I have made a very simple Pie Graph generator using Javascript & SVG graphics. You just enter the angle of the pie graph you want then click the button.

My Problem: It generates graphs that are too big for the angles 100 degrees, 180 degrees & many others. It does generate the correct pie for the angle 277. For an angle such as 180 which should be a half circle it shows a pie of 270 degrees for some reason.

Whats wrong with my simple algorithm that leads to the wrong pie shapes/values?

The JSFiddle is here (for some reason the JSFiddle wont update but if you copy the HTML below it all works): http://jsfiddle.net/mabg3/1/


<html>
<head>
<script type="text/javascript">
<!--
function createPieGraph()
{
var angle = parseInt(document.getElementById("angleVal").value, 10);
var path = document.getElementById("pie"); //document.createElement("path");
var diameter = 200
var rad = diameter/2;
var point = resolveToPoint(angle, diameter);
var d = "M"+diameter+","+diameter+" L"+point.mX+","+point.mY+" A"+rad+","+rad+" "+determineDisplayType(angle)+" "+determineArcEnd(angle, diameter, rad)+" z";
path.setAttribute("d", d);
}

function resolveToPoint( /*int*/ deg, /*int*/ diameter )
{
var rad = Math.PI * deg / 180;
var r = diameter / 2;
var x = r * Math.cos(rad);
var y = r * Math.sin(rad);
var midX = diameter;
var midY = diameter;

if (deg <= 90)
{
console.log("1");
x = midX + x;
y = midY - y;
}
else if (deg <= 180)
{
console.log("2");
x = midX + x;
y = midY + y;
}
else if (deg <= 270)
{
console.log("3");
x = midX - x;
y = midY + y;
}
else if (deg <= 360)
{
console.log("4");
x = midX - x;
y = Math.abs(y);
}

return {mX: x, mY: y};
}

function determineDisplayType( /*int*/ deg )
{
if (deg <= 90)
return "0 0,0";
else if (deg <= 180)
return "0 1,0";
else if (deg <= 270)
return "0 1,0";
else if (deg <= 360)
return "1 1,1";

return "0 0,0";
}

function determineArcEnd( /*int*/ deg, /*int*/ diameter, /*int*/ rad )
{
if (deg >= 270)
return ""+rad+","+diameter;

return ""+diameter+","+rad;
}
-->
</script>
</head>
<body>

<svg id="main" width="400" height="400" style="background-color: red;">
<path id="pie" fill="blue" stroke="white"></path>
</svg>

<input type="text" id="angleVal" value="Enter angle"></input>
<input type="button" onclick="createPieGraph();" value="Show Pie Graph"></input>
</body></html>

Share this post


Link to post
Share on other sites
Advertisement
Firstly, your program doesn't seem to work for angles less than 180 degrees either. I note that if you put the following:

var rad = Math.PI * deg / 180;
var r = diameter / 2;
var x = r * Math.sin(rad);
var y = r * Math.cos(rad);
var midX = diameter;
var midY = diameter;
x = midX + x;
y = midY - y;
return {mX: x, mY: y};

to replace the current body of the resolveToPoint function, and remove the dependence of the arc endpoint on the angle, then the two lines coming from the centre of the pie graph are in the right spot, though the rest of the code has trouble putting the body of the pie graph in the right spot if the angle is between 90 and 180 degrees or 270 and 360 degrees.

Share this post


Link to post
Share on other sites
Thanks man :D

Its working now, I struggle soooo much with maths for a programmer :P

PS heres the new code to display values > 180 correctly now


function resolveToPoint( /*int*/ deg, /*int*/ diameter )
{
var rad = Math.PI * deg / 180;
var r = diameter / 2;
var x = r * Math.sin(rad);
var y = r * Math.cos(rad);
var midX = diameter;
var midY = diameter;
x = midX + x;
y = midY - y;
return {mX: x, mY: y};
}

function determineDisplayType( /*int*/ deg )
{
if (deg <= 90)
return "0 0,0";
else if (deg <= 180)
return "0 0,0";
else if (deg <= 270)
return "0 1,0";
else if (deg <= 360)
return "1 1,0";//"1 1,1";

return "0 0,0";
}

function determineArcEnd( /*int*/ deg, /*int*/ diameter, /*int*/ rad )
{
//if (deg >= 270)
// return ""+rad+","+diameter;

return ""+diameter+","+rad;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!