• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
2

A script to make pies (and more)

Community Expert ,
Mar 26, 2012 Mar 26, 2012

Copy link to clipboard

Copied

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 = Number(values);

      total += values;

    }

    for (i=0; i<values.length; i++)

    {

      values = values*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;

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

    }

  }

}

TOPICS
Scripting

Views

12.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 26, 2012 Mar 26, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 26, 2012 Mar 26, 2012

Copy link to clipboard

Copied

Combine your scripts in creative ways!

fact.PNG

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 26, 2012 Mar 26, 2012

Copy link to clipboard

Copied

Nice one!

Peter

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 07, 2024 May 07, 2024

Copy link to clipboard

Copied

Now this post is VERY old. I copied the script but when I run it it produces nothing. Tried updating the for loops so that the values subscripts are correct (values [i] instead of just values). Then I get the "Pathfinder results defines empty region" error. Any suggestions?

//DESCRIPTION:Make me a pie
// A Jongware script, 25-mar-2012
if (app.selection.length == 1)
{
  val = prompt ("Values: ", "70|30");
  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 = app.selection[0].parent.graphicLines.add();
      cutoff.paths[0].pathType = PathType.CLOSED_PATH;
      cutoff.intersectPath(app.selection[0].duplicate()).fillTint = (i+1)*100/values.length;
      startangle = endangle;
    }
  }
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 07, 2024 May 07, 2024

Copy link to clipboard

Copied

Hi @Tim Sheasby ,

are you working on the first spread of your InDesign document?

If not, I'd test this. See my reply from 2019:

https://community.adobe.com/t5/indesign-discussions/a-script-to-make-pies-and-more/m-p/3980867#M1562...

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 07, 2024 May 07, 2024

Copy link to clipboard

Copied

LATEST

Was working on first (only) page of the document. Added:

'cutoff.paths[0].entirePath = path;'
before line 35 and now it works!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 27, 2012 Mar 27, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 06, 2017 Feb 06, 2017

Copy link to clipboard

Copied

thx for sharing Peter! great stuff

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 06, 2017 Feb 06, 2017

Copy link to clipboard

Copied

That was Jongware, not me!

P.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 30, 2019 Mar 30, 2019

Copy link to clipboard

Copied

Hi.  Awesome script.
I know it's and old post but I hope I can get an answers.
Script work perfectly until I change fill and tint amount of created slices by script.
Then it give this error if I try to run it again.


I open new document, and create pie chart at there when I get this error.
Is there any way you could help me with fixing this error?
Thank you very much.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 06, 2019 Apr 06, 2019

Copy link to clipboard

Copied

Genius!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 28, 2019 Jun 28, 2019

Copy link to clipboard

Copied

wow, this is great, works in CC 2019 and is even more precise than pie charts in AI. Now if I could feed it a text file with values for pies on different line and it would create those automatically it would be heaven;-) Like you put the width of the circle, and chose the text file and boom, it does all the pies. Oh well, still faster than doing it in AI.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 28, 2019 Jun 28, 2019

Copy link to clipboard

Copied

hmm, I get an error as soon as I try to run the script on a circle that is NOT on the first page of the document.Screen Shot 2019-06-28 at 3.12.42 PM.png

Anybody else got this error when run on other page then first page of document?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 29, 2019 Jun 29, 2019

Copy link to clipboard

Copied

Hi Jeff,

the real problem is in line 35:

cutoff = app.activeDocument.graphicLines.add();

That graphic line is always added to the first spread of the document.

And not to the spread where your selection is.

So later cutoff.intersectPath() cannot not work.

Why? You cannot intersect two paths that are positioned on different spreads.

You could try to work around this by writing:

cutoff = app.selection[0].parent.graphicLines.add();

Or perhaps better:

cutoff = app.activeDocument.layoutWindows[0].activeSpread.graphicLines.add();

Regards,
Uwe

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 02, 2019 Jul 02, 2019

Copy link to clipboard

Copied

Thanks Uwe! took the first solution and it works great!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines