Novis\Spreadsheet\Chart::withSeries
$chart->withSeries(string $values, SeriesOptions $options = null): Chart$chart->withSeries( string $values, SeriesOptions $options = null, ): Chart
Returns the chart with one more series. $values is the range of the values, such as B2:B10. The chart you call it on does not change, so keep the chart that withSeries returns.
Every option is optional. categories is the range of the labels under the values, or the x values of a scatter chart. name is the name of the series in the legend. color is a color such as '#1F77B4'.
Good to know: use $sheet->column($key) for each range. The chart then also shows the rows you add after it. A range of another sheet starts with the sheet name, such as Sales!B2:B10. Sheet::addChart checks each range and color, and throws a LogicError for one that is not correct.
The examples below add one series for each shop, and use the rows of a sheet whose name has a space. The last one shows the errors for a range and a color that are not correct.
Parameters
$valuesstring$optionsSeriesOptionsdefault null
Return value
Returns Chart.
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 chart with three series
<?nvs// Shows a column chart with one series for each shop.use Novis\Spreadsheet\Chart;use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Sales');$sheet->addRows([ ['month' => 'January', 'north' => 340, 'south' => 210, 'east' => 95], ['month' => 'February', 'north' => 360, 'south' => 190, 'east' => 120],]);
// `withSeries` returns a new chart. This loop adds one series for each shop.string $months = $sheet->column('month');var $shops = [{ key: 'north', name: 'North' }, { key: 'south', name: 'South' }, { key: 'east', name: 'East' }];var $chart = Chart::column({ title: 'Sales by shop' });foreach ($shops as var $shop) { $chart = $chart->withSeries($sheet->column($shop->key), { categories: $months, name: $shop->name });}echo Core\Arr::count($chart->series), " series\n";echo $chart->series[2]?->name ?? 'no name', ' is ', $chart->series[2]?->values ?? 'no range', "\n";$sheet->addChart('F2', $chart);echo "the chart is added\n";3 seriesEast is D2:D3the chart is addedA series of a sheet with a space
<?nvs// Shows a series that uses the rows of a sheet whose name has a space.use Novis\Spreadsheet\Chart;use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $orders = $book->addSheet('Shop orders');$orders->addRows([['day' => 'Monday', 'orders' => 14], ['day' => 'Tuesday', 'orders' => 19]]);string $values = $orders->column('orders');
// A sheet name with a space goes in single quotes, such as `'Shop orders'!B2:B3`.var $summary = $book->addSheet('Summary');$summary->addRow(['note' => 'Orders of each day']);$summary->addChart('A3', Chart::line()->withSeries("'Shop orders'!{$values}", { name: 'Orders' }));bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// `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>'Shop orders'!\$B\$2:\$B\$3</c:f>") ? 'the values are on Shop orders' : 'other values', "\n";the values are on Shop ordersA range that is not a range
<?nvs// Shows the errors for a series whose range or color is not correct.use Novis\Spreadsheet\Chart;use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Sales');$sheet->addRows([['month' => 'January', 'sales' => 1200], ['month' => 'February', 'sales' => 1450]]);
// `withSeries` only stores the text. `Sheet::addChart` checks it and throws an error.try { $sheet->addChart('D2', Chart::column()->withSeries('sales'));} catch (LogicError $e) { echo $e->message, "\n";}try { $sheet->addChart('D2', Chart::column()->withSeries($sheet->column('sales'), { color: 'blue' }));} catch (LogicError $e) { echo $e->message, "\n";}The chart range `sales` is not a range of cells in this workbook, such as `B2:B10` or `'Shop'!B2:B10`.`blue` is not a color. Write a color as `#RRGGBB`, such as `#FF0000` for red.