PHP code example of kematjaya / chart-bundle

1. Go to this page and download the library: Download kematjaya/chart-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kematjaya / chart-bundle example snippets


    

    namespace App\Chart;

    use App\Entity\Example;
    use Doctrine\ORM\QueryBuilder;
    use Kematjaya\ChartBundle\Chart\AbstractChart;
    
    class ExampleChart extends AbstractChart
    {

        public function getTitle():string
        {
            return $this->translator->trans('examlple_chart');
        }

        public function getQueryBuilder(string $alias = 't', array $params = []): QueryBuilder
        {
            return $this->getEntityManager()->getRepository(Example::class)->createQueryBuilder($alias);
        }

        // category chart
        public function getCategories():array
        {
            return [
              'Jan', 'Feb', 'Mar', 'Apr'
            ];
        }

        public function getSeries(QueryBuilder $qb):array
        {
            $results = [];
            $rs = $qb->getQuery()->getResult();
            foreach (rs as $status) {
                $row = [
                    "name" => $status,
                    "data" => []
                ];

                foreach ($this->getCategories() as $label) {
                    $row['data'][] = rand();
                }

                $results[] = $row;
            }

            return $results;
        }

        public function getChartTitle():string
        {
            return sprintf("%s %s", $this->translator->trans('total'), $this->translator->trans('example'));
        }

        // if using table data 
        public function getDatas(QueryBuilder $qb): array 
        {
            $results = [];
            $rs = $qb->getQuery()->getResult();
            foreach ($this->getCategories() as $label) {

                $count = '<ul>';
                foreach ($rs as $status) {
                    $count .= sprintf("<li>%s: %s</li>", $this->translator->trans($status), rand());
                } 
                $count .= '<ul>';
                $results[] = [$this->translator->trans($label), $count];
            }

            return $results;
        }

        public function getHeaders(): array 
        {
            return [
               $this->translator->trans('month'), $this->translator->trans('total')
            ];
        }

        public function getSequence(): int 
        {
            return 1;
        }

    }