ingredient.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. window.ingredientObj = {
  2. display: function(type, ingredient){
  3. clearScreen();
  4. document.querySelector("#ingredientStrand").style.display = "flex";
  5. document.querySelector("strand-selector").setAttribute("strand", "ingredient");
  6. if(ingredient){
  7. document.querySelector("#ingredientStrand h1").innerText = ingredient.name;
  8. let startDate = new Date();
  9. startDate.setFullYear(new Date().getFullYear() - 1);
  10. let ingredientGraph = new LineGraph(
  11. document.querySelector("#ingredientStrand canvas"),
  12. this.formatData(type, ingredient.id),
  13. "Quantity",
  14. "Date",
  15. {
  16. type: "date",
  17. start: startDate,
  18. end: new Date()
  19. }
  20. );
  21. }
  22. },
  23. formatData: function(type, id){
  24. dataList = new Array(365).fill(0);
  25. let today = new Date();
  26. if(type === "ingredient"){
  27. let dataLastDate = new Date(data.transactions[0].date);
  28. let dateRange = Math.floor((Date.UTC(today.getFullYear(), today.getMonth(), today.getDate()) - Date.UTC(dataLastDate.getFullYear(), dataLastDate.getMonth(), dataLastDate.getDate())) / (1000 * 60 * 60 * 24));
  29. for(let transaction of data.transactions){
  30. let transDate = new Date(transaction.date);
  31. let diff = Math.floor((Date.UTC(transDate.getFullYear(), transDate.getMonth(), transDate.getDate()) - Date.UTC(today.getFullYear(), today.getMonth(), today.getDate())) / (1000 * 60 * 60 * 24));
  32. if(diff <= 0){
  33. for(let recipe of transaction.recipes){
  34. for(let merchRecipe of data.merchant.recipes){
  35. if(merchRecipe._id === recipe.recipe){
  36. for(let ingredient of merchRecipe.ingredients){
  37. if(ingredient.ingredient === id){
  38. dataList[dateRange - Math.abs(diff)] += ingredient.quantity;
  39. }
  40. }
  41. break;
  42. }
  43. }
  44. }
  45. }
  46. }
  47. return dataList;
  48. }
  49. }
  50. }