使用laravel创建交互式图表

时间:2017-06-14 20:41:18

标签: laravel laravel-5 charts

请帮帮我,我需要将此sql行传递给laravel syxtax才能使用 在可定制的图表上 我正在使用 https://github.com/ConsoleTVs/Charts Sql语法:

    SELECT 
 SUM(CASE WHEN status like 'Por Despachar' THEN 1 ELSE 0 END) AS Por_Despachar
,SUM(CASE WHEN status like 'Planificado' THEN 1 ELSE 0 END) AS Planificado
,SUM(CASE WHEN status like 'Despachado' THEN 1 ELSE 0 END) AS Despachado
    FROM presupuesto

我想在这样的交互式图表上添加此参数 enter image description here

这是我的控制器:



<?php

namespace sisVentas\Http\Controllers;

use Illuminate\Http\Request;
use sisVentas\User;
use sisVentas\Http\Requests;
use Charts;
class EstadisticaController extends Controller
{
    //

public function index()
    {
    $data = \DB::select("
            SELECT 
                SUM(CASE WHEN status like 'Por Despachar' THEN 1 ELSE 0 END) AS Por_Despachar
                ,SUM(CASE WHEN status like 'Planificado' THEN 1 ELSE 0 END) AS Planificado
                ,SUM(CASE WHEN status like 'Despachado' THEN 1 ELSE 0 END) AS Despachado
            FROM presupuesto
        ");





        $chart =  Charts::create('pie', 'highcharts')
            // Setup the chart settings
            ->title("Resumen de Presupuestos Realizados")
            // A dimension of 0 means it will take 100% of the space
            // This defines a preset of colors already done:)

            // You could always set them manually

            // Setup the diferent datasets (this is a multi chart)
  ->labels(['Por Despachar', 'Despachado', 'Planificado'])
  ->values([65,10,20])
  ->dimensions(1000,500)
  ->responsive(false);
        return dd($chart, $data);

    }

}
&#13;
&#13;
&#13;

&#13;
&#13;
Chart {#324 ▼
  +id: null
  +customId: null
  +type: "pie"
  +library: "highcharts"
  +title: "Resumen de Presupuestos Realizados"
  +element_label: "Element"
  +labels: array:3 [▼
    0 => "Por Despachar"
    1 => "Despachado"
    2 => "Planificado"
  ]
  +values: array:3 [▼
    0 => 65
    1 => 10
    2 => 20
  ]
  +colors: []
  +responsive: false
  +gauge_style: "left"
  +view: null
  +region: "world"
  #suffix: ""
  +container: ""
  +credits: false
  +loader: true
  +loader_duration: 500
  +loader_color: "#000000"
  +background_color: "inherit"
  +template: "material"
  +one_color: false
  +legend: true
  +x_axis_title: false
  +y_axis_title: null
  +"height": 500
  +"width": 1000
}

array:1 [▼
  0 => {#330 ▼
    +"por_despachar": "3"
    +"planificado": "1"
    +"despachado": "0"
  }
]
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

也许不是直接回答但应该有所帮助

当您有任何难以/无法转换为雄辩的SQL查询时,只需使用数据库外观。

$data = \DB::select("
            SELECT 
                SUM(CASE WHEN status like 'Por Despachar' THEN 1 ELSE 0 END) AS Por_Despachar
                ,SUM(CASE WHEN status like 'Planificado' THEN 1 ELSE 0 END) AS Planificado
                ,SUM(CASE WHEN status like 'Despachado' THEN 1 ELSE 0 END) AS Despachado
            FROM presupuesto
        ");

dd($data);

答案 1 :(得分:0)

$chart = Charts::database($data, 'pie', 'highcharts')              
        ->title('Amount subscribers')        
        ->labels([Por_Despachar, Planificado, Despachado])                            
        ->values([$data[0]->Por_Despachar, $data[1]->Planificado, $data[2]->Despachado])
        ->dimensions(1000,500)
        ->responsive(true);