Skip to content

Novis\Spreadsheet\Chart::column

Starts a column chart, a chart of vertical bars, and returns it. The chart has no data yet: add each series with withSeries, then put the chart on a sheet with Sheet::addChart.

Every option is optional. title is the title of the chart. xTitle is the title under the bars, and yTitle is the title beside them. legend is 'right', 'top', 'bottom', 'left' or 'hidden', and the default is 'right'. labels: true shows the value of each bar.

Good to know: a legend with other text throws a LogicError. A chart does not change. Each method returns a new chart. combinedWith can draw a line or an area on the same axes as the columns.

The examples below draw the sales of each month, add axis titles and values, and show the error for a legend that is not allowed.

Parameters

  • $optionsOptionsdefault null

Return value

Returns Chart.

Examples

Sales by month

<?nvs
// Shows a column chart of the sales of each month.
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],
['month' => 'March', 'sales' => 1380],
]);
// The months are the labels under the columns.
var $chart = Chart::column({ title: 'Sales by month' })
->withSeries($sheet->column('sales'), { categories: $sheet->column('month'), name: 'Sales' });
$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:barDir val="col"/>') ? 'vertical bars' : 'another type', "\n";
echo Core\Str::contains($xml, '<a:t>Sales by month</a:t>') ? 'the title is written' : 'no title', "\n";
Output
vertical bars
the title is written

Axis titles and values on the bars

<?nvs
// Shows a column chart with a title on each axis and the value on each bar.
use Novis\Spreadsheet\Chart;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Stock');
$sheet->addRows([
['item' => 'Pens', 'qty' => 40],
['item' => 'Pads', 'qty' => 25],
['item' => 'Ink', 'qty' => 12],
]);
// `xTitle` is under the bars and `yTitle` is beside them. `labels: true` shows each value.
var $chart = Chart::column({ xTitle: 'Item', yTitle: 'In stock', labels: true })
->withSeries($sheet->column('qty'), { categories: $sheet->column('item') });
$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, '<a:t>Item</a:t>') ? 'the x axis has a title' : 'no x title', "\n";
echo Core\Str::contains($xml, '<a:t>In stock</a:t>') ? 'the y axis has a title' : 'no y title', "\n";
echo Core\Str::contains($xml, '<c:showVal val="1"/>') ? 'each bar shows its value' : 'no values', "\n";
Output
the x axis has a title
the y axis has a title
each bar shows its value

A legend that is not allowed

<?nvs
// Shows the error for a legend position that is not allowed.
use Novis\Spreadsheet\Chart;
// `legend` is 'right', 'top', 'bottom', 'left' or 'hidden'. Other text throws a `LogicError`.
try {
Chart::column({ legend: 'middle' });
} catch (LogicError $e) {
echo $e->message, "\n";
}
// The properties of the chart show the options it has.
var $chart = Chart::column({ title: 'Orders', legend: 'top' });
echo $chart->title ?? 'no title', ', labels: ', $chart->labels ? 'yes' : 'no', "\n";
Output
The chart's `legend` is `'right'`, `'top'`, `'bottom'`, `'left'` or `'hidden'`. It has `'middle'`.
Orders, labels: no