首页 > 解决方案 > 数据从数据库到 laravel 中的 fusioncharts

问题描述

我想在 laravel 中将数据库中的数据显示到 myFusionCharts,但不知何故我卡住了。我无法从文档中找到任何答案,因为他们在样本中使用了预定义的数据集。

任何帮助将非常感激。

这是我的控制器:

 public function index()
{
    $subj = DB::Table('tbl_subjects')               
            ->select('subject')
            ->get();
            // ->toArray()

    foreach ( $subj as $s){
        // $c = $s->subject;
        $c[] = DB::table('tbl_ordinances')
            ->where('subject', 'like', '%'.$s->subject.'%')
            ->get()
            ->count();

    }     
        // dd($subj,$c);
    return view('statistics',compact('subj','c'));
}

我“ dd($subj,$c); ”它返回这个值:

Collection {#302 ▼
#items: array:9 [▼
0 => {#309 ▼
  +"subject": "Peace & Order and Public Safety"
}
1 => {#311 ▼
  +"subject": "Infrastructure"
}
2 => {#312 ▼
  +"subject": "Livelihood/ Poverty Reduction"
}
3 => {#313 ▼
  +"subject": "Finance/ Investment"
}
4 => {#314 ▼
  +"subject": "Administrative Development"
}
5 => {#315 ▼
  +"subject": "Agricultural Development"
}
6 => {#316 ▼
  +"subject": "Social Services"
}
7 => {#317 ▼
  +"subject": "Environment and Disaster Management"
}
8 => {#318 ▼
  +"subject": "Tourism"
}
]
}
array:9 [▼
0 => 2
1 => 0
2 => 0
3 => 0
4 => 4
5 => 0
6 => 3
7 => 6
8 => 1
]

这是我的刀片:

<script type="text/javascript">
FusionCharts.ready(function() {
    var revenueChart = new FusionCharts({
type: 'pie2d',
renderAt: 'chart-container',
width: '500',
height: '500',
dataFormat: 'json',
dataSource: {
  "chart": {
    "caption": "Municipal Ordinances by subject categories",
    // "subCaption": "Last year",
    "numberPrefix": null,
    "showPercentInTooltip": "1",
    "decimals": "1",        
    "theme": "fusion",
  },
  "data": [  // I want THOSE VALUES HERE... but I don't know how..
    {"label": "h","value": "25" },   
    {"label": "h2","value": "25"},    
    {"label": "h3","value": "55"},  
  ]

}
}).render();
});
</script>

<div id="chart-container">FusionCharts will render here..</div>

标签: javascriptphplaravel-5fusioncharts

解决方案


推荐阅读