Skip to content

Novis\Spreadsheet\Chart::doughnut

Starts a doughnut chart and returns it. A doughnut chart is a pie chart with a hole in the middle. Each value of the series is one part of the ring. Add the series with withSeries, then put the chart on a sheet with Sheet::addChart.

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

Good to know: the hole is half the size of the doughnut. A doughnut chart does not combine with other types: Sheet::addChart throws a LogicError for a doughnut chart with combinedWith.

The examples below draw the share of each shop, put the legend on the left, and show the error for a doughnut combined with a pie.

Parameters

  • $optionsOptionsdefault null

Return value

Returns Chart.

Examples

Share of each shop

<?nvs
// Shows a doughnut chart of the share of each shop in the sales.
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],
]);
// A doughnut chart is a pie chart with a hole in the middle.
var $chart = Chart::doughnut({ title: 'Share of sales' })
->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:doughnutChart>') ? 'a doughnut chart' : 'another type', "\n";
echo Core\Str::contains($xml, '<c:holeSize val="50"/>') ? 'the hole is half the size of the doughnut' : 'another hole', "\n";
Output
a doughnut chart
the hole is half the size of the doughnut

The legend on the left

<?nvs
// Shows a doughnut chart with the legend on the left and the value on each part.
use Novis\Spreadsheet\Chart;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Tickets');
$sheet->addRows([
['state' => 'Open', 'count' => 12],
['state' => 'Waiting', 'count' => 5],
['state' => 'Closed', 'count' => 40],
]);
var $chart = Chart::doughnut({ legend: 'left', labels: true })
->withSeries($sheet->column('count'), { categories: $sheet->column('state'), name: 'Tickets' });
$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:legendPos val="l"/>') ? 'the legend is on the left' : 'the legend is elsewhere', "\n";
echo Core\Str::contains($xml, '<c:showVal val="1"/>') ? 'each part shows its value' : 'no values', "\n";
Output
the legend is on the left
each part shows its value

A doughnut does not combine

<?nvs
// Shows the error for a doughnut chart combined with a pie chart.
use Novis\Spreadsheet\Chart;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Shops');
$sheet->addRows([['shop' => 'North', 'sales' => 340, 'costs' => 200], ['shop' => 'South', 'sales' => 210, 'costs' => 150]]);
// Only column, line and area charts combine, so `Sheet::addChart` throws a `LogicError`.
var $chart = Chart::doughnut()->withSeries($sheet->column('sales'))->combinedWith(Chart::pie()->withSeries($sheet->column('costs')));
try {
$sheet->addChart('E2', $chart);
} catch (LogicError $e) {
echo $e->message, "\n";
}
Output
The chart at `E2` combines doughnut and pie. A chart combines two different types of column, line and area.