Laravel 5.5 Console TV条形图高位图多个数据集和标签

时间:2018-08-11 09:42:45

标签: php laravel laravel-5 charts highcharts

我有一个使用此ConsoleTV Chart Package Laravel 5.5 应用程序,我使用了 barchart 中可视化我的数据 >每月报告。我在下面有以下查询。

$dt = Carbon::now()->year;
        $anRequest = AnalysisRequest::where(DB::raw("(DATE_FORMAT(created_at,'%Y'))"),date('Y'))->orderBy('service_id') 
                        ->get();

和在我的图表

$barChart = Charts::database($anRequest, 'bar', 'highcharts')
                  ->title("Monthly service request")
                  ->elementLabel("Total requests")
                  ->dimensions(1000, 500)
                  ->responsive(false)
                  ->groupByMonth(date('Y'), true);

此代码运行良好,可以显示的所有数据,但是我如何添加另一个dataSetsmultipleDataSets而不是仅添加一个设置数据可视化

例如,我会从 AnalysisRequest 中获得另一个查询,但要查询特定的资源

使用 multi

下面的代码示例

查询

 $tags_jan = DB::table('tags')->whereYear('created_at', $dt)->whereMonth('created_at', '1')->count();

         $tags_feb = DB::table('tags')->whereYear('created_at', $dt)->whereMonth('created_at', '2')->count();

         $tags_mar = DB::table('tags')->whereYear('created_at', $dt)->whereMonth('created_at', '3')->count();
// and the list goes on which is bad

图表使用 multi

$chart = Charts::multi('bar', 'highcharts')
            // Setup the chart settings
            ->title("Total Reports for this year")
            // A dimension of 0 means it will take 100% of the space
            ->dimensions(0, 400) // Width x Height
            // This defines a preset of colors already done:)
            // ->template("material")
            // ->responsive(true)
            // You could always set them manually
            // ->colors(['#2196F3', '#F44336', '#FFC107'])
            // Setup the diferent datasets (this is a multi chart)
            ->colors(['green', 'aqua', 'red', 'yellow'])
            ->dataset('Ads', [$ads_jan,$ads_feb,$ads_mar,$ads_apr,$ads_may,$ads_june,$ads_july,$ads_aug,$ads_sept,$ads_oct,$ads_nov,$ads_dec])

            ->dataset('Channels', [$channels_jan,$channels_feb,$channels_mar,$channels_apr,$channels_may,$channels_june,$channels_july,$channels_aug,$channels_sept,$channels_oct,$channels_nov,$channels_dec])

            ->dataset('Shows', [$shows_jan,$shows_feb,$shows_mar,$shows_apr,$shows_may,$shows_june,$shows_july,$shows_aug,$shows_sept,$shows_oct,$shows_nov,$shows_dec])

            ->dataset('Tags', [$tags_jan,$tags_feb,$tags_mar,$tags_apr,$tags_may,$tags_june,$tags_july,$tags_aug,$tags_sept,$tags_oct,$tags_nov,$tags_dec])

上面的代码也可以正常工作,但是正如您在变量中所看到的那样,我正在查询今年的每个月,这并不是很好。如何简化此查询或使用多种数据集解决该问题的另一种方法?

感谢有人可以帮助您。 预先感谢。

1 个答案:

答案 0 :(得分:0)

查询数据库后,您想将不同的图形发布到同一视图中,因此可以添加以下代码,我正在使用该代码在以下指定的同一图形上绘制条形图和折线图... .....查看数据集位置2的线条和条形

$chart = new unclaimed;
$chart->labels( $days );
$chart->dataset('Daily Visitors Bar', 'bar', $totalNumber )->color('white')->backgroundColor(['#009900','#8a8a5c','#f1c40f','#e67e22','#16a085','#2980b9']);

$chart->dataset('Daily Visitors Line', 'line', $totalNumber )->color('black') ->fill(false)->backgroundColor(['##8a8a5c','009900','#f1c40f','#e67e22','#16a085','#2980b9']);