Ben Freeman
Ben Freeman

Reputation: 21

How would I create a piechart in openscad

I'm new to openSCAD and trying to make a 3D piechart for a set of data. What I have tried is this:

        fn=$24;

translate([5,8.75])
cylinder(h=2, r=10,$fn=3, center=false);


color("red")
translate([-5,8.75])
rotate([0,0,180])
cylinder(h=2, r=10,$fn=3, center=false);

color("blue")
translate([-5,-8.75])
rotate([0,0,180])
cylinder(h=2, r=10,$fn=3, center=false);

color("green")
translate([5,-8.75])
cylinder(h=2, r=10,$fn=3, center=false);

color("purple")
translate([-10,-0])
rotate([0,0,120])
cylinder(h=2, r=10,$fn=3, center=false);

After trying this method with low resolution cylinders to create slices I realized that I don't know or even think its possible to accurately resize the slices to represent the data I'm using. I'm wondering if there's an easier/better way that I could accomplish this.

Upvotes: 0

Views: 93

Answers (1)

T.P.
T.P.

Reputation: 494

Using rotate_extrude() is likely a good option as it has the angle parameter for generating part of a circle.

$fa = 2; $fs = 0.4;

h = 5;  // height of the pie pieces
r = 30; // radius
o = 5;  // offset from center to make a ring

values = [
    [50, "red"],
    [60, "green"],
    [170, "blue"],
    [80, "yellow"],
];

// running sum over the percentages/angles
rsum = [ for (i = 0, v = 0;i < len(values);i = i + 1, v = v + values[i - 1][0]) v];

for (i = [0:len(values) - 1])
    color(values[i][1])
        rotate(rsum[i])
            rotate_extrude(angle = values[i][0])
                translate([o, 0])
                    square([r - o, h]);

Upvotes: 1

Related Questions