5
$\begingroup$

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).

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

5 Answers 5

9
$\begingroup$

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)]}}]
$\endgroup$
1
  • $\begingroup$ I'm shocked that {0, p} + start appeared in 5th answer... Finally something without redundant parts :) $\endgroup$
    – Kuba
    Aug 21, 2013 at 11:32
8
$\begingroup$

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

$\endgroup$
7
$\begingroup$

"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!)

$\endgroup$
1
  • 1
    $\begingroup$ Add SectorOrigin so it fits OP's needs and you have my +1 :) $\endgroup$
    – Kuba
    Aug 21, 2013 at 10:34
3
$\begingroup$

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

$\endgroup$
2
$\begingroup$

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
$\endgroup$
6
  • $\begingroup$ Look at the example. It fills from the top... zero degrees is to the right. $\endgroup$
    – C. E.
    Aug 21, 2013 at 8:16
  • $\begingroup$ @Anon Well then 90 Degree then $\endgroup$
    – William
    Aug 21, 2013 at 8:18
  • $\begingroup$ 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. $\endgroup$
    – C. E.
    Aug 21, 2013 at 8:34
  • $\begingroup$ @Anon I amended my answer assuming you don't mind that I use your code. $\endgroup$
    – William
    Aug 21, 2013 at 8:36
  • $\begingroup$ @Liam I've never seen a clock turning in that way ;-) $\endgroup$
    – Öskå
    Aug 21, 2013 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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