Figma Support Forum

Figma challenge – Flexible pie charts

Hey everyone!

I have a challenge for you :face_with_monocle:

The goal
Create a pie chart where each slice is equally spaced when I add or remove a slice.

How to participate
Recreate the attached image, but in a more flexible way.

Is there a right answer?
Who knows :slight_smile:

How it currently works
Each time I add a new slice, I have to manually edit the “Sweep” of the pie to match the proportions I’d need.

Will the slices always be equal sizes as you add/remove them? Is there a maximum/minimum for number of slices?

Yeah ideally equal!

So three options that come to mind, I’m sure there are more. All these assume equal sizes for the arcs.

1a. Create variants of all the iterations manually. A bit time consuming but you’d only have to create each variant once.

1b. This is more hacky, but apply an angular gradient to the fill, then overlay a dashed line to make the gaps. Make those styles and switch as needed. (You could also build the variants this way as well). I tested this out and once you figure out the math for the dashes, it’s usable.

  1. Create a Scripter to generate a pie chart. This is my preferred route but I know it may require a little technical skill to mod correctly. I’ll share a sample script you can test out.

Interested in hearing more ideas from others! Maybe a feature request for radial-based Auto Layout is needed. =)

Here’s a really rough snapshot of what option 1b looks like, the colors are deliberately wrong and not lined up correctly, but this is just two objects: the ellipse with the gradient fill, and an ellipse with just a dash stroke.

Screen Shot 2021-09-21 at 8.58.59 AM

Here’s a sample script you can try out using the Scripter plugin. Hope this helps!

// ==========================================================
// Stuff you can edit
// ==========================================================

let arcCount = 32 // Change to the number of arcs you need

let arcColors = [
	{r: 183.0/255.0, g: 0.0, b: 0.0},
	{r: 152.0/255.0, g: 0.0, b: 183.0/255.0},
	{r: 183.0/255.0, g: 122.0/255.0, b: 0.0},
	{r: 0.0, g: 183.0/255.0, b: 152.0/255.0},
	{r: 0.0, g: 73.0/255.0, b: 183.0/255.0}
] // Add more RGB colors to this array if you want; otherwise this will just cycle through the list of available colors on this list

let sharedStrokeColor = {r: 0.0, g: 0.0, b: 0.0}

// ==========================================================

// No need to mess with stuff down here unless you know what you're doing =)

let arcRadians = (360/arcCount) * (Math.PI/180)

var currentArcColor = 0

for (let i = 0; i < arcCount; i++) {

	let arc = figma.createEllipse()

	let startRadians = i * arcRadians
	let endRadians = startRadians + arcRadians

	arc.arcData = {startingAngle: startRadians, endingAngle: endRadians, innerRadius: 0.85}

	arc.fills = [{
    type: "SOLID",
    visible: true,
    opacity: 1.0,
    blendMode: "NORMAL",
    color: arcColors[currentArcColor]
  }]

	arc.strokes = [{
    type: "SOLID",
    visible: true,
    opacity: 1.0,
    blendMode: "NORMAL",
    color: sharedStrokeColor
  }]

	arc.strokeWeight = 2
	arc.strokeAlign = "CENTER"

	currentArcColor = currentArcColor + 1
	if (currentArcColor >= arcColors.length) {
		currentArcColor = 0	
	}
}

/* TODO List of potential improvements

1a. Ability to select an existing chart to get a count of ellipses, then replace at the right size, color, inner radius, etc.

1b. Pick up the existing colors from the selected ellipses and use them.

2. Enable different sized arcs and/or patterns

*/ 

Awesome! It looks like Rogie and Miggi managed to find a way too :smiley:

2 Likes

Yes! That technique is also very cool! I think the purist in me worries a little that the masked out arc divider lines are not perfectly parallel to each other because of the star. But it is also definitely a viable solution to this challenge.

This topic was automatically closed after 30 days. New replies are no longer allowed.