Skip to content

Novis\Spreadsheet\Chart::scatter

Starts a scatter chart, a chart of points, and returns it. Each row is one point. The categories of a series are the x values of its points, and the series values are the y values. 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 points.

Good to know: give every series categories, so each point has an x value. The chart reads the x values as numbers, so use a column of numbers. A scatter chart does not combine with other types.

The examples below draw the price of each product against the units sold, draw two groups of points in two colors, and show the error for a scatter chart combined with a line.

Parameters

  • $optionsOptionsdefault null

Return value

Returns Chart.

Examples

Price and units sold

<?nvs
// Shows a scatter chart of the price of each product and the units sold.
use Novis\Spreadsheet\Chart;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Products');
$sheet->addRows([
['price' => 4.5, 'sold' => 120],
['price' => 9.0, 'sold' => 75],
['price' => 15.0, 'sold' => 30],
]);
// Each row is one point. `categories` are the x values and the series values are the y values.
var $chart = Chart::scatter({ xTitle: 'Price', yTitle: 'Units sold', legend: 'hidden' })
->withSeries($sheet->column('sold'), { categories: $sheet->column('price') });
$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:scatterChart>') ? 'a scatter chart' : 'another type', "\n";
echo Core\Str::contains($xml, '<c:xVal><c:numRef><c:f>Products!$A$2:$A$4</c:f>') ? 'x is the price, A2:A4' : 'another x', "\n";
Output
a scatter chart
x is the price, A2:A4

Two groups of points

<?nvs
// Shows a scatter chart with two groups of points in two colors.
use Novis\Spreadsheet\Chart;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Visits');
$sheet->addRows([
['minutes' => 2, 'shop' => 1, 'web' => 0],
['minutes' => 5, 'shop' => 3, 'web' => 1],
['minutes' => 9, 'shop' => 4, 'web' => 3],
]);
// Both series use the minutes as x values. The `color` of a series is the color of its points.
string $minutes = $sheet->column('minutes');
var $chart = Chart::scatter({ xTitle: 'Minutes on the page', yTitle: 'Orders' })
->withSeries($sheet->column('shop'), { categories: $minutes, name: 'Shop', color: '#1F77B4' })
->withSeries($sheet->column('web'), { categories: $minutes, name: 'Web', color: '#FF7F0E' });
$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:symbol val="circle"/>') ? 'the points are circles' : 'other points', "\n";
echo Core\Str::contains($xml, '<a:srgbClr val="FF7F0E"/>') ? 'the web points are orange' : 'another color', "\n";
Output
the points are circles
the web points are orange

A scatter does not combine

<?nvs
// Shows the error for a scatter chart combined with a line.
use Novis\Spreadsheet\Chart;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Products');
$sheet->addRows([['price' => 4.5, 'sold' => 120, 'goal' => 100], ['price' => 9.0, 'sold' => 75, 'goal' => 80]]);
// Only column, line and area charts combine, so `Sheet::addChart` throws a `LogicError`.
var $points = Chart::scatter()->withSeries($sheet->column('sold'), { categories: $sheet->column('price') });
try {
$sheet->addChart('E2', $points->combinedWith(Chart::line()->withSeries($sheet->column('goal'))));
} catch (LogicError $e) {
echo $e->message, "\n";
}
Output
The chart at `E2` combines scatter and line. A chart combines two different types of column, line and area.