Pie charts display data as slices of a circular pie, ideal for showing proportions and percentages that add up to 100%.
Basic Usage
To create a pie chart, use type: 'pie' and specify
start and
end for each segment.
await import('/components/data-display/chart.js');
const { tags, $ } = Lightview;
const { Chart } = tags;
const chart = Chart({
type: 'pie',
style: 'width: 100%; max-width: 300px; margin: 0 auto; aspect-ratio: 1;'
},
Chart.Body({},
Chart.Row({},
Chart.Data({ start: 0.0, end: 0.5, color: '#4CAF50' }) // 50%
),
Chart.Row({},
Chart.Data({ start: 0.5, end: 0.8, color: '#2196F3' }) // 30%
),
Chart.Row({},
Chart.Data({ start: 0.8, end: 1.0, color: '#FF9800' }) // 20%
)
)
);
$('#example').content(chart);
With Labels
Add data labels with labels: true and include a
<span class="data">.
await import('/components/data-display/chart.js');
const { tags, $ } = Lightview;
const { Chart, span } = tags;
const chart = Chart({
type: 'pie',
labels: true,
style: 'width: 100%; max-width: 300px; margin: 0 auto; aspect-ratio: 1;'
},
Chart.Body({},
Chart.Row({},
Chart.Label({}, 'Chrome'),
Chart.Data({ start: 0.0, end: 0.6, color: '#4CAF50' },
span({ class: 'data' }, '60%'))
),
Chart.Row({},
Chart.Label({}, 'Firefox'),
Chart.Data({ start: 0.6, end: 0.85, color: '#F44336' },
span({ class: 'data' }, '25%'))
),
Chart.Row({},
Chart.Label({}, 'Safari'),
Chart.Data({ start: 0.85, end: 1.0, color: '#2196F3' },
span({ class: 'data' }, '15%'))
)
)
);
$('#example').content(chart);
Color Schemes
Use custom colors for each slice via the color prop.
await import('/components/data-display/chart.js');
const { tags, $ } = Lightview;
const { Chart, span } = tags;
const chart = Chart({
type: 'pie',
labels: true,
style: 'width: 100%; max-width: 300px; margin: 0 auto; aspect-ratio: 1;'
},
Chart.Body({},
Chart.Row({},
Chart.Label({}, 'Desktop'),
Chart.Data({ start: 0.0, end: 0.45, color: '#00BCD4' },
span({ class: 'data' }, '45%'))
),
Chart.Row({},
Chart.Label({}, 'Mobile'),
Chart.Data({ start: 0.45, end: 0.80, color: '#8BC34A' },
span({ class: 'data' }, '35%'))
),
Chart.Row({},
Chart.Label({}, 'Tablet'),
Chart.Data({ start: 0.80, end: 0.95, color: '#FFC107' },
span({ class: 'data' }, '15%'))
),
Chart.Row({},
Chart.Label({}, 'Other'),
Chart.Data({ start: 0.95, end: 1.0, color: '#607D8B' },
span({ class: 'data' }, '5%'))
)
)
);
$('#example').content(chart);
Axes
Add background lines to the pie chart with the secondaryAxesCount prop.
await import('/components/data-display/chart.js');
const { tags, $ } = Lightview;
const { Chart } = tags;
const chart = Chart({
type: 'pie',
secondaryAxesCount: 4,
style: 'width: 100%; max-width: 300px; margin: 0 auto; aspect-ratio: 1;'
},
Chart.Body({},
Chart.Row({}, Chart.Data({ start: 0.0, end: 0.45, color: '#00BCD4' })),
Chart.Row({}, Chart.Data({ start: 0.45, end: 0.8, color: '#8BC34A' })),
Chart.Row({}, Chart.Data({ start: 0.8, end: 1.0, color: '#FFC107' }))
)
);
$('#example').content(chart);
Calculating Values
Here's how to calculate --start and --end from
percentages:
| Segment | Percentage | --start | --end |
|---|---|---|---|
| First | 50% | 0.0 | 0.5 (0 + 0.5) |
| Second | 30% | 0.5 (prev end) | 0.8 (0.5 + 0.3) |
| Third | 20% | 0.8 (prev end) | 1.0 (0.8 + 0.2) |
// Helper function to convert percentages to pie chart values
function percentagesToPie(percentages) {
let cumulative = 0;
return percentages.map(pct => {
const start = cumulative;
cumulative += pct / 100;
return { start, end: cumulative };
});
}
// Example:
const slices = percentagesToPie([50, 30, 20]);
// Returns: [
// { start: 0, end: 0.5 },
// { start: 0.5, end: 0.8 },
// { start: 0.8, end: 1.0 }
// ]