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.
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
| Property | Type | Description |
|---|---|---|
ChartBar::$data | array | The collected data points, each an object with label, colour and size. |
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.
| Name | Type | Description |
|---|---|---|
$label | string | The bar's label. |
$colour | string | The bar's colour. |
$size | int | The 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
| Property | Type | Description |
|---|---|---|
ChartPie::$data | array | The collected data points, each an object with label, colour and size. |
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.
| Name | Type | Description |
|---|---|---|
$label | string | The slice's label. |
$colour | string | The slice's colour. |
$size | int | The 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
| Constant | Value | Description |
|---|---|---|
ChartUi::BAR_CHART_HEIGHT | 300 | The default bar chart height in pixels. |
ChartUi::PIE_CHART_SIZE | 300 | The 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.
| Name | Type | Description |
|---|---|---|
$dataCallback | callable | Called to add bars with ChartBar::Data. |
$height | ?int | Chart 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.
| Name | Type | Description |
|---|---|---|
$dataCallback | callable | Called to add slices with ChartPie::Data. |
$size | ?int | Chart 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