Skip to content

Novis\Spreadsheet\Chart::line

Starts a line chart and returns it. Each series is one line that joins its values in the order of the rows. 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 line.

Good to know: a line chart is good for values over time, such as visitors per day. combinedWith can draw the line on the same axes as columns or an area. A chart with no series throws a LogicError when you put it on a sheet.

The examples below draw the visitors of each day, draw two lines with the legend at the bottom, and show the error for a chart without a series.

Parameters

  • $optionsOptionsdefault null

Return value

Returns Chart.

Examples

Visitors per day

<?nvs
// Shows a line chart of the visitors of a blog on each day.
use Novis\Spreadsheet\Chart;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Blog');
$sheet->addRows([
['day' => 'Monday', 'visitors' => 420],
['day' => 'Tuesday', 'visitors' => 515],
['day' => 'Wednesday', 'visitors' => 480],
]);
// A line joins the values of the series in the order of the rows.
var $chart = Chart::line({ title: 'Visitors', yTitle: 'Visitors per day' })
->withSeries($sheet->column('visitors'), { categories: $sheet->column('day') });
$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:lineChart>') ? 'a line chart' : 'another type', "\n";
echo Core\Str::contains($xml, '<c:f>Blog!$A$2:$A$4</c:f>') ? 'the days are A2:A4' : 'other days', "\n";
Output
a line chart
the days are A2:A4

Two lines and the legend at the bottom

<?nvs
// Shows a line chart with two lines and the legend under the chart.
use Novis\Spreadsheet\Chart;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Blog');
$sheet->addRows([
['day' => 'Monday', 'visitors' => 420, 'comments' => 31],
['day' => 'Tuesday', 'visitors' => 515, 'comments' => 44],
]);
// Each series is one line. The `name` of each line is in the legend.
var $chart = Chart::line({ legend: 'bottom' })
->withSeries($sheet->column('visitors'), { categories: $sheet->column('day'), name: 'Visitors' })
->withSeries($sheet->column('comments'), { categories: $sheet->column('day'), name: 'Comments', color: '#C00000' });
$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:legendPos val="b"/>') ? 'the legend is at the bottom' : 'the legend is elsewhere', "\n";
echo Core\Str::contains($xml, '<a:srgbClr val="C00000"/>') ? 'the comments line is red' : 'another color', "\n";
Output
the legend is at the bottom
the comments line is red

A line without a series

<?nvs
// Shows the error for a line chart that has no series.
use Novis\Spreadsheet\Chart;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Blog');
$sheet->addRow(['day' => 'Monday', 'visitors' => 420]);
// A chart needs at least one series, so `Sheet::addChart` throws a `LogicError`.
try {
$sheet->addChart('D2', Chart::line({ title: 'Visitors' }));
} catch (LogicError $e) {
echo $e->message, "\n";
}
Output
The line chart at `D2` has no series. Add one with `withSeries()`.