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.
 
 
 

523 line
13 KiB

  1. // Composite Chart
  2. // ================================================================================
  3. let report_count_list = [17, 40, 33, 44, 126, 156,
  4. 324, 333, 478, 495, 527];
  5. let bar_composite_data = {
  6. labels: ["2007", "2008", "2009", "2010", "2011", "2012",
  7. "2013", "2014", "2015", "2016", "2017"],
  8. yMarkers: [
  9. {
  10. label: "Marker 1",
  11. value: 420,
  12. },
  13. {
  14. label: "Marker 2",
  15. value: 250,
  16. }
  17. ],
  18. yRegions: [
  19. {
  20. label: "Region Y 1",
  21. start: 100,
  22. end: 300
  23. },
  24. ],
  25. datasets: [{
  26. "name": "Events",
  27. "values": report_count_list,
  28. // "formatted": report_count_list.map(d => d + " reports")
  29. }]
  30. };
  31. let line_composite_data = {
  32. labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
  33. datasets: [{
  34. "values": [37, 36, 32, 33, 12, 34, 52, 45, 58, 57, 64, 35],
  35. // "values": [36, 46, 45, 32, 27, 31, 30, 36, 39, 49, 40, 40],
  36. // "values": [-36, -46, -45, -32, -27, -31, -30, -36, -39, -49, -40, -40],
  37. }]
  38. };
  39. let more_line_data = [
  40. [4, 0, 3, 1, 1, 2, 1, 2, 1, 0, 1, 1],
  41. // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  42. [2, 3, 3, 2, 1, 4, 0, 1, 2, 7, 11, 4],
  43. [7, 7, 2, 4, 0, 1, 5, 3, 1, 2, 0, 1],
  44. [0, 2, 6, 2, 2, 1, 2, 3, 6, 3, 7, 10],
  45. [9, 10, 8, 10, 6, 5, 8, 8, 24, 15, 10, 13],
  46. [9, 13, 16, 9, 4, 5, 7, 10, 14, 22, 23, 24],
  47. [20, 22, 28, 19, 28, 19, 14, 19, 51, 37, 29, 38],
  48. [29, 20, 22, 16, 16, 19, 24, 26, 57, 31, 46, 27],
  49. [36, 24, 38, 27, 15, 22, 24, 38, 32, 57, 139, 26],
  50. [37, 36, 32, 33, 12, 34, 52, 45, 58, 57, 64, 35],
  51. [36, 46, 45, 32, 27, 31, 30, 36, 39, 58, 82, 62],
  52. // [36, 46, 45, 32, 27, 31, 30, 36, 39, 49, 40, 40]
  53. // [-36, -46, -45, -32, -27, -31, -30, -36, -39, -49, -40, -40]
  54. ];
  55. let c1 = document.querySelector("#chart-composite-1");
  56. let c2 = document.querySelector("#chart-composite-2");
  57. let bar_composite_chart = new Chart (c1, {
  58. title: "Fireball/Bolide Events - Yearly (reported)",
  59. data: bar_composite_data,
  60. type: 'line',
  61. height: 180,
  62. colors: ['green'],
  63. isNavigable: 1,
  64. isSeries: 1,
  65. // valuesOverPoints: 1,
  66. lineOptions: {
  67. dotSize: 8
  68. },
  69. // yAxisMode: 'tick'
  70. // regionFill: 1
  71. });
  72. let line_composite_chart = new Chart (c2, {
  73. data: line_composite_data,
  74. type: 'bar',
  75. height: 180,
  76. colors: ['#46a9f9'],
  77. isSeries: 1,
  78. valuesOverPoints: 1,
  79. xAxisMode: 'tick'
  80. });
  81. bar_composite_chart.parent.addEventListener('data-select', (e) => {
  82. line_composite_chart.updateDataset(more_line_data[e.index]);
  83. });
  84. // Demo Chart (bar, linepts, scatter(blobs), percentage)
  85. // ================================================================================
  86. let type_data = {
  87. labels: ["12am-3am", "3am-6am", "6am-9am", "9am-12pm",
  88. "12pm-3pm", "3pm-6pm", "6pm-9pm", "9pm-12am"],
  89. yMarkers: [
  90. {
  91. label: "Marker 1",
  92. value: 42,
  93. type: 'dashed'
  94. },
  95. {
  96. label: "Marker 2",
  97. value: 25,
  98. type: 'dashed'
  99. }
  100. ],
  101. yRegions: [
  102. {
  103. label: "Region Y 1",
  104. start: -10,
  105. end: 50
  106. },
  107. ],
  108. // will depend on series code for calculating X values
  109. // xRegions: [
  110. // {
  111. // label: "Region X 2",
  112. // start: ,
  113. // end: ,
  114. // }
  115. // ],
  116. datasets: [
  117. {
  118. name: "Some Data",
  119. values: [18, 40, 30, 35, 8, 52, 17, -4],
  120. axisPosition: 'right',
  121. chartType: 'bar'
  122. },
  123. {
  124. name: "Another Set",
  125. values: [30, 50, -10, 15, 18, 32, 27, 14],
  126. axisPosition: 'right',
  127. chartType: 'bar'
  128. },
  129. {
  130. name: "Yet Another",
  131. values: [15, 20, -3, -15, 58, 12, -17, 37],
  132. chartType: 'line'
  133. }
  134. // temp : Stacked
  135. // {
  136. // name: "Some Data",
  137. // values:[25, 30, 50, 45, 18, 12, 27, 14]
  138. // },
  139. // {
  140. // name: "Another Set",
  141. // values: [18, 20, 30, 35, 8, 7, 17, 4]
  142. // },
  143. // {
  144. // name: "Another Set",
  145. // values: [11, 8, 19, 15, 3, 4, 10, 2]
  146. // },
  147. ]
  148. };
  149. let type_chart = new Chart("#chart-types", {
  150. // title: "My Awesome Chart",
  151. data: type_data,
  152. type: 'bar',
  153. height: 250,
  154. colors: ['purple', 'magenta', 'light-blue'],
  155. isSeries: 1,
  156. xAxisMode: 'tick',
  157. yAxisMode: 'span',
  158. valuesOverPoints: 1,
  159. isNavigable: 1,
  160. barOptions: {
  161. stacked: 1
  162. },
  163. tooltipOptions: {
  164. formatTooltipX: d => (d + '').toUpperCase(),
  165. formatTooltipY: d => d + ' pts'
  166. }
  167. });
  168. Array.prototype.slice.call(
  169. document.querySelectorAll('.chart-type-buttons button')
  170. ).map(el => {
  171. el.addEventListener('click', (e) => {
  172. let btn = e.target;
  173. let type = btn.getAttribute('data-type');
  174. let newChart = type_chart.getDifferentChart(type);
  175. if(newChart){
  176. type_chart = newChart;
  177. }
  178. Array.prototype.slice.call(
  179. btn.parentNode.querySelectorAll('button')).map(el => {
  180. el.classList.remove('active');
  181. });
  182. btn.classList.add('active');
  183. });
  184. });
  185. // Trends Chart
  186. // ================================================================================
  187. let trends_data = {
  188. labels: [1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976,
  189. 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986,
  190. 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
  191. 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
  192. 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016] ,
  193. datasets: [
  194. {
  195. "values": [132.9, 150.0, 149.4, 148.0, 94.4, 97.6, 54.1, 49.2, 22.5, 18.4,
  196. 39.3, 131.0, 220.1, 218.9, 198.9, 162.4, 91.0, 60.5, 20.6, 14.8,
  197. 33.9, 123.0, 211.1, 191.8, 203.3, 133.0, 76.1, 44.9, 25.1, 11.6,
  198. 28.9, 88.3, 136.3, 173.9, 170.4, 163.6, 99.3, 65.3, 45.8, 24.7,
  199. 12.6, 4.2, 4.8, 24.9, 80.8, 84.5, 94.0, 113.3, 69.8, 39.8]
  200. }
  201. ]
  202. };
  203. let plotChartArgs = {
  204. title: "Mean Total Sunspot Count - Yearly",
  205. data: trends_data,
  206. type: 'line',
  207. height: 250,
  208. colors: ['blue'],
  209. isSeries: 1,
  210. lineOptions: {
  211. hideDots: 1,
  212. heatline: 1,
  213. },
  214. axisOptions: {
  215. xAxisMode: 'tick',
  216. yAxisMode: 'span',
  217. xIsSeries: 1
  218. }
  219. };
  220. new Chart("#chart-trends", plotChartArgs);
  221. Array.prototype.slice.call(
  222. document.querySelectorAll('.chart-plot-buttons button')
  223. ).map(el => {
  224. el.addEventListener('click', (e) => {
  225. let btn = e.target;
  226. let type = btn.getAttribute('data-type');
  227. let config = {};
  228. config[type] = 1;
  229. if(['regionFill', 'heatline'].includes(type)) {
  230. config.hideDots = 1;
  231. }
  232. // plotChartArgs.init = false;
  233. plotChartArgs.lineOptions = config;
  234. new Chart("#chart-trends", plotChartArgs);
  235. Array.prototype.slice.call(
  236. btn.parentNode.querySelectorAll('button')).map(el => {
  237. el.classList.remove('active');
  238. });
  239. btn.classList.add('active');
  240. });
  241. });
  242. // Update values chart
  243. // ================================================================================
  244. let update_data_all_labels = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon", "Tue",
  245. "Wed", "Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
  246. "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon"];
  247. let update_data_all_values = Array.from({length: 30}, () => Math.floor(Math.random() * 75 - 15));
  248. // We're gonna be shuffling this
  249. let update_data_all_indices = update_data_all_labels.map((d,i) => i);
  250. let get_update_data = (source_array, length=10) => {
  251. let indices = update_data_all_indices.slice(0, length);
  252. return indices.map((index) => source_array[index]);
  253. };
  254. let update_data = {
  255. labels: get_update_data(update_data_all_labels),
  256. datasets: [{
  257. "values": get_update_data(update_data_all_values)
  258. }],
  259. "specific_values": [
  260. {
  261. name: "Altitude",
  262. // name: "A very long text",
  263. line_type: "dashed",
  264. value: 38
  265. },
  266. ]
  267. };
  268. let update_chart = new Chart("#chart-update", {
  269. data: update_data,
  270. type: 'line',
  271. height: 250,
  272. colors: ['red'],
  273. isSeries: 1,
  274. lineOptions: {
  275. // hideLine: 1,
  276. regionFill: 1
  277. },
  278. });
  279. let chart_update_buttons = document.querySelector('.chart-update-buttons');
  280. chart_update_buttons.querySelector('[data-update="random"]').addEventListener("click", (e) => {
  281. shuffle(update_data_all_indices);
  282. let data = {
  283. labels: update_data_all_labels.slice(0, 10),
  284. datasets: [{values: get_update_data(update_data_all_values)}],
  285. }
  286. update_chart.update(data);
  287. });
  288. chart_update_buttons.querySelector('[data-update="add"]').addEventListener("click", (e) => {
  289. let index = update_chart.state.datasetLength; // last index to add
  290. if(index >= update_data_all_indices.length) return;
  291. update_chart.addDataPoint(
  292. update_data_all_labels[index], [update_data_all_values[index]]
  293. );
  294. });
  295. chart_update_buttons.querySelector('[data-update="remove"]').addEventListener("click", (e) => {
  296. update_chart.removeDataPoint();
  297. });
  298. // Event chart
  299. // ================================================================================
  300. let moon_names = ["Ganymede", "Callisto", "Io", "Europa"];
  301. let masses = [14819000, 10759000, 8931900, 4800000];
  302. let distances = [1070.412, 1882.709, 421.700, 671.034];
  303. let diameters = [5262.4, 4820.6, 3637.4, 3121.6];
  304. let jupiter_moons = {
  305. 'Ganymede': {
  306. mass: '14819000 x 10^16 kg',
  307. 'semi-major-axis': '1070412 km',
  308. 'diameter': '5262.4 km'
  309. },
  310. 'Callisto': {
  311. mass: '10759000 x 10^16 kg',
  312. 'semi-major-axis': '1882709 km',
  313. 'diameter': '4820.6 km'
  314. },
  315. 'Io': {
  316. mass: '8931900 x 10^16 kg',
  317. 'semi-major-axis': '421700 km',
  318. 'diameter': '3637.4 km'
  319. },
  320. 'Europa': {
  321. mass: '4800000 x 10^16 kg',
  322. 'semi-major-axis': '671034 km',
  323. 'diameter': '3121.6 km'
  324. },
  325. };
  326. let events_data = {
  327. labels: ["Ganymede", "Callisto", "Io", "Europa"],
  328. datasets: [
  329. {
  330. "values": distances,
  331. "formatted": distances.map(d => d*1000 + " km")
  332. }
  333. ]
  334. };
  335. let events_chart = new Chart("#chart-events", {
  336. title: "Jupiter's Moons: Semi-major Axis (1000 km)",
  337. data: events_data,
  338. type: 'bar',
  339. height: 250,
  340. colors: ['grey'],
  341. isNavigable: 1,
  342. });
  343. let data_div = document.querySelector('.chart-events-data');
  344. events_chart.parent.addEventListener('data-select', (e) => {
  345. let name = moon_names[e.index];
  346. data_div.querySelector('.moon-name').innerHTML = name;
  347. data_div.querySelector('.semi-major-axis').innerHTML = distances[e.index] * 1000;
  348. data_div.querySelector('.mass').innerHTML = masses[e.index];
  349. data_div.querySelector('.diameter').innerHTML = diameters[e.index];
  350. data_div.querySelector('img').src = "./assets/img/" + name.toLowerCase() + ".jpg";
  351. });
  352. // Aggregation chart
  353. // ================================================================================
  354. // Heatmap
  355. // ================================================================================
  356. let heatmap_data = {};
  357. let current_date = new Date();
  358. let timestamp = current_date.getTime()/1000;
  359. timestamp = Math.floor(timestamp - (timestamp % 86400)).toFixed(1); // convert to midnight
  360. for (var i = 0; i< 375; i++) {
  361. heatmap_data[parseInt(timestamp)] = Math.floor(Math.random() * 5);
  362. timestamp = Math.floor(timestamp - 86400).toFixed(1);
  363. }
  364. new Chart("#chart-heatmap", {
  365. data: heatmap_data,
  366. type: 'heatmap',
  367. legend_scale: [0, 1, 2, 4, 5],
  368. height: 115,
  369. discrete_domains: 1
  370. });
  371. Array.prototype.slice.call(
  372. document.querySelectorAll('.heatmap-mode-buttons button')
  373. ).map(el => {
  374. el.addEventListener('click', (e) => {
  375. let btn = e.target;
  376. let mode = btn.getAttribute('data-mode');
  377. let discrete_domains = 0;
  378. if(mode === 'discrete') {
  379. discrete_domains = 1;
  380. }
  381. let colors = [];
  382. let colors_mode = document
  383. .querySelector('.heatmap-color-buttons .active')
  384. .getAttribute('data-color');
  385. if(colors_mode === 'halloween') {
  386. colors = ['#ebedf0', '#fdf436', '#ffc700', '#ff9100', '#06001c'];
  387. }
  388. new Chart("#chart-heatmap", {
  389. data: heatmap_data,
  390. type: 'heatmap',
  391. legend_scale: [0, 1, 2, 4, 5],
  392. height: 115,
  393. discrete_domains: discrete_domains,
  394. legend_colors: colors
  395. });
  396. Array.prototype.slice.call(
  397. btn.parentNode.querySelectorAll('button')).map(el => {
  398. el.classList.remove('active');
  399. });
  400. btn.classList.add('active');
  401. });
  402. });
  403. Array.prototype.slice.call(
  404. document.querySelectorAll('.heatmap-color-buttons button')
  405. ).map(el => {
  406. el.addEventListener('click', (e) => {
  407. let btn = e.target;
  408. let colors_mode = btn.getAttribute('data-color');
  409. let colors = [];
  410. if(colors_mode === 'halloween') {
  411. colors = ['#ebedf0', '#fdf436', '#ffc700', '#ff9100', '#06001c'];
  412. }
  413. let discrete_domains = 1;
  414. let view_mode = document
  415. .querySelector('.heatmap-mode-buttons .active')
  416. .getAttribute('data-mode');
  417. if(view_mode === 'continuous') {
  418. discrete_domains = 0;
  419. }
  420. new Chart("#chart-heatmap", {
  421. data: heatmap_data,
  422. type: 'heatmap',
  423. legend_scale: [0, 1, 2, 4, 5],
  424. height: 115,
  425. discrete_domains: discrete_domains,
  426. legend_colors: colors
  427. });
  428. Array.prototype.slice.call(
  429. btn.parentNode.querySelectorAll('button')).map(el => {
  430. el.classList.remove('active');
  431. });
  432. btn.classList.add('active');
  433. });
  434. });
  435. // Helpers
  436. // ================================================================================
  437. function shuffle(array) {
  438. // https://stackoverflow.com/a/2450976/6495043
  439. // Awesomeness: https://bost.ocks.org/mike/shuffle/
  440. var currentIndex = array.length, temporaryValue, randomIndex;
  441. // While there remain elements to shuffle...
  442. while (0 !== currentIndex) {
  443. // Pick a remaining element...
  444. randomIndex = Math.floor(Math.random() * currentIndex);
  445. currentIndex -= 1;
  446. // And swap it with the current element.
  447. temporaryValue = array[currentIndex];
  448. array[currentIndex] = array[randomIndex];
  449. array[randomIndex] = temporaryValue;
  450. }
  451. return array;
  452. }