John Davidson

php - Variable with counts are not working with larapex

0 comments
Message:


I am using Larapex charts for my laravel project but I cannot make the chart render when using a variable that contains my values retrieved from the database.


        $userLogins = User::select(DB::raw("COUNT(*) as count"))
->whereYear('last_login_at', date('Y'))
->groupBy(\DB::raw("Month(last_login_at)"))
->pluck('count');

The variable above retrives following values:


[1,1,3,1,1,1,1]

Which I then try to place within the chart


        $chart = LarapexChart::areaChart()
->setTitle('Sales during 2021.')
->setSubtitle('UserLogins')
->addData('UserLogins', [$userLogins])
->setXAxis(['January', 'February', 'March', 'April', 'May', 'June']);

But unfortunately the chart does not render the values.


Can anyone help me out?


Best regards.



My Answer:
It looks like the issue might be with how you are passing the values to the chart. When you use [$userLogins], you are actually passing an array within an array to the chart, which might be causing the issue.

Instead, try passing $userLogins directly to the addData method without wrapping it in an array:

php
$chart = LarapexChart::areaChart()
->setTitle('Sales during 2021.')
->setSubtitle('UserLogins')
->addData('UserLogins', $userLogins)
->setXAxis(['January', 'February', 'March', 'April', 'May', 'June']);


By doing this, you are passing the actual array of values to the chart, which should render the chart correctly with the values you retrieved from the database.

Give this a try and see if it resolves the issue. Let me know if you need any further assistance.

Rate this post

4 of 5 based on 1288 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us