marketOverview.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 MarketOverview implements Graph {
  6. id = "marketOverview";
  7. displayName = "Market Overview";
  8. configFieldIDs = ["month", "ticker"];
  9. loadedData: any;
  10. urlParams: any;
  11. constructor(loadedData: any, urlParams: any) {
  12. this.loadedData = loadedData;
  13. this.urlParams = urlParams;
  14. }
  15. setConfigs(useURLParams?: boolean) {
  16. const updateFunc = switchPlot;
  17. const configDiv = document.getElementById("selectorSubtypes");
  18. if (configDiv) {
  19. clearChildren(configDiv);
  20. }
  21. configDiv?.appendChild(addConfigField("select", "month", "Month: ", {
  22. prettyValues: monthsPretty,
  23. "values": months
  24. }, useURLParams && this.urlParams.month ? this.urlParams.month : months[months.length - 1], updateFunc));
  25. configDiv?.appendChild(addConfigField("input", "ticker", "Ticker: ",
  26. undefined, useURLParams && this.urlParams.ticker ? this.urlParams.ticker : undefined, updateFunc));
  27. }
  28. async generatePlot(configValues: any, plotContainerID: string) {
  29. const ticker = configValues.ticker?.toUpperCase();
  30. if (!ticker) {
  31. return;
  32. }
  33. const companyData = await getData(this.loadedData, "company", configValues.month);
  34. const knownCompanies = await getData(this.loadedData, "knownCompanies");
  35. const labels = [] as string[];
  36. const parents = [] as string[];
  37. const values = [] as number[];
  38. let totalAmount = 0;
  39. let totalVolume = 0;
  40. let totalProfit = 0;
  41. for (const key of Object.keys(companyData.individual)) {
  42. const individualData = companyData.individual[key];
  43. const tickerData = individualData[ticker];
  44. if (!tickerData) {
  45. continue;
  46. }
  47. const companyObj = knownCompanies[key]
  48. labels.push(companyObj ? companyObj['Username'] : (key.slice(0, 5) + "..."));
  49. parents.push("Total");
  50. values.push(tickerData.amount);
  51. totalVolume += tickerData.volume;
  52. totalProfit += tickerData.profit;
  53. totalAmount += tickerData.amount;
  54. }
  55. if (labels.length === 0) {
  56. return;
  57. }
  58. labels.push("Total");
  59. parents.push("");
  60. values.push(totalAmount);
  61. const formatMoney = (num: number) => "$" + num.toLocaleString(undefined, { maximumFractionDigits: 0 });
  62. const title = `${ticker} Market - ${prettyMonthName(configValues.month)}`
  63. + "<br>"
  64. + `Produced per day: ${Math.round(totalAmount).toLocaleString()} ${ticker}`
  65. + "<br>"
  66. + `Volume: ${formatMoney(totalVolume)} | Profit: ${formatMoney(totalProfit)}`;
  67. // Create graph
  68. createGraph(plotContainerID, [{
  69. labels: labels,
  70. values: values,
  71. parents: parents,
  72. type: "treemap",
  73. branchvalues: "total",
  74. tiling: {
  75. pad: 0,
  76. },
  77. textposition: "middle center",
  78. hovertemplate: "%{label}<br>%{value:,.3~s}/day<br>%{percentEntry:.2%}<extra></extra>"
  79. }],
  80. {
  81. title: { text: title },
  82. width: this.urlParams.hideOptions !== undefined ? undefined : 800,
  83. height: this.urlParams.hideOptions !== undefined ? undefined : 400,
  84. autosize: this.urlParams.hideOptions !== undefined,
  85. ...(this.urlParams.hideOptions !== undefined ? {
  86. margin: {
  87. l: 10, // left
  88. r: 10, // right
  89. t: 60, // top
  90. b: 10 // bottom
  91. }
  92. } : {}),
  93. }, {});
  94. }
  95. }