CxJS

Multi-level Pie Example

This example shows how to create a multi-level (sunburst) pie chart by nesting PieSlice components with different stack values. Click a slice to see its breakdown in the secondary legend.

<div controller={PageController}>
  <Legend />
  <div style="display: flex; gap: 16px">
    <Svg style="flex: 1; height: 350px">
      <ColorMap />
      <PieChart gap={2}>
        <Repeater records={m.data}>
          <PieSlice
            value={m.$record.value}
            active={m.$record.active}
            colorMap="pie"
            r={55}
            r0={20}
            borderRadius={3}
            name={m.$record.name}
            selection={{
              type: KeySelection,
              bind: m.selection,
              record: m.$record,
              index: m.$index,
              keyField: "id",
            }}
          />
          <Repeater
            records={m.$record.slices}
            recordAlias="$slice"
            indexAlias="$sliceIndex"
          >
            <PieSlice
              value={m.$slice.value}
              active={m.$record.active}
              colorMap="pie"
              colorName={m.$record.name}
              r={90}
              r0={58}
              offset={3}
              borderRadius={3}
              name={m.$slice.name}
              legend={expr(m.selection, m.$record.id, (sel, id) =>
                sel === id ? "slice" : false,
              )}
              stack="outer"
              style={{
                fillOpacity: expr(m.$sliceIndex, (i) => 0.4 + 0.6 * (i / 3)),
              }}
              selection={{
                type: KeySelection,
                bind: m.selection,
                record: m.$record,
                index: m.$index,
                keyField: "id",
              }}
            />
          </Repeater>
        </Repeater>
      </PieChart>
    </Svg>
    <Legend name="slice" vertical class="w-32" />
  </div>
</div>
North America
Europe
Asia Pacific
Latin America
Middle East
Africa

How It Works

  1. Inner ring - Main slices using the default stack
  2. Outer ring - Sub-slices using stack="outer" to form a separate ring
  3. Shared selection - Both rings share the same KeySelection to highlight related slices
  4. Dynamic legend - The legend prop uses expr to show sub-slice legend only when parent is selected
  5. Opacity variation - Sub-slices use varying fillOpacity to show hierarchy within the same color

Configuration

PropertyTypeDefaultDescription
stackstringstackStack name. Use different values for separate rings.
r / r0numberOuter/inner radius. Adjust to create ring separation.
offsetnumber0Gap between rings when using different stacks.
fillOpacitynumberUse in style to vary opacity within a color.