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.
 
 
 

525 lines
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. // maxLegendPoints: 6,
  160. // maxSlices: 3,
  161. isNavigable: 1,
  162. barOptions: {
  163. stacked: 1
  164. },
  165. tooltipOptions: {
  166. formatTooltipX: d => (d + '').toUpperCase(),
  167. formatTooltipY: d => d + ' pts'
  168. }
  169. });
  170. Array.prototype.slice.call(
  171. document.querySelectorAll('.chart-type-buttons button')
  172. ).map(el => {
  173. el.addEventListener('click', (e) => {
  174. let btn = e.target;
  175. let type = btn.getAttribute('data-type');
  176. let newChart = type_chart.getDifferentChart(type);
  177. if(newChart){
  178. type_chart = newChart;
  179. }
  180. Array.prototype.slice.call(
  181. btn.parentNode.querySelectorAll('button')).map(el => {
  182. el.classList.remove('active');
  183. });
  184. btn.classList.add('active');
  185. });
  186. });
  187. // Trends Chart
  188. // ================================================================================
  189. let trends_data = {
  190. labels: [1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976,
  191. 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986,
  192. 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
  193. 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
  194. 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016] ,
  195. datasets: [
  196. {
  197. "values": [132.9, 150.0, 149.4, 148.0, 94.4, 97.6, 54.1, 49.2, 22.5, 18.4,
  198. 39.3, 131.0, 220.1, 218.9, 198.9, 162.4, 91.0, 60.5, 20.6, 14.8,
  199. 33.9, 123.0, 211.1, 191.8, 203.3, 133.0, 76.1, 44.9, 25.1, 11.6,
  200. 28.9, 88.3, 136.3, 173.9, 170.4, 163.6, 99.3, 65.3, 45.8, 24.7,
  201. 12.6, 4.2, 4.8, 24.9, 80.8, 84.5, 94.0, 113.3, 69.8, 39.8]
  202. }
  203. ]
  204. };
  205. let plotChartArgs = {
  206. title: "Mean Total Sunspot Count - Yearly",
  207. data: trends_data,
  208. type: 'line',
  209. height: 250,
  210. colors: ['blue'],
  211. isSeries: 1,
  212. lineOptions: {
  213. hideDots: 1,
  214. heatline: 1,
  215. },
  216. axisOptions: {
  217. xAxisMode: 'tick',
  218. yAxisMode: 'span',
  219. xIsSeries: 1
  220. }
  221. };
  222. new Chart("#chart-trends", plotChartArgs);
  223. Array.prototype.slice.call(
  224. document.querySelectorAll('.chart-plot-buttons button')
  225. ).map(el => {
  226. el.addEventListener('click', (e) => {
  227. let btn = e.target;
  228. let type = btn.getAttribute('data-type');
  229. let config = {};
  230. config[type] = 1;
  231. if(['regionFill', 'heatline'].includes(type)) {
  232. config.hideDots = 1;
  233. }
  234. // plotChartArgs.init = false;
  235. plotChartArgs.lineOptions = config;
  236. new Chart("#chart-trends", plotChartArgs);
  237. Array.prototype.slice.call(
  238. btn.parentNode.querySelectorAll('button')).map(el => {
  239. el.classList.remove('active');
  240. });
  241. btn.classList.add('active');
  242. });
  243. });
  244. // Update values chart
  245. // ================================================================================
  246. let update_data_all_labels = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon", "Tue",
  247. "Wed", "Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
  248. "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon"];
  249. let update_data_all_values = Array.from({length: 30}, () => Math.floor(Math.random() * 75 - 15));
  250. // We're gonna be shuffling this
  251. let update_data_all_indices = update_data_all_labels.map((d,i) => i);
  252. let get_update_data = (source_array, length=10) => {
  253. let indices = update_data_all_indices.slice(0, length);
  254. return indices.map((index) => source_array[index]);
  255. };
  256. let update_data = {
  257. labels: get_update_data(update_data_all_labels),
  258. datasets: [{
  259. "values": get_update_data(update_data_all_values)
  260. }],
  261. "specific_values": [
  262. {
  263. name: "Altitude",
  264. // name: "A very long text",
  265. line_type: "dashed",
  266. value: 38
  267. },
  268. ]
  269. };
  270. let update_chart = new Chart("#chart-update", {
  271. data: update_data,
  272. type: 'line',
  273. height: 250,
  274. colors: ['red'],
  275. isSeries: 1,
  276. lineOptions: {
  277. // hideLine: 1,
  278. regionFill: 1
  279. },
  280. });
  281. let chart_update_buttons = document.querySelector('.chart-update-buttons');
  282. chart_update_buttons.querySelector('[data-update="random"]').addEventListener("click", (e) => {
  283. shuffle(update_data_all_indices);
  284. let data = {
  285. labels: update_data_all_labels.slice(0, 10),
  286. datasets: [{values: get_update_data(update_data_all_values)}],
  287. }
  288. update_chart.update(data);
  289. });
  290. chart_update_buttons.querySelector('[data-update="add"]').addEventListener("click", (e) => {
  291. let index = update_chart.state.datasetLength; // last index to add
  292. if(index >= update_data_all_indices.length) return;
  293. update_chart.addDataPoint(
  294. update_data_all_labels[index], [update_data_all_values[index]]
  295. );
  296. });
  297. chart_update_buttons.querySelector('[data-update="remove"]').addEventListener("click", (e) => {
  298. update_chart.removeDataPoint();
  299. });
  300. // Event chart
  301. // ================================================================================
  302. let moon_names = ["Ganymede", "Callisto", "Io", "Europa"];
  303. let masses = [14819000, 10759000, 8931900, 4800000];
  304. let distances = [1070.412, 1882.709, 421.700, 671.034];
  305. let diameters = [5262.4, 4820.6, 3637.4, 3121.6];
  306. let jupiter_moons = {
  307. 'Ganymede': {
  308. mass: '14819000 x 10^16 kg',
  309. 'semi-major-axis': '1070412 km',
  310. 'diameter': '5262.4 km'
  311. },
  312. 'Callisto': {
  313. mass: '10759000 x 10^16 kg',
  314. 'semi-major-axis': '1882709 km',
  315. 'diameter': '4820.6 km'
  316. },
  317. 'Io': {
  318. mass: '8931900 x 10^16 kg',
  319. 'semi-major-axis': '421700 km',
  320. 'diameter': '3637.4 km'
  321. },
  322. 'Europa': {
  323. mass: '4800000 x 10^16 kg',
  324. 'semi-major-axis': '671034 km',
  325. 'diameter': '3121.6 km'
  326. },
  327. };
  328. let events_data = {
  329. labels: ["Ganymede", "Callisto", "Io", "Europa"],
  330. datasets: [
  331. {
  332. "values": distances,
  333. "formatted": distances.map(d => d*1000 + " km")
  334. }
  335. ]
  336. };
  337. let events_chart = new Chart("#chart-events", {
  338. title: "Jupiter's Moons: Semi-major Axis (1000 km)",
  339. data: events_data,
  340. type: 'bar',
  341. height: 250,
  342. colors: ['grey'],
  343. isNavigable: 1,
  344. });
  345. let data_div = document.querySelector('.chart-events-data');
  346. events_chart.parent.addEventListener('data-select', (e) => {
  347. let name = moon_names[e.index];
  348. data_div.querySelector('.moon-name').innerHTML = name;
  349. data_div.querySelector('.semi-major-axis').innerHTML = distances[e.index] * 1000;
  350. data_div.querySelector('.mass').innerHTML = masses[e.index];
  351. data_div.querySelector('.diameter').innerHTML = diameters[e.index];
  352. data_div.querySelector('img').src = "./assets/img/" + name.toLowerCase() + ".jpg";
  353. });
  354. // Aggregation chart
  355. // ================================================================================
  356. // Heatmap
  357. // ================================================================================
  358. let heatmap_data = {};
  359. let current_date = new Date();
  360. let timestamp = current_date.getTime()/1000;
  361. timestamp = Math.floor(timestamp - (timestamp % 86400)).toFixed(1); // convert to midnight
  362. for (var i = 0; i< 375; i++) {
  363. heatmap_data[parseInt(timestamp)] = Math.floor(Math.random() * 5);
  364. timestamp = Math.floor(timestamp - 86400).toFixed(1);
  365. }
  366. new Chart("#chart-heatmap", {
  367. data: heatmap_data,
  368. type: 'heatmap',
  369. legend_scale: [0, 1, 2, 4, 5],
  370. height: 115,
  371. discrete_domains: 1
  372. });
  373. Array.prototype.slice.call(
  374. document.querySelectorAll('.heatmap-mode-buttons button')
  375. ).map(el => {
  376. el.addEventListener('click', (e) => {
  377. let btn = e.target;
  378. let mode = btn.getAttribute('data-mode');
  379. let discrete_domains = 0;
  380. if(mode === 'discrete') {
  381. discrete_domains = 1;
  382. }
  383. let colors = [];
  384. let colors_mode = document
  385. .querySelector('.heatmap-color-buttons .active')
  386. .getAttribute('data-color');
  387. if(colors_mode === 'halloween') {
  388. colors = ['#ebedf0', '#fdf436', '#ffc700', '#ff9100', '#06001c'];
  389. }
  390. new Chart("#chart-heatmap", {
  391. data: heatmap_data,
  392. type: 'heatmap',
  393. legend_scale: [0, 1, 2, 4, 5],
  394. height: 115,
  395. discrete_domains: discrete_domains,
  396. legend_colors: colors
  397. });
  398. Array.prototype.slice.call(
  399. btn.parentNode.querySelectorAll('button')).map(el => {
  400. el.classList.remove('active');
  401. });
  402. btn.classList.add('active');
  403. });
  404. });
  405. Array.prototype.slice.call(
  406. document.querySelectorAll('.heatmap-color-buttons button')
  407. ).map(el => {
  408. el.addEventListener('click', (e) => {
  409. let btn = e.target;
  410. let colors_mode = btn.getAttribute('data-color');
  411. let colors = [];
  412. if(colors_mode === 'halloween') {
  413. colors = ['#ebedf0', '#fdf436', '#ffc700', '#ff9100', '#06001c'];
  414. }
  415. let discrete_domains = 1;
  416. let view_mode = document
  417. .querySelector('.heatmap-mode-buttons .active')
  418. .getAttribute('data-mode');
  419. if(view_mode === 'continuous') {
  420. discrete_domains = 0;
  421. }
  422. new Chart("#chart-heatmap", {
  423. data: heatmap_data,
  424. type: 'heatmap',
  425. legend_scale: [0, 1, 2, 4, 5],
  426. height: 115,
  427. discrete_domains: discrete_domains,
  428. legend_colors: colors
  429. });
  430. Array.prototype.slice.call(
  431. btn.parentNode.querySelectorAll('button')).map(el => {
  432. el.classList.remove('active');
  433. });
  434. btn.classList.add('active');
  435. });
  436. });
  437. // Helpers
  438. // ================================================================================
  439. function shuffle(array) {
  440. // https://stackoverflow.com/a/2450976/6495043
  441. // Awesomeness: https://bost.ocks.org/mike/shuffle/
  442. var currentIndex = array.length, temporaryValue, randomIndex;
  443. // While there remain elements to shuffle...
  444. while (0 !== currentIndex) {
  445. // Pick a remaining element...
  446. randomIndex = Math.floor(Math.random() * currentIndex);
  447. currentIndex -= 1;
  448. // And swap it with the current element.
  449. temporaryValue = array[currentIndex];
  450. array[currentIndex] = array[randomIndex];
  451. array[randomIndex] = temporaryValue;
  452. }
  453. return array;
  454. }