Using 2 different data to show in chart laravel

2019-07-20 10:27发布

问题:

I am currently using chartTV to create a chart but my chart doesn't seem to appear inside my blade for some reason, the title can be seen but not the graph. I want to create an areaspline chart where it will show number of people hired and not hired this week. I tried doing something like this but it not working. Can someone guide me in doing it correctly? How should I do it?

My dashboardController:

$user1s = DB::table('evaluations')
             ->select('evaluations.Evaluation_Status','evaluations.created_at' ) 
             ->where('Evaluation_Status', '=', 'No') 
             ->get();
$user2s = DB::table('evaluations')
             ->select('evaluations.Evaluation_Status','evaluations.created_at' ) 
             ->where('evaluations.Evaluation_Status', '=', 'Yes') 
             ->get();

Charts::Database($user1s, $user2s, 'areaspline', 'highcharts')
       ->title('My nice chart')
       ->labels(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday','Saturday', 'Sunday'])
       ->dataset('Hired', [$user1s])
       ->dataset('Not Hired', [$user2s]);

return view('dashboard', ['chart' => $chart]);

My dashboard.blade.php

{!! Charts::assets() !!}
{!! $chart->render() !!}

回答1:

Multi database charts can be used if using data from different database tables. The APIs are similar to Charts::multi. The only differentiation is how datasets are specified.

$charts = Charts::multiDatabase('areaspline', 'highcharts')
            ->title('My nice chart')
            ->colors(['#ff0000', '#ffffff'])
            ->labels(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday','Saturday', 'Sunday'])
            ->dataset('Hired', $user1s)
            ->dataset('Not Hired', $user2s);


回答2:

I assume the problem are these 2 lines:

->dataset('Hired', [$user1s])
->dataset('Not Hired',  [$user2s]);

Both $user1s and $user2s are already arrays. Since both array contain objects you also might have to map them to scalar values (numbers or strings) to get the values you want to use.

So you could do something like this, if you want to map the names:

$hiredUsernames = array_map(
    function ($user) {
        return $user->name;
    },
    $user1s
);
// The result will be something like: ['Max', 'Jane', ...]

->dataset('Hired', $hiredUsernames)