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.
 
 
 

968 lines
30 KiB

  1. (function () {
  2. 'use strict';
  3. function __$styleInject(css, ref) {
  4. if ( ref === void 0 ) ref = {};
  5. var insertAt = ref.insertAt;
  6. if (!css || typeof document === 'undefined') { return; }
  7. var head = document.head || document.getElementsByTagName('head')[0];
  8. var style = document.createElement('style');
  9. style.type = 'text/css';
  10. if (insertAt === 'top') {
  11. if (head.firstChild) {
  12. head.insertBefore(style, head.firstChild);
  13. } else {
  14. head.appendChild(style);
  15. }
  16. } else {
  17. head.appendChild(style);
  18. }
  19. if (style.styleSheet) {
  20. style.styleSheet.cssText = css;
  21. } else {
  22. style.appendChild(document.createTextNode(css));
  23. }
  24. }
  25. var asyncGenerator = function () {
  26. function AwaitValue(value) {
  27. this.value = value;
  28. }
  29. function AsyncGenerator(gen) {
  30. var front, back;
  31. function send(key, arg) {
  32. return new Promise(function (resolve, reject) {
  33. var request = {
  34. key: key,
  35. arg: arg,
  36. resolve: resolve,
  37. reject: reject,
  38. next: null
  39. };
  40. if (back) {
  41. back = back.next = request;
  42. } else {
  43. front = back = request;
  44. resume(key, arg);
  45. }
  46. });
  47. }
  48. function resume(key, arg) {
  49. try {
  50. var result = gen[key](arg);
  51. var value = result.value;
  52. if (value instanceof AwaitValue) {
  53. Promise.resolve(value.value).then(function (arg) {
  54. resume("next", arg);
  55. }, function (arg) {
  56. resume("throw", arg);
  57. });
  58. } else {
  59. settle(result.done ? "return" : "normal", result.value);
  60. }
  61. } catch (err) {
  62. settle("throw", err);
  63. }
  64. }
  65. function settle(type, value) {
  66. switch (type) {
  67. case "return":
  68. front.resolve({
  69. value: value,
  70. done: true
  71. });
  72. break;
  73. case "throw":
  74. front.reject(value);
  75. break;
  76. default:
  77. front.resolve({
  78. value: value,
  79. done: false
  80. });
  81. break;
  82. }
  83. front = front.next;
  84. if (front) {
  85. resume(front.key, front.arg);
  86. } else {
  87. back = null;
  88. }
  89. }
  90. this._invoke = send;
  91. if (typeof gen.return !== "function") {
  92. this.return = undefined;
  93. }
  94. }
  95. if (typeof Symbol === "function" && Symbol.asyncIterator) {
  96. AsyncGenerator.prototype[Symbol.asyncIterator] = function () {
  97. return this;
  98. };
  99. }
  100. AsyncGenerator.prototype.next = function (arg) {
  101. return this._invoke("next", arg);
  102. };
  103. AsyncGenerator.prototype.throw = function (arg) {
  104. return this._invoke("throw", arg);
  105. };
  106. AsyncGenerator.prototype.return = function (arg) {
  107. return this._invoke("return", arg);
  108. };
  109. return {
  110. wrap: function (fn) {
  111. return function () {
  112. return new AsyncGenerator(fn.apply(this, arguments));
  113. };
  114. },
  115. await: function (value) {
  116. return new AwaitValue(value);
  117. }
  118. };
  119. }();
  120. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  121. function $(expr, con) {
  122. return typeof expr === "string" ? (con || document).querySelector(expr) : expr || null;
  123. }
  124. $.create = function (tag, o) {
  125. var element = document.createElement(tag);
  126. for (var i in o) {
  127. var val = o[i];
  128. if (i === "inside") {
  129. $(val).appendChild(element);
  130. } else if (i === "around") {
  131. var ref = $(val);
  132. ref.parentNode.insertBefore(element, ref);
  133. element.appendChild(ref);
  134. } else if (i === "onClick") {
  135. element.addEventListener('click', val);
  136. } else if (i === "styles") {
  137. if ((typeof val === "undefined" ? "undefined" : _typeof(val)) === "object") {
  138. Object.keys(val).map(function (prop) {
  139. element.style[prop] = val[prop];
  140. });
  141. }
  142. } else if (i in element) {
  143. element[i] = val;
  144. } else {
  145. element.setAttribute(i, val);
  146. }
  147. }
  148. return element;
  149. };
  150. // https://css-tricks.com/snippets/javascript/loop-queryselectorall-matches/
  151. // Fixed 5-color theme,
  152. // More colors are difficult to parse visually
  153. var HEATMAP_COLORS_BLUE = ['#ebedf0', '#c0ddf9', '#73b3f3', '#3886e1', '#17459e'];
  154. var HEATMAP_COLORS_YELLOW = ['#ebedf0', '#fdf436', '#ffc700', '#ff9100', '#06001c'];
  155. // Universal constants
  156. /**
  157. * Returns whether or not two given arrays are equal.
  158. * @param {Array} arr1 First array
  159. * @param {Array} arr2 Second array
  160. */
  161. /**
  162. * Shuffles array in place. ES6 version
  163. * @param {Array} array An array containing the items.
  164. */
  165. function shuffle(array) {
  166. // Awesomeness: https://bost.ocks.org/mike/shuffle/
  167. // https://stackoverflow.com/a/2450976/6495043
  168. // https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array?noredirect=1&lq=1
  169. for (var i = array.length - 1; i > 0; i--) {
  170. var j = Math.floor(Math.random() * (i + 1));
  171. var _ref = [array[j], array[i]];
  172. array[i] = _ref[0];
  173. array[j] = _ref[1];
  174. }
  175. return array;
  176. }
  177. /**
  178. * Fill an array with extra points
  179. * @param {Array} array Array
  180. * @param {Number} count number of filler elements
  181. * @param {Object} element element to fill with
  182. * @param {Boolean} start fill at start?
  183. */
  184. /**
  185. * Returns pixel width of string.
  186. * @param {String} string
  187. * @param {Number} charWidth Width of single char in pixels
  188. */
  189. // https://stackoverflow.com/a/29325222
  190. function getRandomBias(min, max, bias, influence) {
  191. var range = max - min;
  192. var biasValue = range * bias + min;
  193. var rnd = Math.random() * range + min,
  194. // random in range
  195. mix = Math.random() * influence; // random mixer
  196. return rnd * (1 - mix) + biasValue * mix; // mix full range and bias
  197. }
  198. function toTitleCase(str) {
  199. return str.replace(/\w*/g, function (txt) {
  200. return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
  201. });
  202. }
  203. // Playing around with dates
  204. var NO_OF_MILLIS = 1000;
  205. var SEC_IN_DAY = 86400;
  206. var MONTH_NAMES_SHORT = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
  207. // https://stackoverflow.com/a/11252167/6495043
  208. function clone(date) {
  209. return new Date(date.getTime());
  210. }
  211. function timestampSec(date) {
  212. return date.getTime() / NO_OF_MILLIS;
  213. }
  214. function timestampToMidnight(timestamp) {
  215. var roundAhead = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  216. var midnightTs = Math.floor(timestamp - timestamp % SEC_IN_DAY);
  217. if (roundAhead) {
  218. return midnightTs + SEC_IN_DAY;
  219. }
  220. return midnightTs;
  221. }
  222. // export function getMonthsBetween(startDate, endDate) {}
  223. // mutates
  224. // mutates
  225. function addDays(date, numberOfDays) {
  226. date.setDate(date.getDate() + numberOfDays);
  227. }
  228. var reportCountList = [152, 222, 199, 287, 534, 709, 1179, 1256, 1632, 1856, 1850];
  229. var lineCompositeData = {
  230. labels: ["2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017"],
  231. yMarkers: [{
  232. label: "Average 100 reports/month",
  233. value: 1200,
  234. options: { labelPos: 'left' }
  235. }],
  236. datasets: [{
  237. "name": "Events",
  238. "values": reportCountList
  239. }]
  240. };
  241. var fireball_5_25 = [[4, 0, 3, 1, 1, 2, 1, 1, 1, 0, 1, 1], [2, 3, 3, 2, 1, 3, 0, 1, 2, 7, 10, 4], [5, 6, 2, 4, 0, 1, 4, 3, 0, 2, 0, 1], [0, 2, 6, 2, 1, 1, 2, 3, 6, 3, 7, 8], [6, 8, 7, 7, 4, 5, 6, 5, 22, 12, 10, 11], [7, 10, 11, 7, 3, 2, 7, 7, 11, 15, 22, 20], [13, 16, 21, 18, 19, 17, 12, 17, 31, 28, 25, 29], [24, 14, 21, 14, 11, 15, 19, 21, 41, 22, 32, 18], [31, 20, 30, 22, 14, 17, 21, 35, 27, 50, 117, 24], [32, 24, 21, 27, 11, 27, 43, 37, 44, 40, 48, 32], [31, 38, 36, 26, 23, 23, 25, 29, 26, 47, 61, 50]];
  242. var fireball_2_5 = [[22, 6, 6, 9, 7, 8, 6, 14, 19, 10, 8, 20], [11, 13, 12, 8, 9, 11, 9, 13, 10, 22, 40, 24], [20, 13, 13, 19, 13, 10, 14, 13, 20, 18, 5, 9], [7, 13, 16, 19, 12, 11, 21, 27, 27, 24, 33, 33], [38, 25, 28, 22, 31, 21, 35, 42, 37, 32, 46, 53], [50, 33, 36, 34, 35, 28, 27, 52, 58, 59, 75, 69], [54, 67, 67, 45, 66, 51, 38, 64, 90, 113, 116, 87], [84, 52, 56, 51, 55, 46, 50, 87, 114, 83, 152, 93], [73, 58, 59, 63, 56, 51, 83, 140, 103, 115, 265, 89], [106, 95, 94, 71, 77, 75, 99, 136, 129, 154, 168, 156], [81, 102, 95, 72, 58, 91, 89, 122, 124, 135, 183, 171]];
  243. var fireballOver25 = [
  244. // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  245. [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0], [1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2], [3, 2, 1, 3, 2, 0, 2, 2, 2, 3, 0, 1], [2, 3, 5, 2, 1, 3, 0, 2, 3, 5, 1, 4], [7, 4, 6, 1, 9, 2, 2, 2, 20, 9, 4, 9], [5, 6, 1, 2, 5, 4, 5, 5, 16, 9, 14, 9], [5, 4, 7, 5, 1, 5, 3, 3, 5, 7, 22, 2], [5, 13, 11, 6, 1, 7, 9, 8, 14, 17, 16, 3], [8, 9, 8, 6, 4, 8, 5, 6, 14, 11, 21, 12]];
  246. var barCompositeData = {
  247. labels: MONTH_NAMES_SHORT,
  248. datasets: [{
  249. name: "Over 25 reports",
  250. values: fireballOver25[9]
  251. }, {
  252. name: "5 to 25 reports",
  253. values: fireball_5_25[9]
  254. }, {
  255. name: "2 to 5 reports",
  256. values: fireball_2_5[9]
  257. }]
  258. };
  259. // Demo Chart multitype Chart
  260. // ================================================================================
  261. var typeData = {
  262. labels: ["12am-3am", "3am-6am", "6am-9am", "9am-12pm", "12pm-3pm", "3pm-6pm", "6pm-9pm", "9pm-12am"],
  263. yMarkers: [{
  264. label: "Marker",
  265. value: 43,
  266. options: { labelPos: 'left'
  267. // type: 'dashed'
  268. } }],
  269. yRegions: [{
  270. label: "Region",
  271. start: -10,
  272. end: 50,
  273. options: { labelPos: 'right' }
  274. }],
  275. datasets: [{
  276. name: "Some Data",
  277. values: [18, 40, 30, 35, 8, 52, 17, -4],
  278. axisPosition: 'right',
  279. chartType: 'bar'
  280. }, {
  281. name: "Another Set",
  282. values: [30, 50, -10, 15, 18, 32, 27, 14],
  283. axisPosition: 'right',
  284. chartType: 'bar'
  285. }, {
  286. name: "Yet Another",
  287. values: [15, 20, -3, -15, 58, 12, -17, 37],
  288. chartType: 'line'
  289. }]
  290. };
  291. var trendsData = {
  292. labels: [1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016],
  293. datasets: [{
  294. values: [132.9, 150.0, 149.4, 148.0, 94.4, 97.6, 54.1, 49.2, 22.5, 18.4, 39.3, 131.0, 220.1, 218.9, 198.9, 162.4, 91.0, 60.5, 20.6, 14.8, 33.9, 123.0, 211.1, 191.8, 203.3, 133.0, 76.1, 44.9, 25.1, 11.6, 28.9, 88.3, 136.3, 173.9, 170.4, 163.6, 99.3, 65.3, 45.8, 24.7, 12.6, 4.2, 4.8, 24.9, 80.8, 84.5, 94.0, 113.3, 69.8, 39.8]
  295. }]
  296. };
  297. var moonData = {
  298. names: ["Ganymede", "Callisto", "Io", "Europa"],
  299. masses: [14819000, 10759000, 8931900, 4800000],
  300. distances: [1070.412, 1882.709, 421.700, 671.034],
  301. diameters: [5262.4, 4820.6, 3637.4, 3121.6]
  302. };
  303. // const jupiterMoons = {
  304. // 'Ganymede': {
  305. // mass: '14819000 x 10^16 kg',
  306. // 'semi-major-axis': '1070412 km',
  307. // 'diameter': '5262.4 km'
  308. // },
  309. // 'Callisto': {
  310. // mass: '10759000 x 10^16 kg',
  311. // 'semi-major-axis': '1882709 km',
  312. // 'diameter': '4820.6 km'
  313. // },
  314. // 'Io': {
  315. // mass: '8931900 x 10^16 kg',
  316. // 'semi-major-axis': '421700 km',
  317. // 'diameter': '3637.4 km'
  318. // },
  319. // 'Europa': {
  320. // mass: '4800000 x 10^16 kg',
  321. // 'semi-major-axis': '671034 km',
  322. // 'diameter': '3121.6 km'
  323. // },
  324. // };
  325. // ================================================================================
  326. var today = new Date();
  327. var start = clone(today);
  328. addDays(start, 4);
  329. var end = clone(start);
  330. start.setFullYear(start.getFullYear() - 2);
  331. end.setFullYear(end.getFullYear() - 1);
  332. var dataPoints = {};
  333. var startTs = timestampSec(start);
  334. var endTs = timestampSec(end);
  335. startTs = timestampToMidnight(startTs);
  336. endTs = timestampToMidnight(endTs, true);
  337. while (startTs < endTs) {
  338. dataPoints[parseInt(startTs)] = Math.floor(getRandomBias(0, 5, 0.2, 1));
  339. startTs += SEC_IN_DAY;
  340. }
  341. var heatmapData = {
  342. dataPoints: dataPoints,
  343. start: start,
  344. end: end
  345. };
  346. var dc = {
  347. lineComposite: {
  348. config: {
  349. title: "Fireball/Bolide Events - Yearly (reported)",
  350. data: lineCompositeData,
  351. type: "line",
  352. height: 190,
  353. colors: ["green"],
  354. isNavigable: 1,
  355. valuesOverPoints: 1,
  356. lineOptions: {
  357. dotSize: 8
  358. }
  359. }
  360. },
  361. barComposite: {
  362. config: {
  363. data: barCompositeData,
  364. type: "bar",
  365. height: 210,
  366. colors: ["violet", "light-blue", "#46a9f9"],
  367. valuesOverPoints: 1,
  368. axisOptions: {
  369. xAxisMode: "tick"
  370. },
  371. barOptions: {
  372. stacked: 1
  373. }
  374. }
  375. },
  376. demoMain: {
  377. title: "Create a Chart",
  378. contentBlocks: [{
  379. type: "code",
  380. lang: "html",
  381. content: ' &lt!--HTML--&gt;\n &lt;figure id="frost-chart"&gt;&lt;/figure&gt;'
  382. }, {
  383. type: "code",
  384. lang: "javascript",
  385. content: ' // Javascript\n let chart = new frappe.Chart( "#frost-chart", { // or DOM element\n data: {\n labels: ["12am-3am", "3am-6am", "6am-9am", "9am-12pm",\n "12pm-3pm", "3pm-6pm", "6pm-9pm", "9pm-12am"],\n\n datasets: [\n {\n name: "Some Data", chartType: \'bar\',\n values: [25, 40, 30, 35, 8, 52, 17, -4]\n },\n {\n name: "Another Set", chartType: \'bar\',\n values: [25, 50, -10, 15, 18, 32, 27, 14]\n },\n {\n name: "Yet Another", chartType: \'line\',\n values: [15, 20, -3, -15, 58, 12, -17, 37]\n }\n ],\n\n yMarkers: [{ label: "Marker", value: 70,\n options: { labelPos: \'left\' }}],\n yRegions: [{ label: "Region", start: -10, end: 50,\n options: { labelPos: \'right\' }}]\n },\n\n title: "My Awesome Chart",\n type: \'axis-mixed\', // or \'bar\', \'line\', \'pie\', \'percentage\'\n height: 300,\n colors: [\'purple\', \'#ffa3ef\', \'light-blue\'],\n\n tooltipOptions: {\n formatTooltipX: d => (d + \'\').toUpperCase(),\n formatTooltipY: d => d + \' pts\',\n }\n });\n\n chart.export();'
  386. }, {
  387. type: "demo",
  388. config: {
  389. title: "My Awesome Chart",
  390. data: typeData,
  391. type: "axis-mixed",
  392. height: 300,
  393. colors: ["purple", "magenta", "light-blue"],
  394. maxSlices: 10,
  395. tooltipOptions: {
  396. formatTooltipX: function formatTooltipX(d) {
  397. return (d + '').toUpperCase();
  398. },
  399. formatTooltipY: function formatTooltipY(d) {
  400. return d + ' pts';
  401. }
  402. }
  403. },
  404. options: [{
  405. name: "type",
  406. path: ["type"],
  407. type: "string",
  408. states: {
  409. "Mixed": 'axis-mixed',
  410. "Line": 'line',
  411. "Bar": 'bar',
  412. "Pie Chart": 'pie',
  413. "Percentage Chart": 'percentage'
  414. },
  415. activeState: "Mixed"
  416. }],
  417. actions: [{ name: "Export ...", fn: "export", args: [] }]
  418. }]
  419. },
  420. updateValues: {},
  421. trendsPlot: {
  422. title: "Plot Trends",
  423. contentBlocks: [{
  424. type: "demo",
  425. config: {
  426. title: "Mean Total Sunspot Count - Yearly",
  427. data: trendsData,
  428. type: 'line',
  429. height: 300,
  430. colors: ['#238e38'],
  431. axisOptions: {
  432. xAxisMode: 'tick',
  433. yAxisMode: 'span',
  434. xIsSeries: 1
  435. }
  436. },
  437. options: [{
  438. name: "lineOptions",
  439. path: ["lineOptions"],
  440. type: "map",
  441. mapKeys: ['hideLine', 'hideDots', 'heatline', 'regionFill'],
  442. states: {
  443. "Line": [0, 1, 0, 0],
  444. "Dots": [1, 0, 0, 0],
  445. "HeatLine": [0, 1, 1, 0],
  446. "Region": [0, 1, 0, 1]
  447. },
  448. activeState: "HeatLine"
  449. }],
  450. actions: [{ name: "Export ...", fn: "export", args: [] }]
  451. }]
  452. },
  453. stateChange: {},
  454. heatmap: {
  455. title: "And a Month-wise Heatmap",
  456. contentBlocks: [{
  457. type: "demo",
  458. config: {
  459. title: "Monthly Distribution",
  460. data: heatmapData,
  461. type: 'heatmap',
  462. discreteDomains: 1,
  463. countLabel: 'Level',
  464. colors: HEATMAP_COLORS_BLUE,
  465. legendScale: [0, 1, 2, 4, 5]
  466. },
  467. options: [{
  468. name: "Discrete domains",
  469. path: ["discreteDomains"],
  470. type: 'boolean',
  471. // boolNames: ["Continuous", "Discrete"],
  472. states: { "Discrete": 1, "Continuous": 0 }
  473. }, {
  474. name: "Colors",
  475. path: ["colors"],
  476. type: "object",
  477. states: {
  478. "Green (Default)": [],
  479. "Blue": HEATMAP_COLORS_BLUE,
  480. "GitHub's Halloween": HEATMAP_COLORS_YELLOW
  481. }
  482. }],
  483. actions: [{ name: "Export ...", fn: "export", args: [] }]
  484. }, {
  485. type: "code",
  486. lang: "javascript",
  487. content: ' let heatmap = new frappe.Chart("#heatmap", {\n type: \'heatmap\',\n title: "Monthly Distribution",\n data: {\n dataPoints: {\'1524064033\': 8, /* ... */},\n // object with timestamp-value pairs\n start: startDate\n end: endDate // Date objects\n },\n countLabel: \'Level\',\n discreteDomains: 0 // default: 1\n colors: [\'#ebedf0\', \'#c0ddf9\', \'#73b3f3\', \'#3886e1\', \'#17459e\'],\n // Set of five incremental colors,\n // preferably with a low-saturation color for zero data;\n // def: [\'#ebedf0\', \'#c6e48b\', \'#7bc96f\', \'#239a3b\', \'#196127\']\n });'
  488. }]
  489. },
  490. codePenDemo: {
  491. title: "Demo",
  492. contentBlocks: [{
  493. type: "custom",
  494. html: '<p data-height="299" data-theme-id="light" data-slug-hash="wjKBoq" data-default-tab="js,result"\n data-user="pratu16x7" data-embed-version="2" data-pen-title="Frappe Charts Demo" class="codepen">\n See the Pen <a href="https://codepen.io/pratu16x7/pen/wjKBoq/">Frappe Charts Demo</a>\n by Prateeksha Singh (<a href="https://codepen.io/pratu16x7">@pratu16x7</a>) on\n <a href="https://codepen.io">CodePen</a>.\n </p>'
  495. }]
  496. },
  497. optionsList: {
  498. title: "Available Options",
  499. contentBlocks: [{
  500. type: "code",
  501. lang: "javascript",
  502. content: '\n ...\n {\n data: {\n labels: [],\n datasets: [],\n yRegions: [],\n yMarkers: []\n }\n title: \'\',\n colors: [],\n height: 200,\n\n tooltipOptions: {\n formatTooltipX: d => (d + \'\').toUpperCase(),\n formatTooltipY: d => d + \' pts\',\n }\n\n // Axis charts\n isNavigable: 1, // default: 0\n valuesOverPoints: 1, // default: 0\n barOptions: {\n spaceRatio: 1 // default: 0.5\n stacked: 1 // default: 0\n }\n\n lineOptions: {\n dotSize: 6, // default: 4\n hideLine: 0, // default: 0\n hideDots: 1, // default: 0\n heatline: 1, // default: 0\n regionFill: 1 // default: 0\n }\n\n axisOptions: {\n yAxisMode: \'span\', // Axis lines, default\n xAxisMode: \'tick\', // No axis lines, only short ticks\n xIsSeries: 1 // Allow skipping x values for space\n // default: 0\n },\n\n // Pie/Percentage charts\n maxLegendPoints: 6, // default: 20\n maxSlices: 10, // default: 20\n\n // Percentage chart\n barOptions: {\n height: 15 // default: 20\n depth: 5 // default: 2\n }\n\n // Heatmap\n discreteDomains: 1, // default: 1\n }\n ...\n\n // Updating values\n chart.update(data);\n\n // Axis charts:\n chart.addDataPoint(label, valueFromEachDataset, index)\n chart.removeDataPoint(index)\n chart.updateDataset(datasetValues, index)\n\n // Exporting\n chart.export();\n\n // Unbind window-resize events\n chart.unbindWindowEvents();\n\n '
  503. }]
  504. },
  505. installation: {
  506. title: "Install",
  507. contentBlocks: [{ type: "text", content: 'Install via npm' }, { type: "code", lang: "console", content: ' npm install frappe-charts' }, { type: "text", content: 'And include it in your project' }, { type: "code", lang: "javascript", content: ' import { Chart } from "frappe-charts' }, { type: "text", content: 'Use as:' }, {
  508. type: "code",
  509. lang: "javascript",
  510. content: ' new Chart(); // ES6 module\n // or\n new frappe.Chart(); // Browser'
  511. }, { type: "text", content: '... or include it directly in your HTML' }, {
  512. type: "code",
  513. lang: "html",
  514. content: ' &lt;script src="https://unpkg.com/frappe-charts@1.1.0"&gt;&lt;/script&gt;'
  515. }]
  516. }
  517. };
  518. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  519. function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
  520. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  521. var docSectionBuilder = function () {
  522. function docSectionBuilder(LIB_OBJ) {
  523. _classCallCheck(this, docSectionBuilder);
  524. this.LIB_OBJ = LIB_OBJ;
  525. }
  526. _createClass(docSectionBuilder, [{
  527. key: 'setParent',
  528. value: function setParent(parent) {
  529. // this.parent = parent;
  530. this.section = parent;
  531. }
  532. }, {
  533. key: 'setSys',
  534. value: function setSys(sys) {
  535. this.sys = sys;
  536. this.blockMap = {};
  537. }
  538. }, {
  539. key: 'make',
  540. value: function make() {
  541. var _this = this;
  542. // const section = document.querySelector(this.section);
  543. var s = this.sys;
  544. $.create('h6', { inside: this.section, innerHTML: s.title });
  545. s.contentBlocks.forEach(function (blockConf, index) {
  546. _this.blockMap[index] = _this.getBlock(blockConf);
  547. });
  548. }
  549. }, {
  550. key: 'getBlock',
  551. value: function getBlock(blockConf) {
  552. var fnName = 'get' + toTitleCase(blockConf.type);
  553. if (this[fnName]) {
  554. return this[fnName](blockConf);
  555. } else {
  556. throw new Error('Unknown section block type \'' + blockConf.type + '\'.');
  557. }
  558. }
  559. }, {
  560. key: 'getText',
  561. value: function getText(blockConf) {
  562. return $.create('p', {
  563. inside: this.section,
  564. innerHTML: blockConf.content
  565. });
  566. }
  567. }, {
  568. key: 'getCode',
  569. value: function getCode(blockConf) {
  570. var pre = $.create('pre', { inside: this.section });
  571. var lang = blockConf.lang || 'javascript';
  572. var code = $.create('code', {
  573. inside: pre,
  574. className: 'hljs ' + lang,
  575. innerHTML: blockConf.content
  576. });
  577. }
  578. }, {
  579. key: 'getCustom',
  580. value: function getCustom(blockConf) {
  581. this.section.innerHTML += blockConf.html;
  582. }
  583. }, {
  584. key: 'getDemo',
  585. value: function getDemo(blockConf) {
  586. var args = blockConf.config;
  587. var figure = $.create('figure', { inside: this.section });
  588. this.libObj = new this.LIB_OBJ(figure, args);
  589. this.getDemoOptions(blockConf.options, args, figure);
  590. this.getDemoActions(blockConf.actions, args);
  591. }
  592. }, {
  593. key: 'getDemoOptions',
  594. value: function getDemoOptions(options, args, figure) {
  595. var _this2 = this;
  596. options.forEach(function (o) {
  597. var btnGroup = $.create('div', {
  598. inside: _this2.section,
  599. className: 'btn-group ' + o.name
  600. });
  601. var mapKeys = o.mapKeys;
  602. if (o.type === "map") {
  603. args[o.path[0]] = {};
  604. }
  605. Object.keys(o.states).forEach(function (key) {
  606. var state = o.states[key];
  607. var activeClass = key === o.activeState ? 'active' : '';
  608. var button = $.create('button', {
  609. inside: btnGroup,
  610. className: 'btn btn-sm btn-secondary ' + activeClass,
  611. innerHTML: key,
  612. onClick: function onClick(e) {
  613. // map
  614. if (o.type === "map") {
  615. mapKeys.forEach(function (attr, i) {
  616. args[o.path[0]][attr] = state[i];
  617. });
  618. } else {
  619. // boolean, string, number, object
  620. args[o.path[0]] = state;
  621. }
  622. _this2.libObj = new _this2.LIB_OBJ(figure, args);
  623. }
  624. });
  625. if (activeClass) {
  626. button.click();
  627. }
  628. });
  629. });
  630. }
  631. }, {
  632. key: 'getDemoActions',
  633. value: function getDemoActions(actions, args) {
  634. var _this3 = this;
  635. actions.forEach(function (o) {
  636. $.create('button', {
  637. inside: _this3.section,
  638. className: 'btn btn-sm btn-secondary',
  639. innerHTML: o.name,
  640. onClick: function onClick() {
  641. var _libObj;
  642. (_libObj = _this3.libObj)[o.fn].apply(_libObj, _toConsumableArray(o.args));
  643. }
  644. });
  645. });
  646. }
  647. }]);
  648. return docSectionBuilder;
  649. }();
  650. var Chart = frappe.Chart; // eslint-disable-line no-undef
  651. var dcb = new docSectionBuilder(Chart);
  652. var lineComposite = new Chart("#line-composite-1", dc.lineComposite.config);
  653. var barComposite = new Chart("#bar-composite-1", dc.barComposite.config);
  654. lineComposite.parent.addEventListener('data-select', function (e) {
  655. var i = e.index;
  656. barComposite.updateDatasets([fireballOver25[i], fireball_5_25[i], fireball_2_5[i]]);
  657. });
  658. var section = document.querySelector('.demo-main');
  659. dcb.setParent(section);
  660. dcb.setSys(dc.demoMain);
  661. dcb.make();
  662. // Update values chart
  663. // ================================================================================
  664. var updateDataAllLabels = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon"];
  665. var getRandom = function getRandom() {
  666. return Math.floor(getRandomBias(-40, 60, 0.8, 1));
  667. };
  668. var updateDataAllValues = Array.from({ length: 30 }, getRandom);
  669. // We're gonna be shuffling this
  670. var updateDataAllIndices = updateDataAllLabels.map(function (d, i) {
  671. return i;
  672. });
  673. var getUpdateData = function getUpdateData(source_array) {
  674. var length = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10;
  675. var indices = updateDataAllIndices.slice(0, length);
  676. return indices.map(function (index) {
  677. return source_array[index];
  678. });
  679. };
  680. var updateData = {
  681. labels: getUpdateData(updateDataAllLabels),
  682. datasets: [{
  683. "values": getUpdateData(updateDataAllValues)
  684. }],
  685. yMarkers: [{
  686. label: "Altitude",
  687. value: 25,
  688. type: 'dashed'
  689. }],
  690. yRegions: [{
  691. label: "Range",
  692. start: 10,
  693. end: 45
  694. }]
  695. };
  696. var updateChart = new Chart("#chart-update", {
  697. data: updateData,
  698. type: 'line',
  699. height: 300,
  700. colors: ['#ff6c03'],
  701. lineOptions: {
  702. // hideLine: 1,
  703. regionFill: 1
  704. }
  705. });
  706. var chartUpdateButtons = document.querySelector('.chart-update-buttons');
  707. chartUpdateButtons.querySelector('[data-update="random"]').addEventListener("click", function () {
  708. shuffle(updateDataAllIndices);
  709. var value = getRandom();
  710. var start = getRandom();
  711. var end = getRandom();
  712. var data = {
  713. labels: updateDataAllLabels.slice(0, 10),
  714. datasets: [{ values: getUpdateData(updateDataAllValues) }],
  715. yMarkers: [{
  716. label: "Altitude",
  717. value: value,
  718. type: 'dashed'
  719. }],
  720. yRegions: [{
  721. label: "Range",
  722. start: start,
  723. end: end
  724. }]
  725. };
  726. updateChart.update(data);
  727. });
  728. chartUpdateButtons.querySelector('[data-update="add"]').addEventListener("click", function () {
  729. var index = updateChart.state.datasetLength; // last index to add
  730. if (index >= updateDataAllIndices.length) return;
  731. updateChart.addDataPoint(updateDataAllLabels[index], [updateDataAllValues[index]]);
  732. });
  733. chartUpdateButtons.querySelector('[data-update="remove"]').addEventListener("click", function () {
  734. updateChart.removeDataPoint();
  735. });
  736. document.querySelector('.export-update').addEventListener('click', function () {
  737. updateChart.export();
  738. });
  739. // Trends Chart
  740. // ================================================================================
  741. section = document.querySelector('.trends-plot');
  742. dcb.setParent(section);
  743. dcb.setSys(dc.trendsPlot);
  744. dcb.make();
  745. var eventsData = {
  746. labels: ["Ganymede", "Callisto", "Io", "Europa"],
  747. datasets: [{
  748. "values": moonData.distances,
  749. "formatted": moonData.distances.map(function (d) {
  750. return d * 1000 + " km";
  751. })
  752. }]
  753. };
  754. var eventsChart = new Chart("#chart-events", {
  755. title: "Jupiter's Moons: Semi-major Axis (1000 km)",
  756. data: eventsData,
  757. type: 'bar',
  758. height: 330,
  759. colors: ['grey'],
  760. isNavigable: 1
  761. });
  762. var dataDiv = document.querySelector('.chart-events-data');
  763. eventsChart.parent.addEventListener('data-select', function (e) {
  764. var name = moonData.names[e.index];
  765. dataDiv.querySelector('.moon-name').innerHTML = name;
  766. dataDiv.querySelector('.semi-major-axis').innerHTML = moonData.distances[e.index] * 1000;
  767. dataDiv.querySelector('.mass').innerHTML = moonData.masses[e.index];
  768. dataDiv.querySelector('.diameter').innerHTML = moonData.diameters[e.index];
  769. dataDiv.querySelector('img').src = "./assets/img/" + name.toLowerCase() + ".jpg";
  770. });
  771. // Heatmap
  772. // ================================================================================
  773. section = document.querySelector('.heatmap');
  774. dcb.setParent(section);
  775. dcb.setSys(dc.heatmap);
  776. dcb.make();
  777. section = document.querySelector('.codepen');
  778. dcb.setParent(section);
  779. dcb.setSys(dc.codePenDemo);
  780. dcb.make();
  781. section = document.querySelector('.options');
  782. dcb.setParent(section);
  783. dcb.setSys(dc.optionsList);
  784. dcb.make();
  785. section = document.querySelector('.installation');
  786. dcb.setParent(section);
  787. dcb.setSys(dc.installation);
  788. dcb.make();
  789. }());