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.
 
 
 

484 lines
13 KiB

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