Skip navigation
Currently Being Moderated

A script to make pies (and more)

Mar 26, 2012 4:55 AM

Tags: #javascript #chart #js #graph #pie #pie_chart #jongware

As a sort of dare from our friendly neighbours in the Illustrator forum, I wrote this script that might be useful for InDesign users as well.

 

It creates a pie chart by asking for the series of values, and then slicing up a circle that you must have selected. Fun thing is, it doesn't "just" work with circles; you can select about anything, enter your values at the prompt (comma separated), and get the correct result! (Which depends on the shape; of course it's only mathematically correct when using a circle.)

 

make-me-a-pie.PNG

 

 

//DESCRIPTION:Make me a pie
// A Jongware script, 25-mar-2012
 
if (app.selection.length == 1)
{
  val = prompt ("Values: ", "1,2,3");
  if (val != null)
  {
    values = val.split(",");
    total = 0;
    for (i=0; i<values.length; i++)
    {
      values[i] = Number(values[i]);
      total += values[i];
    }
    for (i=0; i<values.length; i++)
    {
      values[i] = values[i]*2*Math.PI/total;
    }
 
 
    circleCenter = [ (app.selection[0].geometricBounds[3]+app.selection[0].geometricBounds[1])/2,
      (app.selection[0].geometricBounds[2]+app.selection[0].geometricBounds[0])/2 ];
    circleRadius = Math.max ( (app.selection[0].geometricBounds[3]-app.selection[0].geometricBounds[1])/2,
      (app.selection[0].geometricBounds[2]-app.selection[0].geometricBounds[0])/2);
 
 
    startangle = 0;
    for (i=0; i<values.length; i++)
    {
      endangle = startangle + values[i];
      // warning: math! (though this is SIMPLE compared to what I've bin doing today)
      segstart = [ circleCenter[0]+ 2*circleRadius*Math.sin(startangle), circleCenter[1]- 2*circleRadius*Math.cos(startangle) ];
      segend = [ circleCenter[0]+ 2*circleRadius*Math.sin(endangle), circleCenter[1]- 2*circleRadius*Math.cos(endangle) ];
      cutoff = app.activeDocument.graphicLines.add();
      // create surrounding border
      path = [ circleCenter, segstart ];
      for (j=startangle; j<endangle; j += (values[i]/16))
        path = path.concat ([[ circleCenter[0]+2*circleRadius*Math.sin(j),circleCenter[1]-2*circleRadius*Math.cos(j)]] );
      path = path.concat ([segend]);
      cutoff.paths[0].entirePath = path;
      cutoff.paths[0].pathType = PathType.CLOSED_PATH;
      cutoff.intersectPath(app.selection[0].duplicate()).fillTint = (i+1)*100/values.length;
      startangle = endangle;
    }
  }
}
 
Replies
  • Currently Being Moderated
    Mar 26, 2012 5:52 AM   in reply to [Jongware]

    Amazing..... Using "Math" you showed me that there is a long........ way to go.

     

    15% interesting

    25% fascinating

    60% just paragraph filling

     

         100% worth reding this....


    ----------

    Green4ever

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 26, 2012 8:07 AM   in reply to [Jongware]

    Nice one!

     

    Peter

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 27, 2012 5:17 AM   in reply to [Jongware]

    Very cool!

     

    He he. Don't get those Illy guys too jealous! (I feel like pulling out my hair every time I try to script Illy. So limited...)

     

    Harbs

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)