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.
 
 
 

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