Skip to content

Pie

Pie renders value-color pairs as a centered SVG plot layer. It is useful for background summaries, proportional overlays, and donut-style annotations that should share a plot container with other layers. Enable interactive when slices should emit pointer events.

Import

ts
import { Pie } from '@putianyi888/vue3-plots'

Example

Hover a slice

vue
<template>
    <div class="plot" :style="{ height: `${size.height}px`, width: `${size.width}px` }">
        <Pie
            :data="data"
            :inner-radius="34"
            :size="size"
            interactive
            @mouse-enter="hoveredIndex = $event.index"
            @mouse-leave="hoveredIndex = undefined"
        />
    </div>
    <p>{{ hoveredLabel }}</p>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { Pie } from '@putianyi888/vue3-plots'
import type { PlotSize } from '@putianyi888/vue3-plots'

const data = [
    { label: 'Research', value: 42, color: '#2563eb' },
    { label: 'Design', value: 24, color: '#16a34a' },
    { label: 'Engineering', value: 28, color: '#dc2626' },
    { label: 'Review', value: 12, color: '#9333ea' },
]
const size: PlotSize = { width: 360, height: 260 }
const hoveredIndex = ref<number>()
const hoveredLabel = computed(() => {
    if (hoveredIndex.value === undefined) return 'Hover a slice'

    const item = data[hoveredIndex.value]
    return `${item.label}: ${item.value}`
})
</script>

<style scoped>
.plot {
  position: relative;
}
</style>

Props

PropTypeDefaultDescription
sizePlotSizeOuter SVG size in pixels. Default provided by TransformGroup.
paddingPlotPaddingInsets used to calculate the plot area. Default provided by TransformGroup.
strokeWidthMaybeArray<number>1Slice outline width in SVG units.
strokeColorMaybeArray<string>'none'Slice outline color.
totalnumberTotal value used to normalize segments. Defaults to the sum of data values.
startAnglenumber0Start angle in degrees. Zero degrees points right.
interactivebooleanWhether slices emit pointer events and use interactive pointer styling.
innerRadiusMaybeArray<number>0Inner radius for each slice in SVG units. Pass a non-negative value.
outerRadiusMaybeArray<number>Outer radius for each slice in SVG units. Pass a positive value. Defaults to a half of the smaller plot-area dimension.
dataPieDatum[]RequiredPie segments in drawing order.

Events

EventPayloadDescription
clickPiePiece
mouseEnterPiePiece
mouseLeavePiePiece

Types

TypeDescription
PieDatumData item rendered as one Pie or MiniPie slice.
PiePieceEvent payload emitted by interactive Pie slices.

Notes

Pie is a full SVG layer centered in the plot area after padding is applied. When outerRadius is omitted, it defaults to half of the smaller plot-area dimension.

Pie does not validate data values or radius values. Pass finite values, a positive total when overriding automatic totals, non-negative innerRadius values, and positive outerRadius values. Invalid SVG radius values are passed through to the generated path data and may not render.

When interactive is enabled, hovered slices receive a visual floating effect. click, mouse-enter, and mouse-leave emit the slice index, source datum, and generated sector geometry.

Use MiniPie instead when you need an inline DOM-sized pie marker that participates in text flow or can be placed inside another SVG element.