您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

575 行
14 KiB

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