Using 2 different data to show in chart laravel

一笑奈何 提交于 2019-12-24 01:05:03

问题


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)


来源:https://stackoverflow.com/questions/47690319/using-2-different-data-to-show-in-chart-laravel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!