nova-chartjs icon indicating copy to clipboard operation
nova-chartjs copied to clipboard

The chart is not rendered correctly when fetching data from DB for the 'data' array

Open kylescousin opened this issue 2 years ago • 0 comments

I have this table which logs a daily record for the amount of holders for a certain crypto. It's simply "DAY" => "HOLDERS" - and I want to plot this out in a chart. The problem is that there seems to be an issue with the chart rendering before the data is being pulled from the database.

When logging the $holders that are taken from the database and $holders that is hardcoded, it shows the exact same array. Yet, when providing it to the 'data' field in the Linechart, it only works when providing the hardcoded declared array. Why is this, and how can this be fixed?

        $holders = \App\Models\Holder::where('crypto_id', $request->get('viaResourceId'))
            ->select('day', 'active_addresses')
            ->get();

        //Fetching the values from DB doesn't render the chart correctly.
        $days = $holders->pluck('day')->toArray();
        $holders = $holders->pluck('active_addresses')->toArray();

        //Hardcoding the values renders the chart correctly.
        $daysStatic = [1,2,3];
        $holdersStatic = [963625,963625,96325];

        app('debugbar')->info($holders);
        app('debugbar')->info($holdersStatic);
  (new LineChart())
                ->title('Holders')
                ->series(array([

                    'label' => 'Holders by day',
                    'borderColor' => '#f7a35c',
                    'data' => $holdersStatic
                ]))
                ->options([
                    'xaxis' => [
                        'categories' => $daysStatic // <== If you put $days here instead of $daysStatic - it doesn't render the chart.
                    ],
                ])
                ->onlyOnDetail()->width('2/3'),

kylescousin avatar May 01 '22 08:05 kylescousin