CxJS

PieLabel

import { PieLabelsContainer, PieLabel } from 'cx/charts'; Copied

PieLabel displays values for each pie slice with automatic position adjustment to prevent overlapping. Labels must be placed inside a PieLabelsContainer component.

Use innerPointRadius and outerPointRadius on PieSlice to define the label line anchor points. The distance property controls how far labels are pushed from the pie center. Text can be centered or auto-aligned to left/right using autoTextAnchor.

<div controller={PageController}>
  <Legend />
  <Svg style="width: 100%; height: 400px">
    <ColorMap />
    <PieLabelsContainer>
      <PieChart margin="0 80 0 80" gap={2}>
        <Repeater records={m.data}>
          <PieSlice
            value={m.$record.value}
            active={m.$record.active}
            colorMap="pie"
            r={60}
            r0={20}
            borderRadius={3}
            innerPointRadius={60}
            outerPointRadius={70}
            name={m.$record.name}
          >
            <Line style="stroke: gray" />
            <PieLabel
              anchors="1 1 1 1"
              offset="-12 60 12 -60"
              distance={m.distance}
              lineStroke="gray"
            >
              <Rectangle
                style="stroke: rgba(0, 0, 0, 0.1); fill: none"
                visible={falsy(m.autoAlign)}
              />
              <Text
                visible={falsy(m.autoAlign)}
                value={tpl(m.$record.name, m.$record.value, "{0} ({1:n;1})")}
                dominantBaseline="middle"
                textAnchor="middle"
              />
              <Text
                visible={m.autoAlign}
                value={tpl(m.$record.name, m.$record.value, "{0} ({1:n;1})")}
                dominantBaseline="middle"
                autoTextAnchor
                anchors="0.5 1 0.5 0"
                margin="0 5 0 5"
              />
            </PieLabel>
          </PieSlice>
        </Repeater>
      </PieChart>
    </PieLabelsContainer>
  </Svg>
  <LabelsTopLayout columns={2} mod={["stretch", "fixed"]}>
    <Slider
      value={m.count}
      minValue={1}
      maxValue={30}
      step={1}
      label="Points"
      help={{ tpl: "{count:n;0}" }}
    />
    <Slider
      value={m.distance}
      minValue={0}
      maxValue={200}
      label="Distance"
      help={{ tpl: "{distance:n;0}px" }}
    />
    <Switch value={m.autoAlign} label="Auto-align" />
  </LabelsTopLayout>
</div>
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8
Item 9
Item 10
 
10
 
60px
 

How It Works

  1. Wrap PieChart inside PieLabelsContainer
  2. Add innerPointRadius and outerPointRadius to PieSlice for line anchor points
  3. Place a Line inside PieSlice for the connector line from slice to label
  4. Add PieLabel with content (typically Text)
  5. Use autoTextAnchor on Text to automatically align left/right based on position

Configuration

PieLabelsContainer

Container that manages label distribution to prevent overlapping.

PropertyTypeDescription
anchorsstringBounded object anchors. Default "0 1 1 0".

PieLabel

PropertyTypeDefaultDescription
distancenumber100Distance from pie center in pixels.
offsetstringLabel bounding box offset "top right bottom left".
anchorsstringLabel anchors relative to parent bounds.
lineStrokestringStroke color for the connecting line.
lineStyleobjectStyle object for the connecting line.
lineClassstringCSS class for the connecting line.
lineColorIndexnumberColor index for the connecting line.