PHPackages                             bbsnly/chartjs-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. bbsnly/chartjs-php

ActivePackage[Utility &amp; Helpers](/categories/utility)

bbsnly/chartjs-php
==================

PHP wrapper for Chart.js library

v4.0.1(2y ago)27111.8k↓45.6%61MITPHPPHP &gt;=8.2CI passing

Since Jul 19Pushed 4d ago3 watchersCompare

[ Source](https://github.com/bbsnly/chartjs-php)[ Packagist](https://packagist.org/packages/bbsnly/chartjs-php)[ Docs](https://github.com/bbsnly/chartjs-php)[ RSS](/packages/bbsnly-chartjs-php/feed)WikiDiscussions main Synced 2w ago

READMEChangelog (10)Dependencies (1)Versions (16)Used By (1)

ChartJS-PHP
===========

[](#chartjs-php)

[![Contributor Covenant](https://camo.githubusercontent.com/4ae39ae593b602cf0ae07972b61c73728b77ec8e2cf40f579a2441948208036b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f436f6e7472696275746f72253230436f76656e616e742d322e312d3462616161612e737667)](CODE_OF_CONDUCT.md)[![Tests](https://github.com/bbsnly/chartjs-php/actions/workflows/php.yml/badge.svg)](https://github.com/bbsnly/chartjs-php/actions)[![codecov](https://camo.githubusercontent.com/8e620562c5338ac2a54d47b4994718dfe790d758cbab3e2b904fdb1ec0269536/68747470733a2f2f636f6465636f762e696f2f67682f6262736e6c792f63686172746a732d7068702f67726170682f62616467652e7376673f746f6b656e3d4d5449424e5038424454)](https://codecov.io/gh/bbsnly/chartjs-php)[![Total Downloads](https://camo.githubusercontent.com/aab1338a02e9862e97d769ef4984b8def21222e3295bbe536453cc1940f3a474/68747470733a2f2f706f7365722e707567782e6f72672f6262736e6c792f63686172746a732d7068702f642f746f74616c2e737667)](https://packagist.org/packages/bbsnly/chartjs-php)[![Latest Stable Version](https://camo.githubusercontent.com/9584d9eb1d1b8c2d93d583f1cd54e15ef68e548088cd4dff49922ca3c1c21b9a/68747470733a2f2f706f7365722e707567782e6f72672f6262736e6c792f63686172746a732d7068702f762f737461626c652e737667)](https://packagist.org/packages/bbsnly/chartjs-php)[![License](https://camo.githubusercontent.com/f2b5859773e42657f3e22bdd3a6503d6008dac56b99fa08cd3602a44ad005276/68747470733a2f2f706f7365722e707567782e6f72672f6262736e6c792f63686172746a732d7068702f6c6963656e73652e737667)](https://packagist.org/packages/bbsnly/chartjs-php)

This package transforms how you create [ChartJS](https://www.chartjs.org/ "ChartJS") elements by bringing them directly into PHP.

ChartJS-PHP eliminates the complexity of JavaScript when working with [ChartJS](https://www.chartjs.org/ "ChartJS") charts. While [ChartJS](https://www.chartjs.org/ "ChartJS") traditionally requires JavaScript implementation, our PHP solution delivers the same powerful charts through clean, efficient PHP code. By generating [ChartJS](https://www.chartjs.org/ "ChartJS") elements directly in PHP, you write less code, maintain cleaner codebases, and deliver faster results. This is the definitive solution for PHP developers building data visualizations, dashboards, or any application requiring dynamic charts.

**Note: Include the ChartJS library in your project as specified in their [official documentation](https://www.chartjs.org/docs/latest/getting-started/).**

Installation
------------

[](#installation)

Installing ChartJS-PHP is straightforward with [Composer](https://getcomposer.org/). Run this command in your project directory:

```
composer require bbsnly/chartjs-php
```

Minimum Requirements:

- PHP version: 8.1 or higher (tested up to PHP 8.5)
- ChartJS version: 3.0 or higher (4.x recommended)

Upgrading from v4? See the [upgrade guide](UPGRADE.md).

Usage
-----

[](#usage)

Creating charts with ChartJS-PHP is simple and intuitive. Start by instantiating the `Chart` class, define your data, and render your chart. The library handles all the complexity for you.

Choose from our specialized chart classes for even faster development: `BarChart`, `BubbleChart`, `DoughnutChart`, `LineChart`, `PieChart`, `PolarAreaChart`, `RadarChart`, and `ScatterChart`.

Here's how to create a line chart:

```
use Bbsnly\ChartJs\Chart;
use Bbsnly\ChartJs\Config\Data;
use Bbsnly\ChartJs\Config\Dataset;
use Bbsnly\ChartJs\Config\Options;

$chart = new Chart;
$chart->type = 'line';

$data = new Data();
$data->labels = ['Red', 'Green', 'Blue'];

$dataset = new Dataset();
$dataset->data = [5, 10, 20];
$data->datasets[] = $dataset;

$chart->data($data);

$options = new Options();
$options->responsive = true;
$chart->options($options);

$chart->get(); // Chart config as a PHP array
$chart->toJson(); // Chart config as JSON — use this when feeding Chart.js
$chart->toHtml('my_chart'); // HTML and JavaScript that render the chart
```

When passing the config to Chart.js yourself, always use `toJson()` (or `json_encode($chart)`): it encodes empty `data`/`options` as `{}` the way Chart.js expects. A plain `json_encode($chart->get())` would encode them as `[]`.

The `beginAtZero()` helper makes the y axis start at zero (Chart.js 3+ scale syntax) and merges with any scales you already configured:

```
$chart->beginAtZero(); // options.scales.y.beginAtZero = true
```

---

In the example below we will use the `toHtml` method to generate the HTML and JavaScript code for the chart.

```

    toHtml('my_chart'); ?>

```

A few things to know about `toHtml`:

- The element name must start with a letter and may only contain letters, digits, hyphens, and underscores; anything else throws an `InvalidArgumentException`.
- By default the canvas id gets a random suffix (e.g. `my_chart_a1b2c3d4e5f6`) so the same name can be rendered more than once per page. Pass `false` as the second argument — `$chart->toHtml('my_chart', false)` — to use the name verbatim, e.g. when your own CSS or JavaScript needs to target it.
- Every rendered chart instance is registered in `window.bbsnlyChartJSInstances`, keyed by canvas id, so you can access it from your own JavaScript.
- Rendering again into the same verbatim id (e.g. after a partial page swap with Turbo or htmx) destroys the previous chart instance before creating the new one.

---

In the example below we will use the `toJson` method to generate the JSON representation of the chart data.

```

  const ctx = document.getElementById('myChart');

  new Chart(ctx, toJson(); ?>);

```

### JavaScript callbacks

[](#javascript-callbacks)

JSON cannot carry functions, so Chart.js callbacks (tooltip formatters, tick callbacks, scriptable options) would normally arrive as dead strings. Wrap them in `JsExpression` and `toJson()`/`toHtml()` embed them as real JavaScript:

```
use Bbsnly\ChartJs\Config\JsExpression;

$options = new Options();
$options->plugins = [
    'tooltip' => [
        'callbacks' => [
            'label' => new JsExpression('function (context) { return " $" + context.parsed.y; }'),
        ],
    ],
];
```

**Security:** the wrapped code is emitted into the page unescaped — only wrap code you wrote yourself, never user input. Output containing a `JsExpression` is a JavaScript object literal rather than strict JSON: it works inline, but cannot be `JSON.parse`'d, and a plain `json_encode()` of the config (outside `toJson()`) degrades the expression to an ordinary string. One embedding caveat: because the expression bypasses JSON escaping, avoid the literal sequence `` anywhere inside it (even in its string literals) when rendering via `toHtml()` — the HTML parser would end the inline script block there.

Tests
-----

[](#tests)

Run the test suite and static analysis with:

```
composer test
composer analyse
```

Contributing
------------

[](#contributing)

Read our [Contributing](CONTRIBUTING.md) guidelines and start improving ChartJS-PHP today.

License
-------

[](#license)

The ChartJS PHP is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

###  Health Score

57

—

FairBetter than 98% of packages

Maintenance65

Regular maintenance activity

Popularity43

Moderate usage in the ecosystem

Community17

Small or concentrated contributor base

Maturity83

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 74.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~183 days

Recently: every ~375 days

Total

15

Last Release

747d ago

Major Versions

1.2.1 → 2.2.02019-03-12

1.2.2 → 2.3.02020-06-18

2.x-dev → 3.0.02020-06-18

3.0.1 → v4.0.02024-02-03

1.2.x-dev → v4.0.12024-07-31

PHP version history (4 changes)2.0.0PHP &gt;=7.0

2.1.1PHP &gt;=7.1

3.0.0PHP &gt;=7.4

v4.0.0PHP &gt;=8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8020430?v=4)[Anatoliy Babushka](/maintainers/bbsnly)[@bbsnly](https://github.com/bbsnly)

---

Top Contributors

[![bbsnly](https://avatars.githubusercontent.com/u/8020430?v=4)](https://github.com/bbsnly "bbsnly (99 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (33 commits)")[![TomSchenk](https://avatars.githubusercontent.com/u/12969197?v=4)](https://github.com/TomSchenk "TomSchenk (1 commits)")

---

Tags

analyticschart-generationchart-librarychartjschartsdata-visualizationgraphsphpphp-libraryphp-wrapperstatisticsphpchartjs

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/bbsnly-chartjs-php/health.svg)

```
[![Health](https://phpackages.com/badges/bbsnly-chartjs-php/health.svg)](https://phpackages.com/packages/bbsnly-chartjs-php)
```

###  Alternatives

[imanghafoori/laravel-anypass

A minimal yet powerful package to help you in development.

21524.3k](/packages/imanghafoori-laravel-anypass)[halfpastfouram/phpchartjs

PHP library for ChartJS

2512.4k](/packages/halfpastfouram-phpchartjs)[epessine/axis

Draw charts with a simple API on Laravel

267.9k](/packages/epessine-axis)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
