-
Notifications
You must be signed in to change notification settings - Fork 2
ChartJs
Viames Marino edited this page Feb 26, 2026
·
2 revisions
Pair\Helpers\ChartJs builds Chart.js config from PHP, registers the script in Application, and returns the <canvas> element.
new ChartJs(string $type, ?string $elementId = null)Supported chart types: line, bar, radar, doughnut, pie, polarArea, bubble, scatter.
Validation:
- invalid type throws
AppException - invalid
elementIdthrowsAppException - if
elementIdis omitted, Pair generates one (chart-...)
Default options set in constructor:
responsive = trueresizeDelay = 250
Data and datasets:
labels(array $labels): selfdataset(ChartJsDataset $dataset): selfrangeLabels(DateTimeInterface $start, DateTimeInterface $end, string $interval, ?string $pattern = null): self
Layout and interaction:
animation(int $duration, string $easing = 'linear', ?string $onProgress = null, ?string $onComplete = null): selfaspectRatio(float $ratio): selfmaintainAspectRatio(bool $maintainAspectRatio): selfresponsive(bool $responsive): selfresizeDelay(int $milliseconds): selfhorizontal(): selfhover(string $mode, bool $intersect, int $animationDuration): selfinteraction(string $mode, bool $intersect, string $axis): selfstacked(bool $xStacked, bool $yStacked): selfpadding(array|int $padding): selfmargins(int $top, int $right, int $bottom, int $left): selfnormalized(bool $normalized): selfparsing(bool $parsing): selfoption(string $name, array|string|bool $value): self
Elements and plugins:
elementsArc(string $backgroundColor, string $borderColor, int $borderWidth): selfelementsPoint(int $radius, string $backgroundColor, string $borderColor, int $borderWidth): selfelementsRectangle(string $backgroundColor, string $borderColor, int $borderWidth): selflegend(string $position, string $align, array $config = []): selfhideLegend(): selftitle(string $text, array $config = []): selfsubtitle(string $text, array $config = []): selfplugin(string $pluginName, array $pluginOptions): selfdatalabels(array $pluginOptions): self
Performance for large line charts:
decimation(string $algorithm, int $samples, int $threshold): self
Rendering:
render(): stringrenderConfig(): string__toString(): string
CDN loaders:
ChartJs::load(string $version = ''): voidChartJs::loadDatalabels(string $version = ''): voidChartJs::loadGeo(): void
-
decimation(...)is allowed only forlinecharts and validates algorithm/samples/threshold. -
rangeLabels(...)requires current user and session (User::current()andSession::current()) and throwsAppExceptionotherwise. -
render()adds JS toApplication::addScript(...); include Chart.js before page output. -
renderConfig()returns pure JSON config, useful for debugging or custom JS handling.
Minimal line chart:
use Pair\Helpers\ChartJs;
use Pair\Helpers\ChartJsDataset;
ChartJs::load('4.4.1');
$chart = (new ChartJs('line', 'salesChart'))
->labels(['Jan', 'Feb', 'Mar', 'Apr'])
->dataset(
(new ChartJsDataset([120, 135, 128, 162], 'Sales'))
->borderColor('#2f80ed')
->borderWidth(2)
->fill(false)
->pointRadius(3)
->interpolation('monotone')
)
->title('Monthly sales')
->subtitle('FY 2026')
->legend('top', 'center')
->interaction('index', false, 'x');
echo $chart->render();Bar chart with stacked axes and datalabels plugin:
ChartJs::load();
ChartJs::loadDatalabels();
$chart = (new ChartJs('bar', 'channelsChart'))
->labels(['Organic', 'Paid', 'Referral'])
->dataset((new ChartJsDataset([320, 210, 95], 'Leads'))->backgroundColors(['#57A0E5', '#ED6E85', '#F1A454']))
->stacked(false, false)
->datalabels([
'anchor' => 'end',
'align' => 'top',
'color' => '#333'
])
->hideLegend();Decimation on large line series:
$chart = (new ChartJs('line', 'telemetryChart'))
->parsing(false)
->normalized(true)
->decimation('lttb', 300, 1000)
->resizeDelay(150);Use ChartJsDataset to configure series-level options (type, backgroundColors, colorFunction, interpolation, fill, etc.) before adding datasets via dataset(...).
See also: ChartJsDataset, Application, View.