I have a problem that I have not been able to solve for many days and is able to get a general average of two different dates, I will leave the code javascript
and the code laravel
to help me
Take this little code because the rest is too long so take this to show, here I capture everything in sight and make an event
datachart = new Array();
var miArray= [];
$("[name='granjas[]'] option").each(function() {
miArray.push($(this).val());
});
var annos = [];
$("[name='fechas[]'] option").each(function() {
annos.push($(this).val());
});
$(document).on('click','#genera_grafica',()=>{
if ($('#select_parametro option:selected').val()==1) {
var token = $("#token").val();
var json = {
parametro:$('#select_parametro option:selected').val(),
desde:$('[name="fecha[]"]').val(),
granjas:$("[name='granjas[]']").val(),
array:miArray,
annos:annos
}
console.log(json)
if (!json.desde.length && json.desde.length === 0) {
swal({
title:'Upss :(!!',
text:'No hay Fecha/s Seleccionadas.!!',
type:'error',
showCancelButton:false,
confirmButtonClass:'btn-warning',
confirmButtonText:'Reintentar'
});
} else {
$.ajax({
method:'POST',
headers:{'X-CSRF-TOKEN': token},
url:'report_precebo_granjas',
data:json
}).done(function (msg) {
datachart = msg.data;
var general = new Array();
general.push(msg.general);
console.log(general);
datachart.push({
name:'Promedio General',
data:general
})
var categories = msg.categories;
if (datachart.length == 0) {
swal({
title:'No hay Resultados.',
text:'',
type:'error',
showCancelButton:false,
confirmButtonClass:'btn-warning',
confirmButtonText:'Reintentar'
});
} else {
Highcharts.chart('grafica', {
chart: {
type: 'column'
},
title: {
text: 'Reporte de la Conversion Ajustada.'
},
subtitle: {
text: ''
},
xAxis: {
categories: categories,
type: 'category',
labels: {
rotation: -45,
style: {
fontSize: '13px',
fontFamily: 'Verdana, sans-serif'
}
}
},
yAxis: {
min: 0,
title: {
text: 'Conversion Ajustada Final'
}
},
legend:{
enabled:true
},
tooltip: {
headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
'<td style="padding:0"><b>{point.y:.2f}</b></td></tr>',
footerFormat: '</table>',
shared: true,
useHTML: true
},
plotOptions: {
column: {
dataLabels: {
enabled: true,
format: '{point.y:.2f}',
color: '#000000',
rotation: -85,
align: 'right',
y: -30, // 10 pixels down from the top
style: {
fontSize: '10px',
fontFamily: 'Verdana, sans-serif'
}
}
}
},
series: datachart,
responsive: {
rules: [{
condition: {
maxWidth: 500
},
chartOptions: {
legend: {
align: 'center',
verticalAlign: 'bottom',
layout: 'horizontal'
},
yAxis: {
labels: {
align: 'left',
x: 0,
y: -5
},
title: {
text: null
}
},
subtitle: {
text: null
},
credits: {
enabled: false
}
}
}]
}
});
}
})
}
}
}
here in the code laravel
is the problem
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Precebo;
use App\Granja;
use Auth;
use DB;
use App\Http\Requests;
class GraficaGranjaController extends Controller {
public function index()
{
$granjas = Granja::all();
$precebo = Precebo::select('año_destete')
->groupBy('año_destete')->get();
return view('cercafe.graficas_granjas',compact('granjas',$granjas ,'precebo',$precebo));
}
public function report_precebo_granjas(Request $request)
{
if ($request->parametro == 1) {
if ($request->granjas !=0) {
$collection = Precebo::join('granjas','granjas.id','=','formulario_precebo.granja_id')
->select('granja_id','granjas.nombre_granja','año_destete',DB::raw('avg(conversion_ajust_fin) as total'))
->whereIn('año_destete',$request->desde)
->whereIn('granja_id',$request->granjas)
->groupBy('granjas.nombre_granja','año_destete')->get();
$arrayT = [];
foreach ($collection as $value) {
$arrayT[]=[$value->granja_id,$value->nombre_granja,$value->total,$value->año_destete];
}
$granjas = $request->granjas;
$annios = $request->desde;
$an = $request->annos;
$categories = array();
foreach ($annios as $years) {
foreach ($an as $y) {
if ($years == $y) {
$categories[] = $y;
}
}
}
$array = array();
$promedios = array();
foreach ($granjas as $granja) {
$values = array();
foreach ($annios as $year) {
$anoencontrado = 0;
foreach ($arrayT as $data) {
if ($granja == $data[0]) {
$value = $data[3] ? $data[2] : 0;
if ($data[3] == $year) {
$values[] = $value;
$anoencontrado = 1;
}
}
}
if ($anoencontrado === 0) {
$values[] = 0;
}
}
foreach ($arrayT as $data) {
if ($granja == $data[0]) {
$array[]=array(
'name' => $data[1],
'data' => $values
);
$suma = 0;
foreach ($values as $value) {
$suma += $value;
}
$promedios[]= array(
'value'=>(count($values) !== 0)? $suma / count($values) : 0
);
break;
}
}
}
$suma = 0;
foreach ($promedios as $promedio) {
$suma += $promedio['value'];
}
$promedioGeneral = (count($granjas) !== 0) ? $suma / count($granjas) : 0;
}else{
$collection = Precebo::join('granjas','granjas.id','=','formulario_precebo.granja_id')
->select('granja_id','granjas.nombre_granja','año_destete',DB::raw('avg(conversion_ajust_fin) as total'))
->whereIn('año_destete',$request->desde)
->groupBy('granjas.nombre_granja','año_destete')->get();
$arrayT = [];
foreach ($collection as $value) {
$arrayT[]=[$value->granja_id,$value->nombre_granja,$value->total,$value->año_destete];
}
$granjas = $request->array;
$annios = $request->desde;
$an = $request->annos;
$categories = array();
foreach ($annios as $years) {
foreach ($an as $y) {
if ($years == $y) {
$categories[] = $y;
}
}
}
$array = array();
$promedios = array();
foreach ($granjas as $granja) {
$values = array();
foreach ($annios as $year) {
$anoencontrado = 0;
foreach ($arrayT as $data) {
if ($granja == $data[0]) {
$value = $data[3] ? $data[2] : 0;
if ($data[3] == $year) {
$values[] = $value;
$anoencontrado = 1;
}
}
}
if ($anoencontrado === 0) {
$values[] = 0;
}
}
foreach ($arrayT as $data) {
if ($granja == $data[0]) {
$array[]=array(
'name' => $data[1],
'data' => $values
);
$suma = 0;
foreach ($values as $value) {
$suma += $value;
}
$promedios[]= array(
'value'=>(count($values) !== 0)? $suma / count($values) : 0
);
break;
}
}
}
$suma = 0;
$suma_granjas = 0;
foreach ($array as $arra) {
$suma_granjas++;
}
foreach ($promedios as $promedio) {
$suma += $promedio['value'];
}
$promedioGeneral = ($suma_granjas !== 0) ? $suma / $suma_granjas : 0;
}
}
}
I take everything of what comes with the AJAX
and I make a conditional in laravel
saying yes $request->granjas != 0
I mean that if it comes with loaded data, do the first procedure, in case the $request->granjas == 0
suppose, make me the other procedure, when I select a year nothing happens, the general average is fine, but when I select two or more years, the variable promedioGeneral
takes me the data of the two years and makes me a single average and my point is that I take out the general average of the years that I select, not a single general average, any doubt I will respond with great pleasure and thanks for your help