graph.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. window.graphObj = {
  2. display: function(type, ingredient){
  3. clearScreen();
  4. document.querySelector("#graphAction").style.display = "flex";
  5. document.querySelector("#graphAction h1").innerText = `${ingredient.name} (${ingredient.unit})`;
  6. graph.line(document.querySelector("#graphAction canvas"), this.formatData(type, ingredient.id));
  7. },
  8. formatData: function(type, id){
  9. dataList = new Array(365).fill(0);
  10. let today = new Date();
  11. if(type === "ingredient"){
  12. for(let transaction of data.transactions){
  13. let transDate = new Date(transaction.date);
  14. let diff = Math.floor((Date.UTC(transDate.getFullYear(), transDate.getMonth(), transDate.getDate()) - Date.UTC(today.getFullYear(), today.getMonth(), today.getDate())) / (1000 * 60 * 60 * 24));
  15. if(diff <= 0){
  16. for(let recipe of transaction.recipes){
  17. for(let merchRecipe of data.merchant.recipes){
  18. if(merchRecipe._id === recipe.recipe){
  19. for(let ingredient of merchRecipe.ingredients){
  20. if(ingredient.ingredient === id){
  21. dataList[Math.abs(diff)] += ingredient.quantity;
  22. }
  23. }
  24. break;
  25. }
  26. }
  27. }
  28. }
  29. }
  30. return dataList;
  31. }
  32. }
  33. }