Laravel将错误的数据返回到图表

时间:2016-11-24 18:34:53

标签: javascript php laravel charts chart.js

我在页面上有2个图表,我用ajax将数据输入到图表中,并返回错误的数据(两个图表都有相同的数据)。

我的代码:

function drawCompaniesChart() {
    var jsonData = $.ajax({
        url: "{{action('Administration\Dashboard',['chart'=>'companies'])}}",
        dataType: 'json',

        }).done(function (results) {
            var data=results['data'];

        var tempData = {
            labels: [data['label'][6],data['label'][5],data['label'][4],data['label'][3],data['label'][2],data['label'][1],data['label'][0],],
            datasets: [
                {
                    label: "Free",
                    fillColor: "rgba(210, 214, 222, 1)",
                    strokeColor: "rgba(210, 214, 222, 1)",
                    pointColor: "rgba(210, 214, 222, 1)",
                    pointStrokeColor: "#c1c7d1",
                    pointHighlightFill: "#fff",
                    pointHighlightStroke: "rgba(220,220,220,1)",
                    data: [data[data['label'][6]]['1'],data[data['label'][5]]['1'],data[data['label'][4]]['1'],data[data['label'][3]]['1'],data[data['label'][2]]['1'],data[data['label'][1]]['1'],data[data['label'][0]]['1']]

                },
                {
                    label: "Paid 1",
                    fillColor: "rgba(60,141,188,0.9)",
                    strokeColor: "rgba(60,141,188,0.8)",
                    pointColor: "#3b8bba",
                    pointStrokeColor: "rgba(60,141,188,1)",
                    pointHighlightFill: "#fff",
                    pointHighlightStroke: "rgba(60,141,188,1)",
                    data: [data[data['label'][6]]['2'],data[data['label'][5]]['2'],data[data['label'][4]]['2'],data[data['label'][3]]['2'],data[data['label'][2]]['2'],data[data['label'][1]]['2'],data[data['label'][0]]['2']]

                },
                {
                    label: "Paid 2",
                    fillColor: "rgba(60,111,250,0.9)",
                    strokeColor: "rgba(60,111,250,0.8)",
                    pointColor: "#3b8bba",
                    pointStrokeColor: "rgba(60,111,250,1)",
                    pointHighlightFill: "#fff",
                    pointHighlightStroke: "rgba(60,111,250,1)",
                    data: [data[data['label'][6]]['3'],data[data['label'][5]]['3'],data[data['label'][4]]['3'],data[data['label'][3]]['3'],data[data['label'][2]]['3'],data[data['label'][1]]['3'],data[data['label'][0]]['3']]

                }
            ]
        };


        var ctx = document.getElementById("registered-companies-chart").getContext("2d");

        // Instantiate a new chart
        var myLineChart = new Chart(ctx).Line(tempData);



    });}

 function drawOrdersChart() {
         console.log('megyen');
    var jsonData = $.ajax({
        url: "{{action('Administration\Dashboard',['chart'=>'orders'])}}",
        dataType: 'json',

        }).done(function (results) {
            var data=results['data'];

            console.log(data);
        var tempData = {
            labels: [data['label'][6],data['label'][5],data['label'][4],data['label'][3],data['label'][2],data['label'][1],data['label'][0],],
            datasets: [
                {
                    label: "Cancelled",
                    fillColor: "rgba(210, 214, 222, 1)",
                    strokeColor: "rgba(210, 214, 222, 1)",
                    pointColor: "rgba(210, 214, 222, 1)",
                    pointStrokeColor: "#c1c7d1",
                    pointHighlightFill: "#fff",
                    pointHighlightStroke: "rgba(220,220,220,1)",
                    data: [data[data['label'][6]]['1'],data[data['label'][5]]['1'],data[data['label'][4]]['1'],data[data['label'][3]]['1'],data[data['label'][2]]['1'],data[data['label'][1]]['1'],data[data['label'][0]]['1']]

                },
                {
                    label: "Finished",
                    fillColor: "rgba(60,141,188,0.9)",
                    strokeColor: "rgba(60,141,188,0.8)",
                    pointColor: "#3b8bba",
                    pointStrokeColor: "rgba(60,141,188,1)",
                    pointHighlightFill: "#fff",
                    pointHighlightStroke: "rgba(60,141,188,1)",
                    data: [data[data['label'][6]]['2'],data[data['label'][5]]['2'],data[data['label'][4]]['2'],data[data['label'][3]]['2'],data[data['label'][2]]['2'],data[data['label'][1]]['2'],data[data['label'][0]]['2']]

                }
            ]
        };


        var ctx = document.getElementById("orders-chart").getContext("2d");

        // Instantiate a new chart
        var myLineChart = new Chart(ctx).Line(tempData);



    });}

控制器代码:

if (($request->ajax() || $request->wantsJson()) && $request['chart']='companies') {
  $response = [
    'data' => $type
  ];

  return response()->json($response, 200);
}


if (($request->ajax() || $request->wantsJson()) && $request['chart']='orders') {
  $response = [
    'data' => $orders
  ];

  return response()->json($response, 200);
}

订单图表获取公司数据。 我在这里做错了什么?

0 个答案:

没有答案