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.)
//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;
}
}
}
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
Copy link to clipboard
Copied
Combine your scripts in creative ways!
Copy link to clipboard
Copied
Nice one!
Peter
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;
}
}
}
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:
Regards,
Uwe Laubender
( Adobe Community Expert )
Copy link to clipboard
Copied
Was working on first (only) page of the document. Added:
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
Copy link to clipboard
Copied
thx for sharing Peter! great stuff
Copy link to clipboard
Copied
That was Jongware, not me!
P.
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.
Copy link to clipboard
Copied
Genius!
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.
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.
Anybody else got this error when run on other page then first page of document?
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 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
Copy link to clipboard
Copied
Thanks Uwe! took the first solution and it works great!