core.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { graphs } from "./main";
  2. import { updatePermalink } from "./permalink";
  3. import { deepMerge } from "./utils";
  4. export function switchPlot()
  5. {
  6. const graphSelect = document.getElementById("graphType") as HTMLSelectElement;
  7. const graph = graphs.find(obj => obj.id == graphSelect.value);
  8. // Configure layout
  9. const oldGraph = document.getElementById("mainPlot");
  10. oldGraph?.remove();
  11. const newGraph = document.createElement("div");
  12. newGraph.id = "mainPlot";
  13. const graphContainer = document.getElementById("mainPlotContainer");
  14. graphContainer?.appendChild(newGraph);
  15. // Get data
  16. const configValues = {} as any;
  17. graph?.configFieldIDs.forEach(id => {
  18. const inputElem = document.getElementById(id) as HTMLInputElement;
  19. configValues[id] = inputElem?.value;
  20. });
  21. updatePermalink();
  22. (async () => {
  23. graph?.generatePlot(configValues, "mainPlot");
  24. })();
  25. }
  26. // Creating a Plotly graph using several defaults
  27. // Define defaults
  28. const defaultData = {marker: {color: 'rgb(247, 166, 0)'}, hovertemplate: '%{x}: %{y:,.3~s}<extra></extra>'};
  29. const defaultLayout = {
  30. title: {
  31. font: {color: '#eee', family: '"Droid Sans", sans-serif'}},
  32. xaxis: {
  33. title: {
  34. font: {color: '#bbb', family: '"Droid Sans", sans-serif'}
  35. },
  36. tickfont: {color: '#666', family: '"Droid Sans", sans-serif'},
  37. tickangle: -45
  38. },
  39. yaxis: {
  40. title: {
  41. font: {color: '#bbb', family: '"Droid Sans", sans-serif'}
  42. },
  43. tickfont: {color: '#666', family: '"Droid Sans", sans-serif'},
  44. gridcolor: '#323232'
  45. },
  46. plot_bgcolor: '#252525',
  47. paper_bgcolor: '#252525',
  48. dragmode: 'pan'
  49. };
  50. const defaultConfig = {
  51. displayModeBar: true,
  52. modeBarButtonsToRemove: ['lasso2d'], // Remove unwanted buttons
  53. displaylogo: false,
  54. scrollZoom: true,
  55. responsive: true
  56. };
  57. // Actually create the graph
  58. export function createGraph(plotContainerID: string, data: any[], layout: any, config: any)
  59. {
  60. // Assign default values
  61. for(var i = 0; i < data.length; i++)
  62. {
  63. data[i] = deepMerge(structuredClone(defaultData), data[i]);
  64. }
  65. layout = deepMerge(structuredClone(defaultLayout), layout);
  66. config = deepMerge(structuredClone(defaultConfig), config);
  67. // @ts-ignore
  68. Plotly.newPlot(plotContainerID, {'data': data, 'layout': layout, 'config': config})
  69. }
  70. // Create a table
  71. export function createTable(plotContainerID: string, titleText: string, headers: string[], data: any[], dataDisplay: any[])
  72. {
  73. const container = document.getElementById(plotContainerID);
  74. if(!container){return;}
  75. // Create title
  76. const title = document.createElement("div");
  77. title.textContent = titleText;
  78. title.classList.add("title");
  79. container.appendChild(title);
  80. const table = document.createElement("table");
  81. // Create header
  82. const header = document.createElement("thead");
  83. const headRow = document.createElement("tr");
  84. headers.forEach(label => {
  85. const column = document.createElement("th");
  86. column.textContent = label;
  87. headRow.appendChild(column);
  88. });
  89. header.appendChild(headRow);
  90. table.appendChild(header);
  91. // Create body
  92. const body = document.createElement("tbody");
  93. dataDisplay.forEach((dataRow: HTMLElement[]) => {
  94. const row = document.createElement("tr");
  95. dataRow.forEach(dataElem => {
  96. const td = document.createElement("td");
  97. td.appendChild(dataElem);
  98. row.appendChild(td);
  99. });
  100. body.appendChild(row);
  101. });
  102. table.appendChild(body);
  103. container.appendChild(table);
  104. }