Sort expenses and revenue by percentage.

This commit is contained in:
James Cole 2021-03-31 08:10:46 +02:00
parent b6109ca93e
commit 0756054690
No known key found for this signature in database
GPG Key ID: B5669F9493CDE38D
5 changed files with 39 additions and 25 deletions

View File

@ -71,6 +71,7 @@ import {createNamespacedHelpers} from "vuex";
const {mapState, mapGetters, mapActions, mapMutations} = createNamespacedHelpers('dashboard/index') const {mapState, mapGetters, mapActions, mapMutations} = createNamespacedHelpers('dashboard/index')
// TODO same as credit list but reversed
export default { export default {
name: "MainCreditList", name: "MainCreditList",
@ -131,21 +132,27 @@ export default {
}, },
parseIncome(data) { parseIncome(data) {
for (let mainKey in data) { for (let mainKey in data) {
if (data.hasOwnProperty(mainKey) ) { if (data.hasOwnProperty(mainKey)) {
mainKey = parseInt(mainKey); mainKey = parseInt(mainKey);
// contains currency info and entries. // contains currency info and entries.
let current = data[mainKey]; let current = data[mainKey];
if (0 === mainKey) { current.pct = 0;
this.max = data[mainKey].difference_float; this.max = current.difference_float > this.max ? current.difference_float : this.max;
current.pct = 100;
}
if (0 !== mainKey) {
// calc percentage:
current.pct = (data[mainKey].difference_float / this.max) * 100;
}
this.income.push(current); this.income.push(current);
} }
} }
if (0 === this.max) {
this.max = 1;
}
// now sort + pct:
for (let i in this.income) {
if (this.income.hasOwnProperty(i)) {
let current = this.income[i];
current.pct = (current.difference_float / this.max) * 100;
this.income[i] = current;
}
}
this.income.sort((a,b) => (a.pct > b.pct) ? -1 : ((b.pct > a.pct) ? 1 : 0));
} }
} }
} }

View File

@ -78,7 +78,7 @@ export default {
return { return {
locale: 'en-US', locale: 'en-US',
expenses: [], expenses: [],
max: 0, min: 0,
loading: true, loading: true,
error: false error: false
} }
@ -132,20 +132,27 @@ export default {
parseExpenses(data) { parseExpenses(data) {
for (let mainKey in data) { for (let mainKey in data) {
if (data.hasOwnProperty(mainKey) && /^0$|^[1-9]\d*$/.test(mainKey) && mainKey <= 4294967294) { if (data.hasOwnProperty(mainKey) && /^0$|^[1-9]\d*$/.test(mainKey) && mainKey <= 4294967294) {
// contains currency info and entries.
let current = data[mainKey]; let current = data[mainKey];
if (0 === parseInt(mainKey)) { current.pct = 0;
this.max = data[mainKey].difference_float;
current.pct = 100;
}
if (0 !== parseInt(mainKey)) {
// calc percentage:
current.pct = (data[mainKey].difference_float / this.max) * 100;
}
this.expenses.push(current);
this.min = current.difference_float < this.min ? current.difference_float : this.min;
this.expenses.push(current);
} }
} }
if (0 === this.min) {
this.min = -1;
}
// now sort + pct:
for (let i in this.expenses) {
if (this.expenses.hasOwnProperty(i)) {
let current = this.expenses[i];
current.pct = (current.difference_float*-1 / this.min*-1) * 100;
this.expenses[i] = current;
}
}
this.expenses.sort((a,b) => (a.pct > b.pct) ? -1 : ((b.pct > a.pct) ? 1 : 0));
} }
} }
} }

View File

@ -2002,9 +2002,9 @@ caniuse-api@^3.0.0:
lodash.uniq "^4.5.0" lodash.uniq "^4.5.0"
caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001181: caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001181:
version "1.0.30001204" version "1.0.30001205"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001204.tgz#256c85709a348ec4d175e847a3b515c66e79f2aa" resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001205.tgz#d79bf6a6fb13196b4bb46e5143a22ca0242e0ef8"
integrity sha512-JUdjWpcxfJ9IPamy2f5JaRDCaqJOxDzOSKtbdx4rH9VivMd1vIzoPumsJa9LoMIi4Fx2BV2KZOxWhNkBjaYivQ== integrity sha512-TL1GrS5V6LElbitPazidkBMD9sa448bQDDLrumDqaggmKFcuU2JW1wTOHJPukAcOMtEmLcmDJEzfRrf+GjM0Og==
chalk@^1.1.3: chalk@^1.1.3:
version "1.1.3" version "1.1.3"

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long