Novis\Spreadsheet\Chart::bar
Novis\Spreadsheet\Chart::bar(Options $options = null): ChartNovis\Spreadsheet\Chart::bar( Options $options = null, ): Chart
Starts a bar chart, a chart of horizontal bars, and returns it. 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. A bar chart is good for long labels, such as the names of shops or products, because each label has a full line beside its bar.
Good to know: a bar chart does not combine with other types. Sheet::addChart throws a LogicError for a bar chart with combinedWith. To draw a line over bars, use Chart::column.
The examples below draw the sales of each shop, draw two years side by side, and show the error for bars combined with a line.
Parameters
$optionsOptionsdefault null
Return value
Returns Chart.
Examples
Shops ranked by sales
<?nvs// Shows a bar chart of the sales of each shop.use Novis\Spreadsheet\Chart;use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Shops');$sheet->addRows([ ['shop' => 'North', 'sales' => 340], ['shop' => 'South', 'sales' => 210], ['shop' => 'East', 'sales' => 95],]);
// A bar chart draws each value as a horizontal bar. The shop names are beside the bars.var $chart = Chart::bar({ title: 'Sales by shop', legend: 'hidden' }) ->withSeries($sheet->column('sales'), { categories: $sheet->column('shop') });$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="bar"/>') ? 'horizontal bars' : 'another type', "\n";echo Core\Str::contains($xml, '<c:legend>') ? 'a legend' : 'no legend', "\n";horizontal barsno legendThis year and last year
<?nvs// Shows a bar chart with two series, one bar for each year.use Novis\Spreadsheet\Chart;use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Shops');$sheet->addRows([ ['shop' => 'North', 'now' => 340, 'before' => 300], ['shop' => 'South', 'now' => 210, 'before' => 260],]);
// Each series has a name for the legend and its own color.var $chart = Chart::bar({ legend: 'bottom' }) ->withSeries($sheet->column('now'), { categories: $sheet->column('shop'), name: 'This year', color: '#1F77B4' }) ->withSeries($sheet->column('before'), { categories: $sheet->column('shop'), name: 'Last year', color: '#AEC7E8' });echo Core\Arr::count($chart->series), " series\n";$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:v>Last year</c:v>') ? 'the legend names Last year' : 'no name', "\n";echo Core\Str::contains($xml, '<a:srgbClr val="AEC7E8"/>') ? 'its bars are light blue' : 'another color', "\n";2 seriesthe legend names Last yearits bars are light blueBars do not combine with a line
<?nvs// Shows the error for a bar chart combined with a line.use Novis\Spreadsheet\Chart;use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Shops');$sheet->addRows([['shop' => 'North', 'sales' => 340, 'costs' => 200], ['shop' => 'South', 'sales' => 210, 'costs' => 150]]);
// Only column, line and area charts combine. `Sheet::addChart` throws a `LogicError` for bars.var $chart = Chart::bar()->withSeries($sheet->column('sales'))->combinedWith(Chart::line()->withSeries($sheet->column('costs')));try { $sheet->addChart('E2', $chart);} catch (LogicError $e) { echo $e->message, "\n";}The chart at `E2` combines bar and line. A chart combines two different types of column, line and area.