├── assets ├── dist │ ├── controller.d.ts │ └── controller.js ├── LICENSE ├── README.md └── package.json ├── src ├── Builder │ ├── ChartBuilderInterface.php │ └── ChartBuilder.php ├── ChartjsBundle.php ├── Twig │ └── ChartExtension.php ├── DependencyInjection │ └── ChartjsExtension.php └── Model │ └── Chart.php ├── LICENSE ├── README.md ├── composer.json └── CHANGELOG.md /assets/dist/controller.d.ts: -------------------------------------------------------------------------------- 1 | import { Controller } from '@hotwired/stimulus'; 2 | 3 | declare class export_default extends Controller { 4 | readonly viewValue: any; 5 | static values: { 6 | view: ObjectConstructor; 7 | }; 8 | private chart; 9 | connect(): void; 10 | disconnect(): void; 11 | viewValueChanged(): void; 12 | private dispatchEvent; 13 | } 14 | 15 | export { export_default as default }; 16 | -------------------------------------------------------------------------------- /src/Builder/ChartBuilderInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Symfony\UX\Chartjs\Builder; 13 | 14 | use Symfony\UX\Chartjs\Model\Chart; 15 | 16 | /** 17 | * @author Titouan Galopin 18 | */ 19 | interface ChartBuilderInterface 20 | { 21 | public function createChart(string $type): Chart; 22 | } 23 | -------------------------------------------------------------------------------- /src/ChartjsBundle.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Symfony\UX\Chartjs; 13 | 14 | use Symfony\Component\HttpKernel\Bundle\Bundle; 15 | 16 | /** 17 | * @author Titouan Galopin 18 | * 19 | * @final 20 | */ 21 | class ChartjsBundle extends Bundle 22 | { 23 | public function getPath(): string 24 | { 25 | return \dirname(__DIR__); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Builder/ChartBuilder.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Symfony\UX\Chartjs\Builder; 13 | 14 | use Symfony\UX\Chartjs\Model\Chart; 15 | 16 | /** 17 | * @author Titouan Galopin 18 | * 19 | * @final 20 | */ 21 | class ChartBuilder implements ChartBuilderInterface 22 | { 23 | public function createChart(string $type): Chart 24 | { 25 | return new Chart($type); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2020-present Fabien Potencier 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /assets/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2020-present Fabien Potencier 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Symfony UX Chart.js 2 | 3 | Symfony UX Chart.js is a Symfony bundle integrating the [Chart.js](https://www.chartjs.org/) 4 | library in Symfony applications. It is part of [the Symfony UX initiative](https://ux.symfony.com/). 5 | 6 | **This repository is a READ-ONLY sub-tree split**. See 7 | https://github.com/symfony/ux to create issues or submit pull requests. 8 | 9 | ## Sponsor 10 | 11 | The Symfony UX packages are [backed][1] by [Mercure.rocks][2]. 12 | 13 | Create real-time experiences in minutes! Mercure.rocks provides a realtime API service 14 | that is tightly integrated with Symfony: create UIs that update in live with UX Turbo, 15 | send notifications with the Notifier component, expose async APIs with API Platform and 16 | create low level stuffs with the Mercure component. We maintain and scale the complex 17 | infrastructure for you! 18 | 19 | Help Symfony by [sponsoring][3] its development! 20 | 21 | ## Resources 22 | 23 | - [Documentation](https://symfony.com/bundles/ux-chartjs/current/index.html) 24 | - [Report issues](https://github.com/symfony/ux/issues) and 25 | [send Pull Requests](https://github.com/symfony/ux/pulls) 26 | in the [main Symfony UX repository](https://github.com/symfony/ux) 27 | 28 | [1]: https://symfony.com/backers 29 | [2]: https://mercure.rocks 30 | [3]: https://symfony.com/sponsor 31 | -------------------------------------------------------------------------------- /assets/README.md: -------------------------------------------------------------------------------- 1 | # @symfony/ux-chartjs 2 | 3 | JavaScript assets of the [symfony/ux-chartjs](https://packagist.org/packages/symfony/ux-chartjs) PHP package. 4 | 5 | ## Installation 6 | 7 | This npm package is **reserved for advanced users** who want to decouple their JavaScript dependencies from their PHP dependencies (e.g., when building Docker images, running JavaScript-only pipelines, etc.). 8 | 9 | We **strongly recommend not installing this package directly**, but instead install the PHP package [symfony/ux-chartjs](https://packagist.org/packages/symfony/ux-chartjs) in your Symfony application with [Flex](https://github.com/symfony/flex) enabled. 10 | 11 | If you still want to install this package directly, please make sure its version exactly matches [symfony/ux-chartjs](https://packagist.org/packages/symfony/ux-chartjs) PHP package version: 12 | ```shell 13 | composer require symfony/ux-chartjs:2.23.0 14 | npm add @symfony/ux-chartjs@2.23.0 15 | ``` 16 | 17 | **Tip:** Your `package.json` file will be automatically modified by [Flex](https://github.com/symfony/flex) when installing or upgrading a PHP package. To prevent this behavior, ensure to **use at least Flex 1.22.0 or 2.5.0**, and run `composer config --json "extra.symfony/flex.synchronize_package_json" false`. 18 | 19 | ## Resources 20 | 21 | - [Documentation](https://symfony.com/bundles/ux-chartjs/current/index.html) 22 | - [Report issues](https://github.com/symfony/ux/issues) and 23 | [send Pull Requests](https://github.com/symfony/ux/pulls) 24 | in the [main Symfony UX repository](https://github.com/symfony/ux) 25 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "symfony/ux-chartjs", 3 | "type": "symfony-bundle", 4 | "description": "Chart.js integration for Symfony", 5 | "keywords": [ 6 | "symfony-ux" 7 | ], 8 | "homepage": "https://symfony.com", 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Titouan Galopin", 13 | "email": "galopintitouan@gmail.com" 14 | }, 15 | { 16 | "name": "Symfony Community", 17 | "homepage": "https://symfony.com/contributors" 18 | } 19 | ], 20 | "autoload": { 21 | "psr-4": { 22 | "Symfony\\UX\\Chartjs\\": "src/" 23 | } 24 | }, 25 | "autoload-dev": { 26 | "psr-4": { 27 | "Symfony\\UX\\Chartjs\\Tests\\": "tests/" 28 | } 29 | }, 30 | "require": { 31 | "php": ">=8.1", 32 | "symfony/config": "^5.4|^6.0|^7.0|^8.0", 33 | "symfony/dependency-injection": "^5.4|^6.0|^7.0|^8.0", 34 | "symfony/http-kernel": "^5.4|^6.0|^7.0|^8.0", 35 | "symfony/stimulus-bundle": "^2.9.1" 36 | }, 37 | "require-dev": { 38 | "symfony/framework-bundle": "^5.4|^6.0|^7.0|^8.0", 39 | "symfony/phpunit-bridge": "^5.4|^6.0|^7.0|^8.0", 40 | "symfony/twig-bundle": "^5.4|^6.0|^7.0|^8.0", 41 | "symfony/var-dumper": "^5.4|^6.0|^7.0|^8.0" 42 | }, 43 | "conflict": { 44 | "symfony/flex": "<1.13" 45 | }, 46 | "extra": { 47 | "thanks": { 48 | "name": "symfony/ux", 49 | "url": "https://github.com/symfony/ux" 50 | } 51 | }, 52 | "minimum-stability": "dev" 53 | } 54 | -------------------------------------------------------------------------------- /assets/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@symfony/ux-chartjs", 3 | "description": "Chart.js integration for Symfony", 4 | "license": "MIT", 5 | "version": "2.31.0", 6 | "keywords": [ 7 | "symfony-ux" 8 | ], 9 | "homepage": "https://ux.symfony.com/chartjs", 10 | "repository": "https://github.com/symfony/ux-chartjs", 11 | "type": "module", 12 | "files": [ 13 | "dist" 14 | ], 15 | "main": "dist/controller.js", 16 | "types": "dist/controller.d.ts", 17 | "scripts": { 18 | "build": "tsx ../../../bin/build_package.ts .", 19 | "watch": "tsx ../../../bin/build_package.ts . --watch", 20 | "test": "pnpm run test:unit && pnpm run test:browser", 21 | "test:unit": "../../../bin/unit_test_package.sh .", 22 | "test:browser": "playwright test", 23 | "test:browser:ui": "playwright test --ui", 24 | "check": "biome check", 25 | "ci": "biome ci" 26 | }, 27 | "symfony": { 28 | "controllers": { 29 | "chart": { 30 | "main": "dist/controller.js", 31 | "webpackMode": "eager", 32 | "fetch": "eager", 33 | "enabled": true 34 | } 35 | }, 36 | "importmap": { 37 | "@hotwired/stimulus": "^3.0.0", 38 | "chart.js": "^3.4.1 || ^4.0" 39 | } 40 | }, 41 | "peerDependencies": { 42 | "@hotwired/stimulus": "^3.0.0", 43 | "chart.js": "^3.4.1 || ^4.0" 44 | }, 45 | "devDependencies": { 46 | "@hotwired/stimulus": "^3.0.0", 47 | "@testing-library/dom": "^10.4.0", 48 | "@testing-library/jest-dom": "^6.6.3", 49 | "@testing-library/user-event": "^14.6.1", 50 | "chart.js": "^3.4.1 || ^4.0", 51 | "jsdom": "^26.1.0", 52 | "resize-observer-polyfill": "^1.5.1", 53 | "tslib": "^2.8.1", 54 | "tsx": "^4.20.3", 55 | "typescript": "^5.8.3", 56 | "vitest": "^3.2.4", 57 | "vitest-canvas-mock": "^0.3.3" 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # CHANGELOG 2 | 3 | ## 2.30 4 | 5 | - Ensure compatibility with PHP 8.5 6 | 7 | ## 2.29.0 8 | 9 | - Add Symfony 8 support 10 | 11 | ## 2.23.0 12 | 13 | - Listen to Stimulus `disconnect` event to destroy the chart #1944 14 | 15 | ## 2.18.0 16 | 17 | - Replace `chartjs/auto` import with explicit `Chart.register()` call #1263 18 | 19 | ## 2.17.0 20 | 21 | - Add `chartjs:view-value-change` event #1605 22 | 23 | ## 2.15.0 24 | 25 | - Remove restriction that prevented Chart.js 3.9 #1518 26 | 27 | ## 2.14.0 28 | 29 | - Add support for Chart.js version 4 30 | 31 | ## 2.13.2 32 | 33 | - Change "module" key back to "main" in package.json 34 | 35 | ## 2.13.1 36 | 37 | - Revert "Change JavaScript package to `type: module`" 38 | 39 | ## 2.13.0 40 | 41 | - Add Symfony 7 support. 42 | - Change JavaScript package to `type: module` 43 | 44 | ## 2.9.0 45 | 46 | - Add support for symfony/asset-mapper 47 | 48 | - Add dependency on symfony/stimulus-bundle 49 | 50 | - Minimum required PHP version is now 8.1. 51 | 52 | - Minimum required Symfony version is now 5.4. 53 | 54 | ## 2.8.0 55 | 56 | - The chart will now automatically re-render if the `view` Stimulus value 57 | (i.e. the `data-symfony--ux-chartjs--chart-view-value` attribute) changes. 58 | This makes Chart.js work perfectly inside of a LiveComponent. 59 | 60 | ## 2.7.0 61 | 62 | - The `chartjs:connect` JavaScript event now bubbles up. 63 | 64 | - Add `assets/src` to `.gitattributes` to exclude source TypeScript files from 65 | installing. 66 | 67 | - TypeScript types are now included. 68 | 69 | ## 2.6.0 70 | 71 | - [BC BREAK] The `assets/` directory was moved from `Resources/assets/` to `assets/`. Make 72 | sure the path in your `package.json` file is updated accordingly. 73 | 74 | - The directory structure of the bundle was updated to match modern best-practices. 75 | 76 | ## 2.0 77 | 78 | - Support for `stimulus` version 2 was removed and support for `@hotwired/stimulus` 79 | version 3 was added. See the [@symfony/stimulus-bridge CHANGELOG](https://github.com/symfony/stimulus-bridge/blob/main/CHANGELOG.md#300) 80 | for more details. 81 | - Support added for Symfony 6 82 | - Upgrade Chart.js to version 3 83 | 84 | ## 1.3 85 | 86 | - [DEPENDENCY CHANGE] `chart.js` is no longer included automatically (#93) 87 | but `symfony/flex` will automatically add it to your `package.json` file 88 | when upgrading. Additionally `symfony/flex` 1.13 or higher is now required 89 | if installed. 90 | 91 | - Chart.js: add horizontalBar type to chart model #87 - @duboiss 92 | -------------------------------------------------------------------------------- /assets/dist/controller.js: -------------------------------------------------------------------------------- 1 | // src/controller.ts 2 | import { Controller } from "@hotwired/stimulus"; 3 | import { Chart, registerables } from "chart.js"; 4 | if (registerables) { 5 | Chart.register(...registerables); 6 | } 7 | var isChartInitialized = false; 8 | var controller_default = class extends Controller { 9 | constructor() { 10 | super(...arguments); 11 | this.chart = null; 12 | } 13 | connect() { 14 | if (!isChartInitialized) { 15 | isChartInitialized = true; 16 | this.dispatchEvent("init", { 17 | Chart 18 | }); 19 | } 20 | if (!(this.element instanceof HTMLCanvasElement)) { 21 | throw new Error("Invalid element"); 22 | } 23 | const payload = this.viewValue; 24 | if (Array.isArray(payload.options) && 0 === payload.options.length) { 25 | payload.options = {}; 26 | } 27 | this.dispatchEvent("pre-connect", { 28 | options: payload.options, 29 | config: payload 30 | }); 31 | const canvasContext = this.element.getContext("2d"); 32 | if (!canvasContext) { 33 | throw new Error("Could not getContext() from Element"); 34 | } 35 | this.chart = new Chart(canvasContext, payload); 36 | this.dispatchEvent("connect", { chart: this.chart }); 37 | } 38 | disconnect() { 39 | this.dispatchEvent("disconnect", { chart: this.chart }); 40 | if (this.chart) { 41 | this.chart.destroy(); 42 | this.chart = null; 43 | } 44 | } 45 | /** 46 | * If the underlying data or options change, let's update the chart! 47 | */ 48 | viewValueChanged() { 49 | if (this.chart) { 50 | const viewValue = { data: this.viewValue.data, options: this.viewValue.options }; 51 | if (Array.isArray(viewValue.options) && 0 === viewValue.options.length) { 52 | viewValue.options = {}; 53 | } 54 | this.dispatchEvent("view-value-change", viewValue); 55 | this.chart.data = viewValue.data; 56 | this.chart.options = viewValue.options; 57 | this.chart.update(); 58 | const parentElement = this.element.parentElement; 59 | if (parentElement && this.chart.options.responsive) { 60 | const originalWidth = parentElement.style.width; 61 | parentElement.style.width = `${parentElement.offsetWidth + 1}px`; 62 | setTimeout(() => { 63 | parentElement.style.width = originalWidth; 64 | }, 0); 65 | } 66 | } 67 | } 68 | dispatchEvent(name, payload) { 69 | this.dispatch(name, { detail: payload, prefix: "chartjs" }); 70 | } 71 | }; 72 | controller_default.values = { 73 | view: Object 74 | }; 75 | export { 76 | controller_default as default 77 | }; 78 | -------------------------------------------------------------------------------- /src/Twig/ChartExtension.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Symfony\UX\Chartjs\Twig; 13 | 14 | use Symfony\UX\Chartjs\Model\Chart; 15 | use Symfony\UX\StimulusBundle\Helper\StimulusHelper; 16 | use Symfony\WebpackEncoreBundle\Twig\StimulusTwigExtension; 17 | use Twig\Extension\AbstractExtension; 18 | use Twig\TwigFunction; 19 | 20 | /** 21 | * @author Titouan Galopin 22 | * 23 | * @final 24 | */ 25 | class ChartExtension extends AbstractExtension 26 | { 27 | private $stimulus; 28 | 29 | /** 30 | * @param $stimulus StimulusHelper 31 | */ 32 | public function __construct(StimulusHelper|StimulusTwigExtension $stimulus) 33 | { 34 | if ($stimulus instanceof StimulusTwigExtension) { 35 | trigger_deprecation('symfony/ux-chartjs', '2.9', 'Passing an instance of "%s" to "%s" is deprecated, pass an instance of "%s" instead.', StimulusTwigExtension::class, __CLASS__, StimulusHelper::class); 36 | $stimulus = new StimulusHelper(null); 37 | } 38 | 39 | $this->stimulus = $stimulus; 40 | } 41 | 42 | public function getFunctions(): array 43 | { 44 | return [ 45 | new TwigFunction('render_chart', [$this, 'renderChart'], ['is_safe' => ['html']]), 46 | ]; 47 | } 48 | 49 | public function renderChart(Chart $chart, array $attributes = []): string 50 | { 51 | $chart->setAttributes(array_merge($chart->getAttributes(), $attributes)); 52 | 53 | $controllers = []; 54 | if ($chart->getDataController()) { 55 | $controllers[$chart->getDataController()] = []; 56 | } 57 | $controllers['@symfony/ux-chartjs/chart'] = ['view' => $chart->createView()]; 58 | 59 | $stimulusAttributes = $this->stimulus->createStimulusAttributes(); 60 | foreach ($controllers as $name => $controllerValues) { 61 | $stimulusAttributes->addController($name, $controllerValues); 62 | } 63 | 64 | foreach ($chart->getAttributes() as $name => $value) { 65 | if ('data-controller' === $name) { 66 | continue; 67 | } 68 | 69 | if (true === $value) { 70 | $stimulusAttributes->addAttribute($name, $name); 71 | } elseif (false !== $value) { 72 | $stimulusAttributes->addAttribute($name, $value); 73 | } 74 | } 75 | 76 | return \sprintf('', $stimulusAttributes); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/DependencyInjection/ChartjsExtension.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Symfony\UX\Chartjs\DependencyInjection; 13 | 14 | use Symfony\Component\AssetMapper\AssetMapperInterface; 15 | use Symfony\Component\DependencyInjection\ContainerBuilder; 16 | use Symfony\Component\DependencyInjection\Definition; 17 | use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; 18 | use Symfony\Component\DependencyInjection\Reference; 19 | use Symfony\Component\HttpKernel\DependencyInjection\Extension; 20 | use Symfony\UX\Chartjs\Builder\ChartBuilder; 21 | use Symfony\UX\Chartjs\Builder\ChartBuilderInterface; 22 | use Symfony\UX\Chartjs\Twig\ChartExtension; 23 | 24 | /** 25 | * @author Titouan Galopin 26 | * 27 | * @internal 28 | */ 29 | class ChartjsExtension extends Extension implements PrependExtensionInterface 30 | { 31 | public function load(array $configs, ContainerBuilder $container): void 32 | { 33 | $container 34 | ->setDefinition('chartjs.builder', new Definition(ChartBuilder::class)) 35 | ->setPublic(false) 36 | ; 37 | 38 | $container 39 | ->setAlias(ChartBuilderInterface::class, 'chartjs.builder') 40 | ->setPublic(false) 41 | ; 42 | 43 | $container 44 | ->setDefinition('chartjs.twig_extension', new Definition(ChartExtension::class)) 45 | ->addArgument(new Reference('stimulus.helper')) 46 | ->addTag('twig.extension') 47 | ->setPublic(false) 48 | ; 49 | } 50 | 51 | public function prepend(ContainerBuilder $container): void 52 | { 53 | if (!$this->isAssetMapperAvailable($container)) { 54 | return; 55 | } 56 | 57 | $container->prependExtensionConfig('framework', [ 58 | 'asset_mapper' => [ 59 | 'paths' => [ 60 | __DIR__.'/../../assets/dist' => '@symfony/ux-chartjs', 61 | ], 62 | ], 63 | ]); 64 | } 65 | 66 | private function isAssetMapperAvailable(ContainerBuilder $container): bool 67 | { 68 | if (!interface_exists(AssetMapperInterface::class)) { 69 | return false; 70 | } 71 | 72 | // check that FrameworkBundle 6.3 or higher is installed 73 | $bundlesMetadata = $container->getParameter('kernel.bundles_metadata'); 74 | if (!isset($bundlesMetadata['FrameworkBundle'])) { 75 | return false; 76 | } 77 | 78 | return is_file($bundlesMetadata['FrameworkBundle']['path'].'/Resources/config/asset_mapper.php'); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/Model/Chart.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Symfony\UX\Chartjs\Model; 13 | 14 | /** 15 | * @author Titouan Galopin 16 | * 17 | * @final 18 | */ 19 | class Chart 20 | { 21 | public const TYPE_LINE = 'line'; 22 | public const TYPE_BAR = 'bar'; 23 | public const TYPE_RADAR = 'radar'; 24 | public const TYPE_PIE = 'pie'; 25 | public const TYPE_DOUGHNUT = 'doughnut'; 26 | public const TYPE_POLAR_AREA = 'polarArea'; 27 | public const TYPE_BUBBLE = 'bubble'; 28 | public const TYPE_SCATTER = 'scatter'; 29 | 30 | private $type; 31 | private $data = []; 32 | private $options = []; 33 | private $attributes = []; 34 | 35 | public function __construct(string $type) 36 | { 37 | $this->type = $type; 38 | } 39 | 40 | /** 41 | * @return $this 42 | */ 43 | public function setData(array $data): self 44 | { 45 | $this->data = $data; 46 | 47 | return $this; 48 | } 49 | 50 | /** 51 | * Sets Chart.js options. 52 | * 53 | * @see https://www.chartjs.org/docs/2.9.4 54 | * 55 | * 56 | * $chart->setOptions([ 57 | * 'scales' => [ 58 | * 'yAxes' => [ 59 | * ['ticks' => ['min' => 0, 'max' => 100]], 60 | * ] 61 | * ] 62 | * ]); 63 | * 64 | * 65 | * @return $this 66 | */ 67 | public function setOptions(array $options): self 68 | { 69 | $this->options = $options; 70 | 71 | return $this; 72 | } 73 | 74 | /** 75 | * @return $this 76 | */ 77 | public function setAttributes(array $attributes): self 78 | { 79 | $this->attributes = $attributes; 80 | 81 | return $this; 82 | } 83 | 84 | public function createView(): array 85 | { 86 | return [ 87 | 'type' => $this->type, 88 | 'data' => $this->data, 89 | 'options' => $this->options, 90 | ]; 91 | } 92 | 93 | public function getDataController(): ?string 94 | { 95 | return $this->attributes['data-controller'] ?? null; 96 | } 97 | 98 | public function getAttributes(): array 99 | { 100 | return $this->attributes; 101 | } 102 | 103 | public function getType(): string 104 | { 105 | return $this->type; 106 | } 107 | 108 | public function getData(): array 109 | { 110 | return $this->data; 111 | } 112 | 113 | public function getOptions(): array 114 | { 115 | return $this->options; 116 | } 117 | } 118 | --------------------------------------------------------------------------------