Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to create a function that returns a partly filled Disk, based on a percentage. So inputting 1 would give a fully filled disk, while inputting 0.5 would return a Disk with half of if it filled (from 12 'o clock to 6 'o clock). Etc.

So far I've written this:

Graphics[{{EdgeForm[Thick], White, Disk[{0, 0}, 1]}, 
          {Blue, Disk[{0, 0}, 1, {4 Pi/3, Pi/2}]}}]

enter image description here

The function part is not difficult but I can't get the coordinates to work properly. How do I transform a percentage like 0.1 to the correct segment of the Disk? (The {4 Pi/3, Pi/2} of the function).

share|improve this question
    
@Öskå Yes technically a math issue. 2 Pi * percen. Honestly I would just use degree in this case. –  Liam William Aug 21 '13 at 8:17
    
You need Disk[{0, 0}, 1, {Pi/2, Pi/2 +/- n 2 Pi}] but I agree with @Öskå. –  Kuba Aug 21 '13 at 8:18
    
ah, I just found it too :) And removed my comment about it.., meh, no awake yet. But this answer is probably helpful. –  Öskå Aug 21 '13 at 8:22

5 Answers 5

up vote 9 down vote accepted

In my opinion using Rescale is not really necessary here. If you want to change both, the percentage p and the starting point s of the cake the formula for the angle argument of Disk is simply

2Pi*({0, p} + s)

This gives

enter image description here

and the code is simply

percentGauge[p_, start_] := 
 Graphics[{{EdgeForm[Thick], White, Disk[{0, 0}, 1]}, {Blue, 
    Disk[{0, 0}, 1, 2 Pi ({0, p} + start)]}}]
share|improve this answer
    
I'm shocked that {0, p} + start appeared in 5th answer... Finally something without redundant parts :) –  Kuba Aug 21 '13 at 11:32

Rescale is useful:

percentFill[
   n_] := {90 Degree, (90 +  Rescale[n, {0, 1},  {0, 360}]) Degree};
Table[Graphics[{{
    EdgeForm[Thick], White, Disk[{0, 0}, 1]},
   {Blue, Disk[{0, 0}, 1, percentFill[p]]}}], {p, 0, 1, .1}]

disks

share|improve this answer

"Partly filled disks based on percentage" == PieChart gives True so

f[s_] := PieChart[{1 - s, s}, SectorOrigin -> π/2]

is what you are looking for.

Usage:

f[.2]

pie

---EDIT---

SectorOrigin set to $ \pi/2 $ to match your requirement for .5 splitting the disk top-bottom rather than left-right (and also to get @Kuba's +1!)

share|improve this answer
1  
Add SectorOrigin so it fits OP's needs and you have my +1 :) –  Kuba Aug 21 '13 at 10:34

Assuming you are on v9, here's another approach (it's different from what you ask, but maybe somebody else will like that):

myGauge[x_] := 
   AngularGauge[100 x, {0, 100}, 
      ScaleOrigin -> Reverse@{5 \[Pi]/2, \[Pi]/2}, 
      ScaleOrigin -> Top, 
      ScaleDivisions -> 10, 
      GaugeMarkers -> Placed[Automatic, "ScaleRange"], 
      GaugeLabels -> Placed["Value", Center]]

As you can see, I use AngularGauge to solve the issue. I specified some formatting - see the Documentation Center for more options / features (e.g. remove the Reverse to get other orientation).

Interactively:

Manipulate[myGauge[x], {{x, 0.42, "Fraction"}, 0, 1}]

yields:

enter image description here

share|improve this answer

You can use Degree to have the circle go around part of the whole.

filledDisk[frac_] := Graphics[{{EdgeForm[Thick], White, Disk[{0, 0}, 1]}, 
  {Blue, Disk[{0, 0}, 1, {90 Degree, (90 + frac 360) Degree}]}}]

And if you need percentage you can calculate it like so.

.1 * 360
share|improve this answer
    
Look at the example. It fills from the top... zero degrees is to the right. –  Pickett Aug 21 '13 at 8:16
    
@Anon Well then 90 Degree then –  Liam William Aug 21 '13 at 8:18
    
For a full answer I was looking for something like this: filledDisk[frac_] := Graphics[{{EdgeForm[Thick], White, Disk[{0, 0}, 1]}, {Blue, Disk[{0, 0}, 1, {90 Degree, (90 + frac 360) Degree}]}}] and the corresponding function with radians. –  Pickett Aug 21 '13 at 8:34
    
@Anon I amended my answer assuming you don't mind that I use your code. –  Liam William Aug 21 '13 at 8:36
    
@Liam I've never seen a clock turning in that way ;-) –  Öskå Aug 21 '13 at 8:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.