Skip to content

Novis\Spreadsheet\Chart::area

Starts an area chart and returns it. An area chart is a line chart with the space under each line filled. Add each series with withSeries, then put the chart on a sheet with Sheet::addChart.

It takes the same options as Chart::column: title, xTitle, yTitle, legend and labels. The color of a series is the color of its area.

Good to know: combinedWith can draw an area on the same axes as columns or a line, such as a target with the real sales. Two area charts do not combine. Add a second series to one area chart instead.

The examples below draw the orders of each quarter, draw columns and an area, and show the error for two area charts combined.

Parameters

  • $optionsOptionsdefault null

Return value

Returns Chart.

Examples

Orders over the year

<?nvs
// Shows an area chart of the orders of a shop in each quarter.
use Novis\Spreadsheet\Chart;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Orders');
$sheet->addRows([
['quarter' => 'Q1', 'orders' => 310],
['quarter' => 'Q2', 'orders' => 290],
['quarter' => 'Q3', 'orders' => 405],
['quarter' => 'Q4', 'orders' => 520],
]);
// An area chart is a line chart with the space under the line filled.
var $chart = Chart::area({ title: 'Orders per quarter', legend: 'hidden' })
->withSeries($sheet->column('orders'), { categories: $sheet->column('quarter'), color: '#2CA02C' });
$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:areaChart>') ? 'an area chart' : 'another type', "\n";
echo Core\Str::contains($xml, '<c:f>Orders!$B$2:$B$5</c:f>') ? 'the values are B2:B5' : 'other values', "\n";
Output
an area chart
the values are B2:B5

Columns and an area for the target

<?nvs
// Shows columns of the real sales and an area of the target in one chart.
use Novis\Spreadsheet\Chart;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Sales');
$sheet->addRows([
['month' => 'January', 'sales' => 1200, 'target' => 1300],
['month' => 'February', 'sales' => 1450, 'target' => 1400],
]);
// `combinedWith` returns a chart with the area on the same axes as the columns.
var $target = Chart::area()->withSeries($sheet->column('target'), { categories: $sheet->column('month'), name: 'Target' });
var $chart = Chart::column({ title: 'Sales and target' })
->withSeries($sheet->column('sales'), { categories: $sheet->column('month'), name: 'Sales' })
->combinedWith($target);
$sheet->addChart('E2', $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:barChart>') ? 'columns' : 'no columns', "\n";
echo Core\Str::contains($xml, '<c:areaChart>') ? 'and an area' : 'and no area', "\n";
Output
columns
and an area

Two areas do not combine

<?nvs
// Shows the error for an area chart combined with another area chart.
use Novis\Spreadsheet\Chart;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Orders');
$sheet->addRows([['quarter' => 'Q1', 'shop' => 310, 'web' => 120], ['quarter' => 'Q2', 'shop' => 290, 'web' => 180]]);
// A combined chart has two different types, so `Sheet::addChart` throws a `LogicError`.
var $chart = Chart::area()->withSeries($sheet->column('shop'))->combinedWith(Chart::area()->withSeries($sheet->column('web')));
try {
$sheet->addChart('E2', $chart);
} catch (LogicError $e) {
echo $e->message, "\n";
}
// Two series in one area chart are allowed.
$sheet->addChart('E2', Chart::area()->withSeries($sheet->column('shop'))->withSeries($sheet->column('web')));
echo "the area chart with two series is added\n";
Output
The chart at `E2` combines area and area. A chart combines two different types of column, line and area.
the area chart with two series is added