在laravel中的amcharts中填充动态数据

时间:2017-08-05 06:08:22

标签: javascript php laravel amcharts

大家好我想在amcharts中填充动态数据,我从数据库得到响应,如下所示

Object {Client Projects: 7, Intranet Projects: 1}

现在我想在我的图表中传递这些数据,我怎么能在这里做我的图表代码。

    //getting data in $projects variable
    var data = {!! json_encode($projects) !!};
    console.log(data); returning output  Object {Client Projects: 7, Intranet Projects: 1}

    var chart = AmCharts.makeChart("chartdiv2", {
                "type": "pie",
                "hideCredits":true,
                "dataProvider": dataProvider,
                "valueField": "litres",
                "titleField": "country",
                "colorField": "color",
                "labelsEnabled": true,
                "autoMargins": false,
                "marginTop": 20,
                "marginBottom": 20,
                "marginLeft": 10,
                "marginRight": 10,
                "pullOutRadius": 0,
                "labelColorField": "color",
                "balloon": {
                          "fixedPosition": true
                           }
                 });

1 个答案:

答案 0 :(得分:0)

我得到了答案

    var data = {!! json_encode($projects) !!};
     // console.log(data);
    var dataProvider = [];// this variable you have to pass in dataProvider inside chart 

    for(var key in data) {
      dataProvider.push({
        litres: data[key],
        country: key,
      });
    }
 // var test =JSON.stringify(dataProvider);
 // console.log(test);
  var chart = AmCharts.makeChart("chartdiv2", {
    "type": "pie",
    "hideCredits":true,
    "dataProvider": dataProvider,
    "valueField": "litres",
    "titleField": "country",
    "colorField": "color",
    "labelsEnabled": true,
    "autoMargins": false,
    "marginTop": 20,
    "marginBottom": 20,
    "marginLeft": 10,
    "marginRight": 10,
    "pullOutRadius": 0,
    "labelColorField": "color",
    "balloon": {
       "fixedPosition": true
       }
    });