0

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.

Rorabih
  • 73
  • 6
  • I'm not familiar with the library, but could it have something to do with the fact that the number of values you supply as data (7) doesn't match the number of labels you supply (6)? Seems like every example from Larapex docs shows a matching number of both. – El_Vanja Mar 23 '21 at 16:38
  • @El_Vanja that was my initial concern but after adding the same number of values to x-axis it unfortunately did not resolve the issue. – Rorabih Mar 23 '21 at 16:46
  • Could it then be that you place your data array into another array? If `$userLogins` is already an array, maybe you don't need to wrap it in an array when supplying the values: `->addData('UserLogins', [$userLogins])`. None of the examples use a multidimensional array. – El_Vanja Mar 23 '21 at 16:50
  • @El_Vanja I tried to remove the brackets by doing this: $skips = ["[", "]"]; $formatted = str_replace($skips, ' ', $userLogins); and therefore giving me this output: 1,1,3,1,1,1,1 But that did not help either. But if I take the values and place them manually inside the array it shows the data correctly in the chart. – Rorabih Mar 23 '21 at 17:00
  • I'm confused now. `$userLogins` should be an array (that is what `pluck` returns). Did you turn it into a string at some point? – El_Vanja Mar 23 '21 at 17:03
  • @El_Vanja I format the array to remove the brackets with the following snippet $skips = ["[", "]"]; $formatted = str_replace($skips, ' ', $userLogins); I did try to add data like so: ->addData('UserLogins', $userLogins) but I got an error that the expected type should be an array. – Rorabih Mar 23 '21 at 17:11
  • When you dump `$userLogins` (right after retrieving it from the database, before you try to do any formatting), what do you get? It should be an array. – El_Vanja Mar 23 '21 at 17:14
  • @El_Vanja It is an array - Illuminate\Support\Collection {#1408 ▼ #items: array:7 [▼ 0 => 1 1 => 1 2 => 3 3 => 1 4 => 1 5 => 1 6 => 1 ] } – Rorabih Mar 23 '21 at 17:16
  • 1
    It's a Collection object, actually. So directly supplying `->addData('UserLogins', $userLogins->all())` should work (note the added call to `all` to retrieve the array stored under `items` in the collection). – El_Vanja Mar 23 '21 at 17:18
  • @El_Vanja thank you so much. That solved my issue. – Rorabih Mar 23 '21 at 17:25
  • @El_Vanja can you maybe help me with a refactor of the $userLogins statement? – Rorabih Mar 23 '21 at 17:40
  • You mean something along [these lines](https://stackoverflow.com/questions/18533080/laravel-eloquent-groupby-and-also-return-count-of-each-group)? – El_Vanja Mar 23 '21 at 19:40
  • @El_Vanja Not quite. I want to output a chart showing user logins this year grouped by months. – Rorabih Mar 24 '21 at 08:51

1 Answers1

2

The issue is that you are supplying values that the charts cannot understand. The result of pluck is a Collection object and you have placed that object in an array, but it should be an array of numeric data. To extract just the count, you need to change this:

->addData('UserLogins', [$userLogins])

to this:

->addData('UserLogins', $userLogins->all())

Description of the all method from the docs:

array all()

Get all of the items in the collection

So this will extract your actual array of counts ([1,1,3,1,1,1,1]) from the Collection object.

El_Vanja
  • 3,660
  • 4
  • 18
  • 21