I need to place a graph of difference sales x expense. I bring the data through jquery and query in the database, I'm stuck because I don't know how to send the data separately to one, or if a single jquery can do the 2 different queries
This is the query to bring the sale:
function datagrafico2(base_url, year) {
namesMonth = ["Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Set", "Oct", "Nov", "Dic"];
$.ajax({
url: base_url + "Admin/getData?id=<?= $perfil->sede_id ?>",
type: "POST",
data: {
year: year
},
dataType: "json",
success: function(data) {
var dias = new Array();
var montos = new Array();
$.each(data, function(key, value) {
dias.push(namesMonth[value.mes - 1]);
valor = Number(value.monto);
montos.push(valor);
});
grafica(dias, montos);
}
});
And this is the query to bring the expenses:
function gasto2(base_url, year) {
namesMonth = ["Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Set", "Oct", "Nov", "Dic"];
$.ajax({
url: base_url + "Admin/getDataGasto?id=<?= $perfil->sede_id ?>",
type: "POST",
data: {
year: year
},
dataType: "json",
success: function(data) {
var dias = new Array();
var montos = new Array();
$.each(data, function(key, value) {
dias.push(namesMonth[value.mes - 1]);
valor = Number(value.monto);
montos.push(valor);
});
grafica(dias, montos);
}
});
}
and this is the code that draws the graph, at the moment I only have drawing the sale and in expenses I do not have the values accommodated
function grafica(dias, montos) {
var options = {
chart: {
type: 'bar',
events: {
dataPointSelection: function(event, chartContext, config) {
var fecha = config.w.globals.categoryLabels[config.dataPointIndex];
$.ajax({
url: base_url + "Admin/view?id=<?= $perfil->sede_id ?>",
type: "POST",
dataType: "html",
data: {
fecha: fecha
},
success: function(data) {
$('#myModal').modal('show'); {
$('.modal-body').html(data)
}
}
});
}
}
},
series: [{
name: 'monto',
data: montos
}],
stroke: {
show: true,
width: 3
},
plotOptions: {
bar: {
columnWidth: '100%',
distributed: true,
}
},
xaxis: {
labels: {
rotate: -45
},
categories: dias,
tickPlacement: 'on'
},
yaxis: {
title: {
text: 'Dias',
},
},
}
var chart = new ApexCharts(document.querySelector("#chart2"), options);
chart.render();
}
If you need me to attach the controller and model of each query tell me, I have everything with codeigniter
This was the query that I brought to expense
public function montosMesesGasto($year,$id){
$this->db->select("MONTH(fecha) as mes, SUM(monto) as monto");
$this->db->from("gastos");
$this->db->where("fecha >=",$year."-01-01");
$this->db->where("fecha <=",$year."-12-31");
$this->db->where("sede_id",$id);
$this->db->group_by("mes");
$this->db->order_by("mes");
$resultados = $this->db->get();
return $resultados->result();
}
and this is the one for sale
public function montosMesesGasto($year,$id){
$this->db->select("MONTH(fecha) as mes, SUM(monto) as monto");
$this->db->from("venta");
$this->db->where("fecha >=",$year."-01-01");
$this->db->where("fecha <=",$year."-12-31");
$this->db->where("sede_id",$id);
$this->db->group_by("mes");
$this->db->order_by("mes");
$resultados = $this->db->get();
return $resultados->result();
}
I did antonio's option and did a query in one. I'm going to edit the question to revise the query, that's how it was with a single query