瀏覽代碼

Display costs for categories.

Lee Morgan 5 年之前
父節點
當前提交
51ed7f124c
共有 2 個文件被更改,包括 72 次插入22 次删除
  1. 22 21
      views/dashboardPage/js/classes/Merchant.js
  2. 50 1
      views/dashboardPage/js/strands/analytics.js

+ 22 - 21
views/dashboardPage/js/classes/Merchant.js

@@ -57,7 +57,7 @@ class MerchantIngredient{
         let total = 0;
         const {start, end} = this._parent.getTransactionIndices(from, to);
 
-        for(let i = start; i <= end; i++){
+        for(let i = start; i < end; i++){
             total += this._parent.transactions[i].getIngredientQuantity(this._ingredient);
         }
 
@@ -325,15 +325,13 @@ class Merchant{
     }
 
     getTransactions(from = 0, to = new Date()){
-        if(merchant._transactions.length <= 0){
-            return [];
-        }
+        if(merchant._transactions.length <= 0) return [];
 
         if(from === 0) from = this._transactions[this._transactions.length-1].date;
 
         const {start, end} = this.getTransactionIndices(from, to);
 
-        return this._transactions.slice(start, end + 1);
+        return this._transactions.slice(start, end);
     }
 
     /*
@@ -459,7 +457,7 @@ class Merchant{
         const {start, end} = this.getTransactionIndices(from, to);
 
         let total = 0;
-        for(let i = start; i <= end; i++){
+        for(let i = start; i < end; i++){
             for(let j = 0; j < this._transactions[i].recipes.length; j++){
                 for(let k = 0; k < this.recipes.length; k++){
                     if(this._transactions[i].recipes[j].recipe === this.recipes[k]){
@@ -526,7 +524,7 @@ class Merchant{
         const {start, end} = this.getTransactionIndices(from, to);
 
         let recipeList = [];
-        for(let i = start; i <= end; i++){
+        for(let i = start; i < end; i++){
             for(let j = 0; j < this._transactions[i].recipes.length; j++){
                 let exists = false;
                 for(let k = 0; k < recipeList.length; k++){
@@ -582,24 +580,27 @@ class Merchant{
     }
 
     getTransactionIndices(from, to){
-        let start, end;
-        
-        for(let i = this._transactions.length - 1; i >= 0; i--){
-            if(this._transactions[i].date >= from){
-                end = i;
-                break;
+        let start = 0;
+        let end = 0;
+
+        if(
+            from > this._transactions[0].date ||
+            to >= this._transactions[this._transactions.length-1].date
+        ){
+            for(let i = this._transactions.length - 1; i >= 0; i--){
+                if(this._transactions[i].date > from){
+                    end = i + 1;
+                    break;
+                }
             }
-        }
         
-        for(let i = 0; i < this._transactions.length; i++){
-            if(this._transactions[i].date < to){
-                start = i;
-                break;
+            for(let i = 0; i < this._transactions.length; i++){
+                if(this._transactions[i].date <= to){
+                    start = i;
+                    break;
+                }
             }
         }
-
-        if(end === undefined) return false;
-
         return {start: start, end: end};
     }
 }

+ 50 - 1
views/dashboardPage/js/strands/analytics.js

@@ -181,7 +181,7 @@ let analytics = {
 
         let yaxis = `QUANTITY (${this.ingredient.unit.toUpperCase()})`;
 
-        const layout = {
+        let layout = {
             title: this.ingredient.name.toUpperCase(),
             xaxis: {title: "DATE"},
             yaxis: {title: yaxis},
@@ -234,7 +234,56 @@ let analytics = {
     },
 
     displayCategory: function(){
+        if(this.category === undefined) this.category = merchant.categorizeIngredients()[0];
+
+        let startOfDay = new Date();
+        startOfDay.setHours(0, 0, 0, 0);
+        let endOfDay = new Date();
+        endOfDay.setDate(endOfDay.getDate() + 1);
+        endOfDay.setHours(0, 0, 0, 0);
+
+        let dates = [];
+        let quantities = [];
+
+        for(let i = 0; i < 30; i++){
+            dates.push(new Date(startOfDay));
+            let transactions = merchant.getTransactions(startOfDay, endOfDay);
+
+            let quantity = 0;
+            for(let j = 0; j < transactions.length; j++){
+                for(let k = 0; k < this.category.ingredients.length; k++){
+                    quantity += transactions[j].getIngredientQuantity(this.category.ingredients[k].ingredient);
+                }
+            }
+
+            startOfDay.setDate(startOfDay.getDate() - 1);
+            endOfDay.setDate(endOfDay.getDate() - 1);
+            quantities.push(quantity);
+        }
+
+        let trace = {
+            x: dates,
+            y: quantities,
+            mode: "lines+markers",
+            line: {
+                color: "rgb(255, 99, 107)"
+            }
+        };
+
+        let layout = {
+            title: this.category.name,
+            xaxis: {title: "DATE"},
+            yaxis: {title: "COST ($)"},
+            margin: {
+                l: 40,
+                r: 10,
+                b: 20,
+                t: 30
+            },
+            paper_bgcolor: "white"
+        }
 
+        Plotly.newPlot("analCategoriesGraph", [trace], layout);
     },
 
     displayRecipe: function(){