help
Jan 1, 2017
3
Level 13
Whats wrong with query?
I try to using chartjs for my char representation but its not ordering .when I count manually in my database total students its all right but when I see in my graph its order very different I don't understand whats wrong I did here is y controller
public function index()
{
$female = Student::select(DB::raw("count(*) as count"))
->orderBy('year_of_admission','asc')
->groupBy(DB::raw("(year_of_admission)"))
->where('gender', '=', 1)
->get()->toArray();
$female = array_column($female, 'count');
$male = Student::select(DB::raw("count(*) as count"))
->orderBy('year_of_admission','asc')
->groupBy(DB::raw("(year_of_admission)"))
->where('gender', '=', 2)
->get()->toArray();
$male = array_column($male, 'count');
$pie = Student::select(DB::raw("count(*) as count"))
->orderBy("year_of_admission")
->groupBy(DB::raw("(year_of_admission)"))
->get()->toArray();
$pie = array_column($pie, 'count');
$students = Student::with('city')->get();
// $sss = Student::with('states')->get();
$teachers = Teacher::with('states')->get();
//return view('home');
return view('home', compact('students','teachers'))
->with('female',json_encode($female,JSON_NUMERIC_CHECK))
->with('male',json_encode($male,JSON_NUMERIC_CHECK))
->with('pie',json_encode($pie,JSON_NUMERIC_CHECK));
}
and my javascript
var year = ['2005','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015'];
var female = <?php echo $female; ?>;
var male = <?php echo $male; ?>;
var ctx = document.getElementById("Chart");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: year,
datasets: [{
label: 'Female',
data: female,
backgroundColor:'#6e3d0e',
hoverBackgroundColor:'#6e3d0e',
borderWidth: 1
},{
label: 'Male',
data: male,
backgroundColor:'#0b0d3e',
hoverBackgroundColor:'#0b0d3e',
borderWidth: 1,
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
Thanks
Please or to participate in this conversation.