Novis\Spreadsheet\Sheet::addChart
$sheet->addChart(
string $cell,
Chart $chart,
?{ width?: float, height?: float } $options = null,
): SheetPuts the chart $chart at the cell $cell, and returns the sheet. You build the chart with Chart, such as Chart::column(), and add its series with withSeries.
The top-left corner of the chart is at the cell. width and height are its size in pixels. The default size is 480 by 288 pixels.
A series range is a range of cells in this workbook, such as B2:B9 or 'Shop orders'!B2:B9. A range in another file, a web address and a range over more than one sheet throw a LogicError. A title and an axis title have at most 255 characters.
Good to know: give each series a range from $sheet->column($key). The chart then also shows the rows you add after it. The file stores only the ranges of the chart. Excel reads the values from the cells. A CSV file has no charts, so addChart does nothing there.
Parameters
$cellstring$chartChart$options?{ width?: float, height?: float }default null
Return value
Returns Sheet.
Changelog
| Version | Change |
|---|---|
0.0.6 | Each Novis\Spreadsheet\Sheet and Workbook method that adds to the file says what it adds, for example addRow(), addChart() and addSheet(). setPrintOptions() replaces print(). Chart->withSeries() and combinedWith() return a new chart. |
Examples
A report with totals and a chart
<?nvs// Shows how to write a monthly report with a total row and a column chart.use Novis\Spreadsheet\Chart;use Novis\Spreadsheet\Formula;use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Sales');$sheet->addRows([ ['month' => 'January', 'sales' => 1200], ['month' => 'February', 'sales' => 1450], ['month' => 'March', 'sales' => 1380],]);
// The chart uses the months only, so its ranges are taken before the total row.var $chart = Chart::column({ title: 'Sales by month', legend: 'hidden' }) ->withSeries($sheet->column('sales'), { categories: $sheet->column('month'), name: 'Sales' });$sheet->addRow(['month' => 'Total', 'sales' => Formula::of('=SUM(' . $sheet->column('sales') . ')', { cached: 4030 })]);$sheet->addChart('D2', $chart, { width: 600.0 });
var $written = $book->finish();bytes $file = $written->bytes ?? Core\Bytes::fill(0, 0);echo $written->rows['Sales'], " rows\n";
// `Core\Zip::read` returns the XML of the chart. This line checks the range of its values.var $xml = Core\Zip::read($file, 'xl/charts/chart1.xml') as string;echo Core\Str::contains($xml, '<c:f>Sales!$B$2:$B$4</c:f>') ? 'the chart shows rows 2 to 4' : 'other rows', "\n";4 rowsthe chart shows rows 2 to 4A pie chart added before the last row
<?nvs// Shows that a chart added before the last row still shows every row.use Novis\Spreadsheet\Chart;use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Shops');$sheet->addRow(['shop' => 'North', 'sales' => 340]);
// Here `column('sales')` is `B2:B2`. A range from `column` grows to the last row of the sheet.$sheet->addChart('D2', Chart::pie({ title: 'Sales by shop' })->withSeries($sheet->column('sales'), { categories: $sheet->column('shop') }));$sheet->addRows([['shop' => 'South', 'sales' => 210], ['shop' => 'East', 'sales' => 95]]);bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// `Core\Zip::read` returns the XML of the chart. These lines check its ranges.var $xml = Core\Zip::read($file, 'xl/charts/chart1.xml') as string;echo Core\Str::contains($xml, '<c:f>Shops!$B$2:$B$4</c:f>') ? 'the values are B2:B4' : 'other values', "\n";echo Core\Str::contains($xml, '<c:f>Shops!$A$2:$A$4</c:f>') ? 'the names are A2:A4' : 'other names', "\n";the values are B2:B4the names are A2:A4Two types that cannot be combined
<?nvs// Shows the error for a chart that combines a pie chart with a line.use Novis\Spreadsheet\Chart;use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Sales');$sheet->addRows([['month' => 'January', 'sales' => 1200, 'costs' => 800], ['month' => 'February', 'sales' => 1450, 'costs' => 900]]);
// Only column, line and area charts can be combined. A pie chart cannot, so `addChart` throws a `LogicError`.var $chart = Chart::pie()->withSeries($sheet->column('sales'))->combinedWith(Chart::line()->withSeries($sheet->column('costs')));try { $sheet->addChart('E2', $chart);} catch (LogicError $e) { echo $e->message, "\n";}
// A column chart with a line is allowed.$sheet->addChart('E2', Chart::column()->withSeries($sheet->column('sales'))->combinedWith(Chart::line()->withSeries($sheet->column('costs'))));echo "the column chart with a line is added\n";The chart at `E2` combines pie and line. A chart combines two different types of column, line and area.the column chart with a line is added