4 Replies Latest reply: Mar 27, 2012 5:17 AM by Harbs. RSS

    A script to make pies (and more)

    [Jongware] CommunityMVP

      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;
          }
        }
      }