Chart

Reference for the Chart module: declarative tools for drawing bar and pie charts. A callback records the data points; the module scales them and renders a bar chart as HTML or a pie chart as SVG, and offers a rotating spectrum of default colours.

This page is a pure reference. For how the framework fits together, see the Guide.

Classes
  • ChartBar - collects the data points for a bar chart.
  • ChartPie - collects the data points for a pie chart.
  • ChartUi - renders bar and pie charts and supplies default colours.

ChartBar

Collects the labelled, coloured, sized data points that make up a bar chart. The points are held on a shared static array; reset it, add points, then read them back.

Properties

PropertyTypeDescription
ChartBar::$dataarrayThe collected data points, each an object with label, colour and size.
Functions

Data

ChartBar::Data(string $label, string $colour, int $size): void

Add one bar to the collected data, with a label, a colour and a size.

NameTypeDescription
$labelstringThe bar's label.
$colourstringThe bar's colour.
$sizeintThe bar's value; must be zero or greater.

Returns void

GetData

ChartBar::GetData(): array

The bar data points collected so far.

Returns array A list of objects with label, colour and size.

Reset

ChartBar::Reset(): void

Clear the collected bar data points.

Returns void

ChartPie

Collects the labelled, coloured, sized data points that make up a pie chart. The points are held on a shared static array; reset it, add points, then read them back.

Properties

PropertyTypeDescription
ChartPie::$dataarrayThe collected data points, each an object with label, colour and size.
Functions

Data

ChartPie::Data(string $label, string $colour, int $size): void

Add one slice to the collected data, with a label, a colour and a size.

NameTypeDescription
$labelstringThe slice's label.
$colourstringThe slice's colour.
$sizeintThe slice's value; must be zero or greater.

Returns void

GetData

ChartPie::GetData(): array

The pie data points collected so far.

Returns array A list of objects with label, colour and size.

Reset

ChartPie::Reset(): void

Clear the collected pie data points.

Returns void

ChartUi

Renders bar and pie charts from data recorded by a callback, and hands out colours from a fixed spectrum. Bar charts are drawn as scaled HTML, pie charts as SVG slices.

Constants

ConstantValueDescription
ChartUi::BAR_CHART_HEIGHT300The default bar chart height in pixels.
ChartUi::PIE_CHART_SIZE300The default pie chart width and height in pixels.

Bar

ChartUi::Bar(callable $dataCallback, ?int $height = null): void

Render a bar chart. Runs the callback to collect the bars, scales them to the tallest value, and echoes the chart HTML with value and label markings. Renders nothing when no bars are collected.

NameTypeDescription
$dataCallbackcallableCalled to add bars with ChartBar::Data.
$height?intChart height in pixels, or null for the default height.

Returns void

GetNextColor

ChartUi::GetNextColor(): string

Get the next colour from the fixed spectrum, advancing the counter and wrapping around at the end.

Returns string A colour hex code.

Pie

ChartUi::Pie(callable $dataCallback, ?int $size = null): void

Render a pie chart. Runs the callback to collect the slices, sizes each slice by its share of the total, and echoes an SVG with labelled slices. Renders nothing when no slices are collected or the total is zero.

NameTypeDescription
$dataCallbackcallableCalled to add slices with ChartPie::Data.
$size?intChart width and height in pixels, or null for the default size.

Returns void

ResetColorCounter

ChartUi::ResetColorCounter(): void

Reset the colour counter so the next GetNextColor call starts from the beginning of the spectrum.

Returns void