matHistory.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { createGraph, switchPlot } from "../core";
  2. import { months, monthsPretty } from "../staticData/constants";
  3. import { addConfigField, clearChildren, getData, prettyMonthName } from "../utils";
  4. import { Graph } from "./graph";
  5. export class MatHistory implements Graph {
  6. id = "matHistory";
  7. displayName = "MAT History";
  8. configFieldIDs = ["metric", "ticker"];
  9. loadedData: any;
  10. urlParams: any;
  11. constructor(loadedData: any, urlParams: any)
  12. {
  13. this.loadedData = loadedData;
  14. this.urlParams = urlParams;
  15. }
  16. setConfigs(useURLParams?: boolean)
  17. {
  18. const updateFunc = function() {switchPlot();}
  19. const configDiv = document.getElementById("selectorSubtypes");
  20. if(configDiv)
  21. {
  22. clearChildren(configDiv);
  23. }
  24. configDiv?.appendChild(addConfigField("select", "metric", "Metric: ", {prettyValues: ["Volume", "Profit", "Price", "Produced", "Consumption", "Surplus"], values: ["volume", "profit", "price", "amount", "consumed", "surplus"]}, useURLParams ? this.urlParams.metric : undefined, updateFunc));
  25. configDiv?.appendChild(addConfigField("input", "ticker", "Ticker: ", undefined, useURLParams && this.urlParams.ticker ? this.urlParams.ticker : undefined, updateFunc));
  26. }
  27. async generatePlot(configValues: any, plotContainerID: string)
  28. {
  29. if(!configValues.ticker || configValues.ticker == ""){return;}
  30. // Get Data
  31. const totalTickerData = [];
  32. for(var i = 0; i < months.length; i++)
  33. {
  34. const monthData = await getData(this.loadedData, "prod", months[i]);
  35. totalTickerData.push(monthData[(configValues.ticker ?? "").toUpperCase()])
  36. }
  37. const tickerData = [] as number[]; // Data for the specific metric
  38. const validMonths = [] as string[]; // Months with data
  39. totalTickerData.forEach((data, i) => {
  40. if(!data){return;}
  41. validMonths.push(monthsPretty[i]);
  42. switch(configValues.metric)
  43. {
  44. case "volume":
  45. tickerData.push(data.volume);
  46. break;
  47. case "profit":
  48. tickerData.push(data.profit);
  49. break;
  50. case "price":
  51. tickerData.push(data.amount == 0 ? 0 : data.volume / data.amount);
  52. break;
  53. case "amount":
  54. tickerData.push(data.amount);
  55. break;
  56. case "consumed":
  57. tickerData.push(data.consumed);
  58. break;
  59. case "surplus":
  60. tickerData.push(data.amount - data.consumed);
  61. break;
  62. }
  63. });
  64. if(validMonths.length == 0){return;}
  65. const titles = {
  66. 'profit': 'Production Profit History of ',
  67. 'volume': 'Production Volume History of ',
  68. 'amount': 'Production Amount History of ',
  69. 'price': 'Price History of ',
  70. 'consumed': 'Consumption History of ',
  71. 'surplus': 'Surplus Production History of '
  72. } as any
  73. const yAxis = {
  74. 'profit': 'Daily Profit [$/day]',
  75. 'volume': 'Daily Volume [$/day]',
  76. 'amount': 'Daily Production [per day]',
  77. 'price': 'Price [$]',
  78. 'consumed': 'Daily Consumption [per day]',
  79. 'surplus': 'Daily Surplus [per day]'
  80. } as any
  81. // Create graph
  82. createGraph(plotContainerID, [{x: validMonths, y: tickerData, type: 'bar'}],
  83. {
  84. width: this.urlParams.hideOptions !== undefined ? undefined : 800,
  85. height: this.urlParams.hideOptions !== undefined ? undefined : 400,
  86. autosize: this.urlParams.hideOptions !== undefined,
  87. ...(this.urlParams.hideOptions !== undefined ? {margin: {
  88. l: 60, // left
  89. r: 10, // right
  90. t: 40, // top
  91. b: 60 // bottom
  92. }} : {}),
  93. title: {text: titles[configValues.metric] + (configValues.ticker ?? "").toUpperCase()},
  94. xaxis: {
  95. title: {text: 'Month'}
  96. },
  97. yaxis: {
  98. title: {text: yAxis[configValues.metric]}
  99. }
  100. }, {})
  101. }
  102. }