You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

346 lines
8.3 KiB

  1. import { shuffle } from '../../../src/js/utils/helpers';
  2. import { HEATMAP_COLORS_YELLOW, HEATMAP_COLORS_BLUE } from '../../../src/js/utils/constants';
  3. import { fireballOver25, fireball_2_5, fireball_5_25, lineCompositeData,
  4. barCompositeData, typeData, trendsData, moonData, heatmapData } from './data';
  5. // ================================================================================
  6. let c1 = document.querySelector("#chart-composite-1");
  7. let c2 = document.querySelector("#chart-composite-2");
  8. let Chart = frappe.Chart; // eslint-disable-line no-undef
  9. let lineCompositeChart = new Chart (c1, {
  10. title: "Fireball/Bolide Events - Yearly (reported)",
  11. data: lineCompositeData,
  12. type: 'line',
  13. height: 190,
  14. colors: ['green'],
  15. isNavigable: 1,
  16. valuesOverPoints: 1,
  17. lineOptions: {
  18. dotSize: 8
  19. },
  20. // yAxisMode: 'tick'
  21. // regionFill: 1
  22. });
  23. let barCompositeChart = new Chart (c2, {
  24. data: barCompositeData,
  25. type: 'bar',
  26. height: 190,
  27. colors: ['violet', 'light-blue', '#46a9f9'],
  28. valuesOverPoints: 1,
  29. axisOptions: {
  30. xAxisMode: 'tick'
  31. },
  32. barOptions: {
  33. stacked: 1
  34. },
  35. });
  36. lineCompositeChart.parent.addEventListener('data-select', (e) => {
  37. let i = e.index;
  38. barCompositeChart.updateDatasets([
  39. fireballOver25[i], fireball_5_25[i], fireball_2_5[i]
  40. ]);
  41. });
  42. // ================================================================================
  43. let args = {
  44. title: "My Awesome Chart",
  45. data: typeData,
  46. type: 'axis-mixed',
  47. height: 250,
  48. colors: ['purple', 'magenta', 'light-blue'],
  49. maxLegendPoints: 6,
  50. maxSlices: 10,
  51. tooltipOptions: {
  52. formatTooltipX: d => (d + '').toUpperCase(),
  53. formatTooltipY: d => d + ' pts',
  54. }
  55. };
  56. let aggrChart = new Chart("#chart-aggr", args);
  57. Array.prototype.slice.call(
  58. document.querySelectorAll('.aggr-type-buttons button')
  59. ).map(el => {
  60. el.addEventListener('click', (e) => {
  61. let btn = e.target;
  62. let type = btn.getAttribute('data-type');
  63. args.type = type;
  64. let newChart = aggrChart.getDifferentChart(type);
  65. if(newChart){
  66. aggrChart = newChart;
  67. }
  68. Array.prototype.slice.call(
  69. btn.parentNode.querySelectorAll('button')).map(el => {
  70. el.classList.remove('active');
  71. });
  72. btn.classList.add('active');
  73. });
  74. });
  75. // Update values chart
  76. // ================================================================================
  77. let updateDataAllLabels = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon", "Tue",
  78. "Wed", "Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
  79. "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon"];
  80. let getRandom = () => Math.floor(Math.random() * 75 - 15);
  81. let updateDataAllValues = Array.from({length: 30}, getRandom);
  82. // We're gonna be shuffling this
  83. let updateDataAllIndices = updateDataAllLabels.map((d,i) => i);
  84. let getUpdateData = (source_array, length=10) => {
  85. let indices = updateDataAllIndices.slice(0, length);
  86. return indices.map((index) => source_array[index]);
  87. };
  88. let update_data = {
  89. labels: getUpdateData(updateDataAllLabels),
  90. datasets: [{
  91. "values": getUpdateData(updateDataAllValues)
  92. }],
  93. yMarkers: [
  94. {
  95. label: "Altitude",
  96. value: 25,
  97. type: 'dashed'
  98. }
  99. ],
  100. yRegions: [
  101. {
  102. label: "Range",
  103. start: 10,
  104. end: 45
  105. },
  106. ],
  107. };
  108. let update_chart = new Chart("#chart-update", {
  109. data: update_data,
  110. type: 'line',
  111. height: 250,
  112. colors: ['#ff6c03'],
  113. lineOptions: {
  114. // hideLine: 1,
  115. regionFill: 1
  116. },
  117. });
  118. let chart_update_buttons = document.querySelector('.chart-update-buttons');
  119. chart_update_buttons.querySelector('[data-update="random"]').addEventListener("click", () => {
  120. shuffle(updateDataAllIndices);
  121. let value = getRandom();
  122. let start = getRandom();
  123. let end = getRandom();
  124. let data = {
  125. labels: updateDataAllLabels.slice(0, 10),
  126. datasets: [{values: getUpdateData(updateDataAllValues)}],
  127. yMarkers: [
  128. {
  129. label: "Altitude",
  130. value: value,
  131. type: 'dashed'
  132. }
  133. ],
  134. yRegions: [
  135. {
  136. label: "Range",
  137. start: start,
  138. end: end
  139. },
  140. ],
  141. };
  142. update_chart.update(data);
  143. });
  144. chart_update_buttons.querySelector('[data-update="add"]').addEventListener("click", () => {
  145. let index = update_chart.state.datasetLength; // last index to add
  146. if(index >= updateDataAllIndices.length) return;
  147. update_chart.addDataPoint(
  148. updateDataAllLabels[index], [updateDataAllValues[index]]
  149. );
  150. });
  151. chart_update_buttons.querySelector('[data-update="remove"]').addEventListener("click", () => {
  152. update_chart.removeDataPoint();
  153. });
  154. // Trends Chart
  155. // ================================================================================
  156. let plotChartArgs = {
  157. title: "Mean Total Sunspot Count - Yearly",
  158. data: trendsData,
  159. type: 'line',
  160. height: 250,
  161. colors: ['#238e38'],
  162. lineOptions: {
  163. hideDots: 1,
  164. heatline: 1,
  165. },
  166. axisOptions: {
  167. xAxisMode: 'tick',
  168. yAxisMode: 'span',
  169. xIsSeries: 1
  170. }
  171. };
  172. new Chart("#chart-trends", plotChartArgs);
  173. Array.prototype.slice.call(
  174. document.querySelectorAll('.chart-plot-buttons button')
  175. ).map(el => {
  176. el.addEventListener('click', (e) => {
  177. let btn = e.target;
  178. let type = btn.getAttribute('data-type');
  179. let config = {};
  180. config[type] = 1;
  181. if(['regionFill', 'heatline'].includes(type)) {
  182. config.hideDots = 1;
  183. }
  184. // plotChartArgs.init = false;
  185. plotChartArgs.lineOptions = config;
  186. new Chart("#chart-trends", plotChartArgs);
  187. Array.prototype.slice.call(
  188. btn.parentNode.querySelectorAll('button')).map(el => {
  189. el.classList.remove('active');
  190. });
  191. btn.classList.add('active');
  192. });
  193. });
  194. // Event chart
  195. // ================================================================================
  196. let eventsData = {
  197. labels: ["Ganymede", "Callisto", "Io", "Europa"],
  198. datasets: [
  199. {
  200. "values": moonData.distances,
  201. "formatted": moonData.distances.map(d => d*1000 + " km")
  202. }
  203. ]
  204. };
  205. let eventsChart = new Chart("#chart-events", {
  206. title: "Jupiter's Moons: Semi-major Axis (1000 km)",
  207. data: eventsData,
  208. type: 'bar',
  209. height: 250,
  210. colors: ['grey'],
  211. isNavigable: 1,
  212. });
  213. let dataDiv = document.querySelector('.chart-events-data');
  214. eventsChart.parent.addEventListener('data-select', (e) => {
  215. let name = moonData.names[e.index];
  216. dataDiv.querySelector('.moon-name').innerHTML = name;
  217. dataDiv.querySelector('.semi-major-axis').innerHTML = moonData.distances[e.index] * 1000;
  218. dataDiv.querySelector('.mass').innerHTML = moonData.masses[e.index];
  219. dataDiv.querySelector('.diameter').innerHTML = moonData.diameters[e.index];
  220. dataDiv.querySelector('img').src = "./assets/img/" + name.toLowerCase() + ".jpg";
  221. });
  222. // Heatmap
  223. // ================================================================================
  224. let heatmapArgs = {
  225. title: "Monthly Distribution",
  226. data: heatmapData,
  227. type: 'heatmap',
  228. height: 115,
  229. discreteDomains: 1,
  230. colors: HEATMAP_COLORS_BLUE,
  231. legendScale: [0, 1, 2, 4, 5]
  232. };
  233. new Chart("#chart-heatmap", heatmapArgs);
  234. Array.prototype.slice.call(
  235. document.querySelectorAll('.heatmap-mode-buttons button')
  236. ).map(el => {
  237. el.addEventListener('click', (e) => {
  238. let btn = e.target;
  239. let mode = btn.getAttribute('data-mode');
  240. let discreteDomains = 0;
  241. if(mode === 'discrete') {
  242. discreteDomains = 1;
  243. }
  244. let colors = [];
  245. let colors_mode = document
  246. .querySelector('.heatmap-color-buttons .active')
  247. .getAttribute('data-color');
  248. if(colors_mode === 'halloween') {
  249. colors = HEATMAP_COLORS_YELLOW;
  250. } else if (colors_mode === 'blue') {
  251. colors = HEATMAP_COLORS_BLUE;
  252. }
  253. heatmapArgs.discreteDomains = discreteDomains;
  254. heatmapArgs.colors = colors;
  255. new Chart("#chart-heatmap", heatmapArgs);
  256. Array.prototype.slice.call(
  257. btn.parentNode.querySelectorAll('button')).map(el => {
  258. el.classList.remove('active');
  259. });
  260. btn.classList.add('active');
  261. });
  262. });
  263. Array.prototype.slice.call(
  264. document.querySelectorAll('.heatmap-color-buttons button')
  265. ).map(el => {
  266. el.addEventListener('click', (e) => {
  267. let btn = e.target;
  268. let colors_mode = btn.getAttribute('data-color');
  269. let colors = [];
  270. if(colors_mode === 'halloween') {
  271. colors = HEATMAP_COLORS_YELLOW;
  272. } else if (colors_mode === 'blue') {
  273. colors = HEATMAP_COLORS_BLUE;
  274. }
  275. let discreteDomains = 1;
  276. let view_mode = document
  277. .querySelector('.heatmap-mode-buttons .active')
  278. .getAttribute('data-mode');
  279. if(view_mode === 'continuous') {
  280. discreteDomains = 0;
  281. }
  282. heatmapArgs.discreteDomains = discreteDomains;
  283. heatmapArgs.colors = colors;
  284. new Chart("#chart-heatmap", heatmapArgs);
  285. Array.prototype.slice.call(
  286. btn.parentNode.querySelectorAll('button')).map(el => {
  287. el.classList.remove('active');
  288. });
  289. btn.classList.add('active');
  290. });
  291. });