Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mySQL与PHP到JSON的客户端图表_Php_Mysql_Laravel_Highcharts - Fatal编程技术网

mySQL与PHP到JSON的客户端图表

mySQL与PHP到JSON的客户端图表,php,mysql,laravel,highcharts,Php,Mysql,Laravel,Highcharts,我希望有一个更好的方法来做我正在做的事情。我有几张图表要做,我想用最好的方式去做 我使用的是HighCharts,它需要一个JSON对象作为数据。我正在使用Laravel作为我的后端。我正在向我的视图发送2个变量(1个用于标签,1个用于值)。我知道我可以在控制器中执行此操作,但尚未启动 我的问题: 我有这张桌子 | date | printer | quant | | 2017-01-23 | JP1 | 20 | | 2017-01-23 | JP1

我希望有一个更好的方法来做我正在做的事情。我有几张图表要做,我想用最好的方式去做

我使用的是HighCharts,它需要一个JSON对象作为数据。我正在使用Laravel作为我的后端。我正在向我的视图发送2个变量(1个用于标签,1个用于值)。我知道我可以在控制器中执行此操作,但尚未启动

我的问题:

我有这张桌子

|     date    | printer  | quant  |
| 2017-01-23  | JP1      |  20    |
| 2017-01-23  | JP1      |  12    |
| 2017-01-23  | JP2      |  18    |
| 2017-01-24  | JP5      |  16    |
| 2017-01-24  | JP1      |   8    |
| 2017-01-26  | JP2      |   12   |
我认为,点击所有的案件,可变打印机,错过的天数,需要每天的数量总和,并使0如果失踪。每台打印机都需要一个阵列,我没有预先制作的列表,因为它们变化很大。我的查询将包含一个日期范围。打印机列表对于该日期范围是不同的

我所做的是做一个pivot-mySQL查询,它让我明白了这一点

[
{
"date": "2017-01-23",
"sum": "1416",
"JP4_3": "375",
"JP4_1": "533",
"JP4_2": "508",
"B3": null,
"A2": null
},
{
"date": "2017-01-24",
"sum": "2151",
"JP4_3": "847",
"JP4_1": "499",
"JP4_2": "805",
"B3": null,
"A2": null
},
{
"date": "2017-01-25",
"sum": "2097",
"JP4_3": "284",
"JP4_1": "917",
"JP4_2": "896",
"B3": null,
"A2": null
}
]
我可以循环并获取每个属性以生成一个数组

我想知道有没有更好的办法。我需要一个标签和数据来处理高图表。这里有一个例子

    xAxis: {
        categories: ['Apples', 'Bananas', 'Oranges']
    },
    series: [{
        name: 'Jane',
        data: [1, 0, 4]
    }, {
        name: 'John',
        data: [5, 7, 3]
    }]

我知道这是一个复杂而冗长的问题。感谢您的帮助和指导。

想给出一个代码示例,说明我是如何做到这一点的。有点痛,但我想这是正常的。这会进行几个数据库调用,但对我来说仍然非常快。这是使用laravel框架

    // Main chart info
    $PrinterChartArray ["chart"] = array (
            'type' => 'line' 
    );
    $PrinterChartArray ["title"] = array (
            "text" => "Units per Printer" 
    );
    $PrinterChartArray ["credits"] = array (
            "enabled" => false 
    );
    $PrinterChartArray ["xAxis"] = array (
            "categories" => array () 
    );
    $PrinterChartArray ["tooltip"] = array (
            "valueSuffix" => " units" 
    );
    $PrinterChartArray ["yAxis"] = array (
            "title" => array (
                    "text" => "Units" 
            ) 
    );

    // Get series Names
    $printerNames = PrintJob::distinct()->select('printer')
        ->whereDate('job_date', '>=', date($from))
        ->whereDate('job_date', '<=', date($to))
        ->get();

    // loop through series names and set series name and data for high charts
    $chartSeries = [];
    foreach($printerNames as $printer)
    {
        $printName = $printer->printer;

        // Used where inside join so wouldnt miss dates
        // used a calendar table with all dates to fill in missing dates
        $data = PrintJob::select(DB::raw('COALESCE(SUM(print_jobs.quantity), 0) as sum'), DB::raw('calendar.datefield'))
        ->rightJoin('calendar', function($join) use ($printName)
        {
            $join->on('print_jobs.job_date', '=', 'calendar.datefield')
                ->where('printer', '=', $printName);
        })
        ->whereDate('calendar.datefield', '>=', date($from))
        ->whereDate('calendar.datefield', '<=', date($to))         
        ->groupBy('calendar.datefield')
        ->orderBy('calendar.datefield', 'ASC')
        //->toSql();
        ->get();
        //->pluck('sum');

        // Needed to convert the returned value from a string value to a int value (apparently a driver issue with php and mysql)
        foreach ($data as $key => $var) {
            $data[$key] = (int)$var->sum;
        }
        // Add series to high chart
        $chartSeries[] = array(
            "name" => $printName,
            "data" => $data
        );
    }

    // Unessesary to use PrintJob for this but keeping similar to series to not mess up label
    $chartLabel = PrintJob::select(DB::raw('calendar.datefield'))
        ->rightJoin('calendar', function($join) use ($printName)
        {
            $join->on('print_jobs.job_date', '=', 'calendar.datefield');
        })
        ->whereDate('calendar.datefield', '>=', date($from))
        ->whereDate('calendar.datefield', '<=', date($to))         
        ->groupBy('calendar.datefield')
        ->orderBy('calendar.datefield', 'ASC')
        //->toSql(); --- This will show the actual query
        //->get(); --- Will give array of objects
        ->pluck('calendar.datefield'); // --- Will give a array of just the values wanted

    $PrinterChartArray ["series"] = $chartSeries;
    $PrinterChartArray ["xAxis"] = array (
            "categories" => $chartLabel 
    );
    //return $PrinterChartArray; -- used to see output without view

  return view('print.charts', compact('jobsByDayLabels', 'jobsByDayValues', 'unitsByDayLabels', 'unitsByDayValues', 'PrinterChartArray'));

仅从laravel返回数据数组并在视图中使用

var data = JSON.parse( '{!! json_encode($chartData) !!}' );

我在我的代码中使用了这个

可能没有比循环和创建自己的数组更好的格式化数据的方法,因为highcharts需要这样一种特定的格式。我是在将highcharts集成到几个不同的PHP/MySQL项目中之后说这番话的,有几个项目是Laravel。通常,我会循环并缓存图表数据(在内存或表中),以保存频繁处理的大型数据集。Hi@Samsquanch。你能举个例子吗。简单的事情。我来自c#,很久没有接触过php了。这里有几个我认为很有用的功能,我不知道该去寻找。或者如果你知道这个senario的好资源。我找到了一个,但他们没有显示数据库,所以它不是很清楚。
$(function() {
  $('#printerUnitsByDay').highcharts({!! json_encode($PrinterChartArray) !!})
});
var data = JSON.parse( '{!! json_encode($chartData) !!}' );