Reputation: 1
I want to display the data from the database to myFusionCharts in laravel but somehow I got stuck. I can't find any answers from the documentations since they use predefined dataset in their sample.
Any help would be much appreciated.
This is my controller:
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'));
}
I " dd($subj,$c); " and it returns this values:
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
]
This is my blade:
<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>
Upvotes: 0
Views: 448
Reputation: 172
You can try with below documents to achieve your case -
<?php
//address of the server where db is installed
$servername = "localhost";
//username to connect to the db
//the default value is root
$username = "root";
//password to connect to the db
//this is the value you specified during installation of WAMP stack
$password = "password";
//name of the db under which the table is created
$dbName = "test";
//establishing the connection to the db.
$conn = new mysqli($servername, $username, $password, $dbName);
//checking if there were any error during the last connection attempt
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
Document reference - https://www.fusioncharts.com/blog/charts-laravel-web-application/
https://www.fusioncharts.com/dev/using-with-server-side-languages/tutorials/php-mysql-charts
Upvotes: 0