topCompanies.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { createGraph, switchPlot } from "../core";
  2. import { months, monthsPretty, prettyModeNames } from "../staticData/constants";
  3. import { addConfigField, clearChildren, getData, prettyMonthName } from "../utils";
  4. import { Graph } from "./graph";
  5. export class TopCompanies implements Graph {
  6. id = "topCompanies";
  7. displayName = "Top Companies";
  8. configFieldIDs = ["metric", "month"];
  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"], values: ["volume", "profit"]}, useURLParams ? this.urlParams.metric : undefined, updateFunc));
  25. configDiv?.appendChild(addConfigField("select", "month", "Month: ", {prettyValues: monthsPretty, "values": months}, useURLParams && this.urlParams.month ? this.urlParams.month : months[months.length - 1], updateFunc));
  26. }
  27. async generatePlot(configValues: any, plotContainerID: string)
  28. {
  29. // Get Data
  30. const companyData = await getData(this.loadedData, "company", configValues.month);
  31. const knownCompanies = await getData(this.loadedData, "knownCompanies");
  32. // Convert the data object into an array of [companyID, volume] pairs
  33. const volumeArray = Object.entries(companyData.totals).map(([companyID, info]) => ({
  34. companyID,
  35. volume: (info as any)[configValues.metric]
  36. }));
  37. // Sort the array by volume in descending order
  38. volumeArray.sort((a, b) => b.volume - a.volume);
  39. // Extract tickers and volumes into separate arrays
  40. const companyIDs = volumeArray.map(item => item.companyID);
  41. const volumes = volumeArray.map(item => item.volume);
  42. const companyNames = [] as any[];
  43. companyIDs.forEach(id => {
  44. companyNames.push(knownCompanies[id] || (id.slice(0, 5) + "..."));
  45. });
  46. // Create graph
  47. createGraph(plotContainerID, [{x: companyNames, y: volumes, type: 'bar'}],
  48. {
  49. width: this.urlParams.hideOptions !== undefined ? undefined : 800,
  50. height: this.urlParams.hideOptions !== undefined ? undefined : 400,
  51. autosize: this.urlParams.hideOptions !== undefined,
  52. ...(this.urlParams.hideOptions !== undefined ? {margin: {
  53. l: 60, // left
  54. r: 10, // right
  55. t: 40, // top
  56. b: 100 // bottom
  57. }} : {}),
  58. title: {text: 'Top Companies (' + prettyModeNames[configValues.metric] + ') - ' + prettyMonthName(configValues.month)},
  59. xaxis: {
  60. title: {text: 'Ticker'},
  61. range: [-0.5, 29.5]
  62. },
  63. yaxis: {
  64. title: {text: prettyModeNames[configValues.metric] + ' [$/day]'},
  65. range: [0, null]
  66. }
  67. }, {})
  68. }
  69. }