Novis\Spreadsheet\Chart::combinedWith
Returns the chart with the type and the series of $other drawn on the same axes. Use it for two kinds of data in one chart, such as columns of the sales and a line of the costs. The chart you call it on does not change.
The title, the axis titles, the legend and labels come from the chart you call combinedWith on. The options of $other are not used.
Good to know: only column, line and area charts can be combined. A combined chart has two different types from these three. Sheet::addChart throws a LogicError for any other combination, such as a pie with a line, two lines, or three types.
The examples below draw columns of the sales with a line of the costs. Then they show that the first chart does not change, and show the error for three types in one chart.
Parameters
$otherChart
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
Columns of sales and a line of costs
<?nvs// Shows a chart with columns of the sales and a line of the costs.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], ['month' => 'March', 'sales' => 1380, 'costs' => 950],]);
// `combinedWith` returns a chart with the line on the same axes as the columns.string $months = $sheet->column('month');var $costs = Chart::line()->withSeries($sheet->column('costs'), { categories: $months, name: 'Costs' });var $chart = Chart::column({ title: 'Sales and costs', legend: 'bottom' }) ->withSeries($sheet->column('sales'), { categories: $months, name: 'Sales' }) ->combinedWith($costs);$sheet->addChart('E2', $chart);bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// `Core\Zip::read` returns the XML of the chart. The two lines below check that it has columns and a line.var $xml = Core\Zip::read($file, 'xl/charts/chart1.xml') as string;echo Core\Str::contains($xml, '<c:barChart>') ? 'columns' : 'no columns', "\n";echo Core\Str::contains($xml, '<c:lineChart>') ? 'and a line' : 'and no line', "\n";columnsand a lineThe first chart does not change
<?nvs// Shows that `combinedWith` returns a new chart and the first chart does not change.use Novis\Spreadsheet\Chart;
var $columns = Chart::column({ title: 'Sales' })->withSeries('B2:B4');var $line = Chart::line({ title: 'Costs' })->withSeries('C2:C4');var $both = $columns->combinedWith($line);
// `combined` has one entry for each chart that `combinedWith` added. This prints `0` and `1`.echo 'columns: ', Core\Arr::count($columns->combined), " added\n";echo 'both: ', Core\Arr::count($both->combined), " added\n";
// The title comes from the chart you call `combinedWith` on.echo $both->title ?? 'no title', "\n";columns: 0 addedboth: 1 addedSalesThree types in one chart
<?nvs// Shows the error for a chart that combines three types.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, 'target' => 1300], ['month' => 'February', 'sales' => 1450, 'costs' => 900, 'target' => 1400],]);
// A chart combines two types, so `Sheet::addChart` throws a `LogicError`.var $chart = Chart::column() ->withSeries($sheet->column('sales')) ->combinedWith(Chart::line()->withSeries($sheet->column('costs'))) ->combinedWith(Chart::area()->withSeries($sheet->column('target')));try { $sheet->addChart('E2', $chart);} catch (LogicError $e) { echo $e->message, "\n";}The chart at `E2` combines column and line and area. A chart combines two different types of column, line and area.