ktulchu / yii2-chart
ActiveRecord time-series charts for Yii2
Package info
Language:JavaScript
Type:yii2-extension
pkg:composer/ktulchu/yii2-chart
Requires
- php: ^8.0
- yiisoft/yii2: ^2.0.50
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-18 14:55:57 UTC
README
A Composer-installable Yii2 widget for ActiveRecord time-series charts (PHP 8+, Chart.js).
Project background
This widget builds on charting needs and implementation experience from a private commercial Yii2 application. The public repository is a standalone Composer packaging of that work, not a publication of the original application. Its earlier development history remains in private commercial GitLab; public GitHub commits show the independent package's history.
Install
composer require ktulchu/yii2-chart:^0.1
Published on Packagist. Install the tagged v0.1.0 release with Composer; no custom VCS repository is required.
Usage
use app\models\AccessLog; use Ktulchu\Yii2Chart\Chartwidget; echo Chartwidget::widget([ 'options' => [ 'modelName' => AccessLog::class, 'fieldName' => 'created_at', 'start' => '2026-11-16', 'range' => 'day', 'aggregate' => 'count', ], ]);
Sum payment amounts per day of the selected week:
echo Chartwidget::widget([ 'options' => [ 'modelName' => \app\models\Payment::class, 'fieldName' => 'paid_at', 'start' => '2026-11-16', 'range' => 'week', 'aggregate' => 'sum', 'valueAttribute' => 'amount', ], 'type' => 'line', // bar (default), line, pie, doughnut ]);
One setting, two jobs: range determines both the calendar period and its grouping:
| range | Period containing start |
Buckets |
|---|---|---|
day |
Calendar day | 24 hours |
week |
Monday through Sunday | 7 days |
month |
Calendar month | 28–31 days |
year |
Calendar year | 12 months |
Missing intervals are filled with zeroes. One aggregate SQL GROUP BY query fetches the data; there is no per-bucket N+1.
Options
modelName(required): anyii\db\ActiveRecordclass.fieldName: model column with an integer Unix timestamp or SQL DATE/DATETIME/TIMESTAMP; defaults tocreated_at. The legacycreated_atoption remains available.start: a date in the selected period, asYYYY-MM-DDor a Unix timestamp; defaults to current date.range:day,week,month,year(default:week).aggregate:count(default),sum,avg,min,max.valueAttribute: a numeric column, required for aggregates other thancount.nameAttribute: optional second dataset with a count of non-null values of this column (legacy).end: optional inclusive timestamp/datetime in the selected calendar period.- Widget
type:bar(default),line,pie,doughnut;heightin pixels (default 260).
Requires Yii2 2.0.50+, PHP 8.0+, and MySQL/MariaDB or PostgreSQL. SQL column identifiers are validated against the ActiveRecord table schema. For Unix timestamp fields, PHP and the database session must use the same time zone for consistent date grouping (UTC recommended).
The widget loads pinned Chart.js 4.6.1 UMD from jsDelivr. This requires browser access to the CDN. Override \Ktulchu\Yii2Chart\ChartAssetsBundle in Yii's asset manager to serve a local copy for offline operation or strict CSP.
Tests
php tests/smoke.php
CI also runs real Yii2 integration tests against MySQL 8 and PostgreSQL 16. The dependency-free smoke test does not replace browser-level testing.
Later
- Query filtering and adapters
- Interactive period selector
- Independent analytics query builder