Skip to content

Novis\Spreadsheet\Chart::pie

Starts a pie chart and returns it. Each value of the first series is one slice, and the categories of the series are the names of the slices. Add the series with withSeries, then put the chart on a sheet with Sheet::addChart.

It takes the same options as Chart::column. A pie chart has no axes, so xTitle and yTitle are not shown. labels: true shows the value on each slice.

Good to know: give a pie chart one series. A pie chart does not combine with other types: Sheet::addChart throws a LogicError for a pie chart with combinedWith. A range with a sheet name, such as Sales!B2:B3, uses the cells of that sheet.

The examples below draw the sales of each shop, show the value on each slice, and draw a pie of the rows of another sheet.

Parameters

  • $optionsOptionsdefault null

Return value

Returns Chart.

Examples

Sales by shop

<?nvs
// Shows a pie chart with one slice for the sales of each shop.
use Novis\Spreadsheet\Chart;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Shops');
$sheet->addRows([
['shop' => 'North', 'sales' => 340],
['shop' => 'South', 'sales' => 210],
['shop' => 'East', 'sales' => 95],
]);
// Each value is one slice. The shop names are the names of the slices in the legend.
var $chart = Chart::pie({ title: 'Sales by shop' })
->withSeries($sheet->column('sales'), { categories: $sheet->column('shop') });
$sheet->addChart('D2', $chart);
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// `Core\Zip::read` returns the XML of the chart. These lines check what it contains.
var $xml = Core\Zip::read($file, 'xl/charts/chart1.xml') as string;
echo Core\Str::contains($xml, '<c:pieChart>') ? 'a pie chart' : 'another type', "\n";
echo Core\Str::contains($xml, '<c:valAx>') ? 'with axes' : 'without axes', "\n";
Output
a pie chart
without axes

The value on each slice

<?nvs
// Shows a pie chart with the value on each slice and no legend.
use Novis\Spreadsheet\Chart;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Survey');
$sheet->addRows([
['answer' => 'Yes', 'votes' => 64],
['answer' => 'No', 'votes' => 21],
['answer' => 'Not sure', 'votes' => 15],
]);
// `labels: true` shows each value on its slice. `legend: 'hidden'` removes the legend.
var $chart = Chart::pie({ legend: 'hidden', labels: true })
->withSeries($sheet->column('votes'), { categories: $sheet->column('answer') });
$sheet->addChart('D2', $chart);
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
var $xml = Core\Zip::read($file, 'xl/charts/chart1.xml') as string;
echo Core\Str::contains($xml, '<c:showVal val="1"/>') ? 'each slice shows its value' : 'no values', "\n";
echo Core\Str::contains($xml, '<c:legend>') ? 'a legend' : 'no legend', "\n";
Output
each slice shows its value
no legend

A pie of another sheet

<?nvs
// Shows a pie chart on a summary sheet that uses the rows of another sheet.
use Novis\Spreadsheet\Chart;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sales = $book->addSheet('Sales');
$sales->addRows([['shop' => 'North', 'sales' => 340], ['shop' => 'South', 'sales' => 210]]);
string $values = $sales->column('sales');
string $names = $sales->column('shop');
// A range with the name of a sheet, such as `Sales!B2:B3`, uses the cells of that sheet.
var $summary = $book->addSheet('Summary');
$summary->addRow(['note' => 'Sales of each shop']);
$summary->addChart('A3', Chart::pie()->withSeries("Sales!{$values}", { categories: "Sales!{$names}" }));
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
var $xml = Core\Zip::read($file, 'xl/charts/chart1.xml') as string;
echo Core\Str::contains($xml, '<c:f>Sales!$B$2:$B$3</c:f>') ? 'the slices are Sales!B2:B3' : 'other values', "\n";
Output
the slices are Sales!B2:B3