fxcosta / laravel-chartjs

Simple package to facilitate and automate the use of charts in Laravel 5.x using Chartjs v2 library
486 stars 112 forks source link

Number of datasets are limited in a Chart due to colours[] array. #5

Closed AshishGupta001 closed 7 years ago

AshishGupta001 commented 8 years ago

In current impl, number of datasets in any chart is limited to just 3. This seems is due to the colours[] array defined in config/chart.js.

I have workedaround this issue by generating the colors for each dataset at run time. Thought this could be useful to other users of laravel-chartjs. Feel free to use the code below. Note: I have not done extensive testing but it seems to work for me.

/resources/ChartBar.php (within method 'render')

        for($i = 0; $i < $datasetQnt; $i++) {
            // Pick a colour for a dataset at run time.
            $spread = 25;
            $r = rand(0+$spread,255-$spread);
            $g = rand(0+$spread,255-$spread);
            $b = rand(0+$spread,255-$spread);
            $color = "rgba($r, $g, $b, .7)";

            $dataset[$i] = array_column($data, $i);
            $dataset[$i] = implode(", ", $dataset[$i]);
            $colours[$i] = $color; //$this->colours[$i];
        }
fxcosta commented 8 years ago

The idea is to have as many datasets want to have and just setting the colors in chartjs.php. In fact, if I have 5 datasets and have only added three colors, the last two will have a problem. Your code is fine, the only problem is that little is configurable for other users. This can be a solution only if the number of colors is less than the number of datasets.

You already contribiu with any open source project? You can send me a Pull Request and I do merge their code. It would be the same, just implementing the random color if there is no defined color.

Send a Pull Request will be better because it will keep your name on the script and will serve as an incentive for contributions in other projects. Leave their signatures on their solutions!

Thanks for feedback!

AshishGupta001 commented 8 years ago

Thanks Felix. That's very thoughtful of you. Would LOVE to collaborate with you and contribute in the laravel-chartjs project.

Right now I am tying some loose ends for the app I was working on (had to deploy for demo purpose). Once done I will send a pull request and maybe enhance the current implementation.

BTW I opened one other issue #4 which seems you are currently developing. Wondering whether you can share some timeline when it will be available.

Thanks Ashish

On Thu, Feb 11, 2016 at 9:17 AM, Felix Costa notifications@github.com wrote:

The idea is to have as many datasets want to have and just setting the colors in chartjs.php. In fact, if I have 5 datasets and have only added three colors, the last two will have a problem. Your code is fine, the only problem is that little is configurable for other users. This can be a solution only if the number of colors is less than the number of datasets.

You already contribiu with any open source project? You can send me a Pull Request and I do merge their code. It would be the same, just implementing the random color if there is no defined color.

Send a Pull Request will be better because it will keep your name on the script and will serve as an incentive for contributions in other projects. Leave their signatures on their solutions!

Thanks for feedback!

— Reply to this email directly or view it on GitHub https://github.com/fxcosta/laravel-chartjs/issues/5#issuecomment-182690867 .

fxcosta commented 7 years ago

Wow, finally new version is coming! :D

Barmunksu commented 4 years ago

Hello! i found $this->colours = config('chartjs.colours') in ChartjsServiceProvider How can i use auto color?