analytics.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. let analytics = {
  2. display: function(){
  3. const itemsList = document.getElementById("itemsList");
  4. while(itemsList.children.length > 0){
  5. itemsList.removeChild(itemsList.firstChild);
  6. }
  7. for(let i = 0; i < merchant.ingredients.length; i++){
  8. let li = document.createElement("li");
  9. li.classList.add("itemButton");
  10. li.item = merchant.ingredients[i];
  11. li.innerText = merchant.ingredients[i].ingredient.name;
  12. li.onclick = ()=>{this.ingredientDisplay(merchant.ingredients[i], li)};
  13. itemsList.appendChild(li);
  14. }
  15. },
  16. ingredientDisplay: function(ingredient, li){
  17. const itemsList = document.getElementById("itemsList");
  18. for(let i = 0; i < itemsList.children.length; i++){
  19. itemsList.children[i].classList.remove("analItemActive");
  20. }
  21. li.classList.add("analItemActive");
  22. let startDate = new Date();
  23. startDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() - 30);
  24. //Get list of recipes that contain the ingredient
  25. let containingRecipes = [];
  26. for(let i = 0; i < merchant.recipes.length; i++){
  27. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  28. if(merchant.recipes[i].ingredients[j].ingredient === ingredient.ingredient){
  29. containingRecipes.push({
  30. recipe: merchant.recipes[i],
  31. quantity: merchant.recipes[i].ingredients[j].quantity
  32. });
  33. break;
  34. }
  35. }
  36. }
  37. //Create Graph
  38. const dateIndices = merchant.transactionIndices(startDate);
  39. let quantities = [];
  40. let dates = [];
  41. let currentDate = merchant.transactions[dateIndices[0]].date;
  42. let currentQuantity = 0;
  43. for(let i = dateIndices[0]; i < dateIndices[1]; i++){
  44. if(currentDate.getDate() !== merchant.transactions[i].date.getDate()){
  45. quantities.push(ingredient.ingredient.convert(currentQuantity));
  46. dates.push(currentDate);
  47. currentQuantity = 0;
  48. currentDate = merchant.transactions[i].date;
  49. }
  50. for(let j = 0; j < merchant.transactions[i].recipes.length; j++){
  51. for(let k = 0; k < containingRecipes.length; k++){
  52. if(merchant.transactions[i].recipes[j].recipe === containingRecipes[k].recipe){
  53. for(let l = 0; l < merchant.transactions[i].recipes[j].recipe.ingredients.length; l++){
  54. const transIngredient = merchant.transactions[i].recipes[j].recipe.ingredients[l];
  55. if(transIngredient.ingredient === ingredient.ingredient){
  56. currentQuantity += transIngredient.quantity * merchant.transactions[i].recipes[j].quantity;
  57. break;
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. let trace = {
  65. x: dates,
  66. y: quantities,
  67. mode: "lines+markers"
  68. }
  69. const layout = {
  70. title: ingredient.ingredient.name
  71. }
  72. Plotly.newPlot("itemUseGraph", [trace], layout);
  73. //Create daily use card
  74. let sum = 0;
  75. for(let i = 0; i < quantities.length; i++){
  76. sum += quantities[i];
  77. }
  78. document.getElementById("analAvgUse").innerText = `${(sum / 30).toFixed(2)} ${ingredient.ingredient.unit}`;
  79. }
  80. }
  81. module.exports = analytics;