CxJS

PieChart

import { PieChart, PieSlice } from 'cx/charts'; Copied

Pie charts compare parts to the whole. Use PieChart as a container and PieSlice for each segment. The ColorMap component assigns unique colors to each slice. Click the legend to toggle slice visibility.

<div controller={PageController}>
  <Legend />
  <Svg style="width: 100%; height: 350px">
    <ColorMap />
    <PieChart angle={m.angle} gap={m.gap}>
      <Repeater records={m.data}>
        <PieSlice
          value={m.$record.value}
          active={m.$record.active}
          colorMap="pie"
          r={80}
          r0={m.r0}
          borderRadius={m.borderRadius}
          name={m.$record.name}
          innerPointRadius={80}
          outerPointRadius={90}
          tooltip={{
            text: { tpl: "{$record.name}: {$record.value}%" },
            trackMouse: true,
          }}
          selection={{
            type: KeySelection,
            bind: m.selection,
            record: m.$record,
            index: m.$index,
            keyField: "id",
          }}
        >
          <Line style="stroke: gray" />
          <Rectangle
            anchors="1 1 1 1"
            offset="-10 20 10 -20"
            style="fill: white; stroke: gray"
          >
            <Text tpl="{$record.value:n;0}%" dy="0.4em" ta="middle" />
          </Rectangle>
        </PieSlice>
      </Repeater>
    </PieChart>
  </Svg>
  <LabelsTopLayout columns={2} mod={["stretch", "fixed"]}>
    <Slider
      value={m.angle}
      minValue={30}
      maxValue={360}
      label="Angle"
      help={{ tpl: "{angle:n;0}°" }}
    />
    <Slider
      value={m.gap}
      minValue={0}
      maxValue={20}
      label="Gap"
      help={{ tpl: "{gap:n;0}px" }}
    />
    <Slider
      value={m.r0}
      minValue={0}
      maxValue={70}
      label="Inner Radius"
      help={{ tpl: "{r0:n;0}%" }}
    />
    <Slider
      value={m.borderRadius}
      minValue={0}
      maxValue={20}
      label="Border Radius"
      help={{ tpl: "{borderRadius:n;0}px" }}
    />
  </LabelsTopLayout>
</div>
Electronics
Clothing
Food
Books
Other
 
360°
 
4px
 
50%
 
5px

Configuration

PieChart

PropertyTypeDefaultDescription
anglenumber360Total angle of the pie in degrees.
startAnglenumber0Starting angle in degrees.
clockwisebooleanfalseSet to true for clockwise direction.
gapnumber0Gap between slices in pixels.

PieSlice

PropertyTypeDefaultDescription
valuenumberValue represented by the slice.
activebooleantrueControls visibility. Bind to legend for toggle.
rnumber50Outer radius (percentage of available space).
r0number0Inner radius for donut charts.
offsetnumber0Offset from center to explode the slice.
colorIndexnumberColor palette index (0-15).
colorMapstringNamed color map for automatic colors.
colorNamestringName used to resolve color. Defaults to name.
borderRadiusnumber0Corner rounding in pixels.
namestringName shown in legend.
legendstringlegendLegend name. Set to false to hide.
legendDisplayTextstringCustom text for legend entry.
legendShapestringcircleShape used in legend.
legendActionstringautoAction on legend click: auto, toggle, or select.
stackstringstackStack name for multi-level pies.
disabledbooleanfalseDisables interaction with the slice.
innerPointRadiusnumberInner radius for label line calculations.
outerPointRadiusnumberOuter radius for label line calculations.
percentageRadiusbooleantrueIf true, r and r0 are percentages of available space.
hoverIdstringID for hover synchronization.
hoverChannelstringdefaultChannel for hover synchronization.
selectionSelectionSelection configuration for click handling.
tooltipobjectTooltip configuration.