I have two tables named income and expenses, each of these tables also have a currency type.
Im doing a query to find out how much income and expenses have ocurred per month per currency.
The query is fine and it returns this:
[
{"type":"income","monto":"1000","currency":"1","month":"6","year":"2016","idcurrency":"1","name":"Pesos argentinos","simbolo":"$"},
{"type":"expenditure","monto":"-500","currency":"1","month":"6","year":"2016","idcurrency":"1","name":"Pesos argentinos","simbolo":"$"},
{"type":"income","monto":"5000","currency":"2","month":"6","year":"2016","idcurrency":"2","name":"Dolares estadounidenses","simbolo":"u$d"},
{"type":"expenditure","monto":"-5000","currency":"2","month":"6","year":"2016","idcurrency":"2","name":"Dolares estadounidenses","simbolo":"u$d"},
{"type":"expenditure","monto":"-5000","currency":"3","month":"6","year":"2016","idcurrency":"3","name":"Pesos uruguayos","simbolo":"$"}
]
I need to plot it using charts js, but the problem is that according to the documentation of charts js, i need to set the labels and the values for each of these labels. It may happend that on any month some currency didnt have any movement but another did, so the data will be showing wrong. How can i solve this?
Thanks
Edit: Im trying to use a bar graph, for the labels im actually using this to generate them:
$desde = new DateTime();
$desde->modify('first day of this month');
$desde = $desde->sub(new DateInterval('P1Y'));
$hasta = new DateTime();
$hasta->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($desde, $interval, $hasta);
$info = array();
foreach ($period as $dt) {
array_push($info,$dt->format("Y-m"));
}
Patricks solution but i cannot get it to work:
// Bar chart
var ctx = document.getElementById("graficoIngresosCostos");
let dataSource =
<?=$dataSource?>;
//build the labels (you already did it in your OP. This is your info array
// that holds the first day of each month)
let labels = <?=$labels?>;
let currencies = <?=$monedas?>;
let datasets = [];
currencies.forEach(c => {
//filter out the incomes and expenditures for just the currency
// you're interested in
let incomeSource = dataSource.filter(
source=>source.nombre===c && source.concepto==='ingresos');
let expenseSource = dataSource.filter(
source=>source.nombre===c && source.concepto==='gastos');
let incomeDataset = { //dataset that holds this currency's income
label:c + ': ingresos',
data:[],
backgroundColor:'#03586A' //set color of bars
};
let expenseDataset = {//dataset that holds this currency's expense
label:c + ': gastos',
data:[],
backgroundColor:'#03586A' //set color of bars
};
//add datapoints to income and expense datasets for this currency
incomeSource.forEach(source=>{incomeDataset.data.push(source.monto)});
expenseSource.forEach(source=>{expenseDataset.data.push(source.monto)});
//todo: set backgroundColor for the bars of this dataset
//add the datasets to the chart
datasets.push(incomeDataset,expenseDataset);
});
//at this point you have all the datasets organized. Build chart
//ctx refers to the 2dContext of the canvas
let chart = new Chart(ctx,{
type:'bar',
data:{labels:labels, datasets:datasets}
});