Add earned + spent, needs cleaning up still.

This commit is contained in:
James Cole
2025-09-21 08:54:26 +02:00
parent 69cae3ae55
commit 90623101a3

View File

@@ -54,12 +54,21 @@ export default () => ({
if (data.hasOwnProperty(i)) { if (data.hasOwnProperty(i)) {
let current = data[i]; let current = data[i];
let code = current.currency_code; let code = current.currency_code;
if (!series.hasOwnProperty(code)) {
series[code] = { // create two series, "spent" and "earned".
name: code, for(const type of ['spent', 'earned']) {
let typeCode = code + '_' + type;
if (!series.hasOwnProperty(typeCode)) {
series[typeCode] = {
name: typeCode,
code: code,
type: type,
yAxisID: '', yAxisID: '',
data: {}, data: {},
}; };
}
}
if (!currencies.includes(code)) {
currencies.push(code); currencies.push(code);
} }
} }
@@ -76,31 +85,38 @@ export default () => ({
code = current.primary_currency_code; code = current.primary_currency_code;
} }
// twice again, for speny AND earned.
for(const type of ['spent', 'earned']) {
let typeCode = code + '_' + type;
// loop series, add 0 if not present or add actual amount. // loop series, add 0 if not present or add actual amount.
for (const ii in series) { for (const ii in series) {
if (series.hasOwnProperty(ii)) { if (series.hasOwnProperty(typeCode)) {
let amount = 0.0; let amount = 0.0;
if (code === ii) { if (typeCode === ii) {
// this series' currency matches this column's currency. // this series' currency matches this column's currency.
amount = parseFloat(current.entries.spent); amount = parseFloat(current.entries[type]);
if(this.convertToPrimary) { if(this.convertToPrimary) {
amount = parseFloat(current.entries.pc_entries.spent); amount = parseFloat(current.entries.pc_entries[type]);
} }
yAxis = 'y' + code; yAxis = 'y' + typeCode;
} }
if (series[ii].data.hasOwnProperty(current.label)) { if (series[typeCode].data.hasOwnProperty(current.label)) {
// there is a value for this particular currency. The amount from this column will be added. // there is a value for this particular currency. The amount from this column will be added.
// (even if this column isn't recorded in this currency and a new filler value is written) // (even if this column isn't recorded in this currency and a new filler value is written)
// this is so currency conversion works. // this is so currency conversion works.
series[ii].data[current.label] = series[ii].data[current.label] + amount; series[typeCode].data[current.label] = series[typeCode].data[current.label] + amount;
} }
if (!series[ii].data.hasOwnProperty(current.label)) { if (!series[typeCode].data.hasOwnProperty(current.label)) {
// this column's amount is not yet set in this series. // this column's amount is not yet set in this series.
series[ii].data[current.label] = amount; series[typeCode].data[current.label] = amount;
} }
} }
} }
}
// add label to x-axis, not unimportant. // add label to x-axis, not unimportant.
if (!options.data.labels.includes(current.label)) { if (!options.data.labels.includes(current.label)) {
options.data.labels.push(current.label); options.data.labels.push(current.label);
@@ -111,9 +127,10 @@ export default () => ({
let count = 0; let count = 0;
for (const i in series) { for (const i in series) {
let yAxisID = 'y' + i; let yAxisID = 'y' + i;
let currencyCode = i.replace('_spent', '').replace('_earned', '');
let dataset = { let dataset = {
label: i, label: i,
currency_code: i, currency_code: currencyCode,
yAxisID: yAxisID, yAxisID: yAxisID,
data: [], data: [],
// backgroundColor: getColors(null, 'background'), // backgroundColor: getColors(null, 'background'),