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.
 
 
 

1075 lines
33 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. function insertAfter(newNode, referenceNode) {
  152. referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
  153. }
  154. // Playing around with dates
  155. var NO_OF_MILLIS = 1000;
  156. var SEC_IN_DAY = 86400;
  157. var MONTH_NAMES_SHORT = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
  158. function clone(date) {
  159. return new Date(date.getTime());
  160. }
  161. function timestampSec(date) {
  162. return date.getTime() / NO_OF_MILLIS;
  163. }
  164. function timestampToMidnight(timestamp) {
  165. var roundAhead = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  166. var midnightTs = Math.floor(timestamp - timestamp % SEC_IN_DAY);
  167. if (roundAhead) {
  168. return midnightTs + SEC_IN_DAY;
  169. }
  170. return midnightTs;
  171. }
  172. // export function getMonthsBetween(startDate, endDate) {}
  173. // mutates
  174. // mutates
  175. function addDays(date, numberOfDays) {
  176. date.setDate(date.getDate() + numberOfDays);
  177. }
  178. // Fixed 5-color theme,
  179. // More colors are difficult to parse visually
  180. var HEATMAP_COLORS_BLUE = ['#ebedf0', '#c0ddf9', '#73b3f3', '#3886e1', '#17459e'];
  181. var HEATMAP_COLORS_YELLOW = ['#ebedf0', '#fdf436', '#ffc700', '#ff9100', '#06001c'];
  182. // Universal constants
  183. /**
  184. * Returns the value of a number upto 2 decimal places.
  185. * @param {Number} d Any number
  186. */
  187. /**
  188. * Returns whether or not two given arrays are equal.
  189. * @param {Array} arr1 First array
  190. * @param {Array} arr2 Second array
  191. */
  192. /**
  193. * Shuffles array in place. ES6 version
  194. * @param {Array} array An array containing the items.
  195. */
  196. function shuffle(array) {
  197. // Awesomeness: https://bost.ocks.org/mike/shuffle/
  198. // https://stackoverflow.com/a/2450976/6495043
  199. // https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array?noredirect=1&lq=1
  200. for (var i = array.length - 1; i > 0; i--) {
  201. var j = Math.floor(Math.random() * (i + 1));
  202. var _ref = [array[j], array[i]];
  203. array[i] = _ref[0];
  204. array[j] = _ref[1];
  205. }
  206. return array;
  207. }
  208. /**
  209. * Fill an array with extra points
  210. * @param {Array} array Array
  211. * @param {Number} count number of filler elements
  212. * @param {Object} element element to fill with
  213. * @param {Boolean} start fill at start?
  214. */
  215. /**
  216. * Returns pixel width of string.
  217. * @param {String} string
  218. * @param {Number} charWidth Width of single char in pixels
  219. */
  220. // https://stackoverflow.com/a/29325222
  221. function getRandomBias(min, max, bias, influence) {
  222. var range = max - min;
  223. var biasValue = range * bias + min;
  224. var rnd = Math.random() * range + min,
  225. // random in range
  226. mix = Math.random() * influence; // random mixer
  227. return rnd * (1 - mix) + biasValue * mix; // mix full range and bias
  228. }
  229. function toTitleCase(str) {
  230. return str.replace(/\w*/g, function (txt) {
  231. return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
  232. });
  233. }
  234. // Composite Chart
  235. // ================================================================================
  236. var reportCountList = [152, 222, 199, 287, 534, 709, 1179, 1256, 1632, 1856, 1850];
  237. var lineCompositeData = {
  238. labels: ["2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017"],
  239. yMarkers: [{
  240. label: "Average 100 reports/month",
  241. value: 1200,
  242. options: { labelPos: 'left' }
  243. }],
  244. datasets: [{
  245. "name": "Events",
  246. "values": reportCountList
  247. }]
  248. };
  249. 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]];
  250. 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]];
  251. var fireballOver25 = [
  252. // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  253. [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]];
  254. var barCompositeData = {
  255. labels: MONTH_NAMES_SHORT,
  256. datasets: [{
  257. name: "Over 25 reports",
  258. values: fireballOver25[9]
  259. }, {
  260. name: "5 to 25 reports",
  261. values: fireball_5_25[9]
  262. }, {
  263. name: "2 to 5 reports",
  264. values: fireball_2_5[9]
  265. }]
  266. };
  267. // Demo Chart multitype Chart
  268. // ================================================================================
  269. var typeData = {
  270. labels: ["12am-3am", "3am-6am", "6am-9am", "9am-12pm", "12pm-3pm", "3pm-6pm", "6pm-9pm", "9pm-12am"],
  271. yMarkers: [{
  272. label: "Marker",
  273. value: 43,
  274. options: { labelPos: 'left'
  275. // type: 'dashed'
  276. } }],
  277. yRegions: [{
  278. label: "Region",
  279. start: -10,
  280. end: 50,
  281. options: { labelPos: 'right' }
  282. }],
  283. datasets: [{
  284. name: "Some Data",
  285. values: [18, 40, 30, 35, 8, 52, 17, -4],
  286. axisPosition: 'right',
  287. chartType: 'bar'
  288. }, {
  289. name: "Another Set",
  290. values: [30, 50, -10, 15, 18, 32, 27, 14],
  291. axisPosition: 'right',
  292. chartType: 'bar'
  293. }, {
  294. name: "Yet Another",
  295. values: [15, 20, -3, -15, 58, 12, -17, 37],
  296. chartType: 'line'
  297. }]
  298. };
  299. 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"];
  300. var baseLength = 10;
  301. var fullLength = 30;
  302. var getRandom = function getRandom() {
  303. return Math.floor(getRandomBias(-40, 60, 0.8, 1));
  304. };
  305. var updateDataAllValues = Array.from({ length: fullLength }, getRandom);
  306. // We're gonna be shuffling this
  307. var updateDataAllIndices = updateDataAllLabels.map(function (d, i) {
  308. return i;
  309. });
  310. var getUpdateArray = function getUpdateArray(sourceArray) {
  311. var length = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10;
  312. var indices = updateDataAllIndices.slice(0, length);
  313. return indices.map(function (index) {
  314. return sourceArray[index];
  315. });
  316. };
  317. var currentLastIndex = baseLength;
  318. function getUpdateData() {
  319. shuffle(updateDataAllIndices);
  320. var value = getRandom();
  321. var start = getRandom();
  322. var end = getRandom();
  323. currentLastIndex = baseLength;
  324. return {
  325. labels: updateDataAllLabels.slice(0, baseLength),
  326. datasets: [{
  327. values: getUpdateArray(updateDataAllValues)
  328. }],
  329. yMarkers: [{
  330. label: "Altitude",
  331. value: value,
  332. type: 'dashed'
  333. }],
  334. yRegions: [{
  335. label: "Range",
  336. start: start,
  337. end: end
  338. }]
  339. };
  340. }
  341. function getAddUpdateData() {
  342. if (currentLastIndex >= fullLength) return;
  343. // TODO: Fix update on removal
  344. currentLastIndex++;
  345. var c = currentLastIndex - 1;
  346. return [updateDataAllLabels[c], [updateDataAllValues[c]]];
  347. // updateChart.addDataPoint(
  348. // updateDataAllLabels[index], [updateDataAllValues[index]]
  349. // );
  350. }
  351. var trendsData = {
  352. 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],
  353. datasets: [{
  354. 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]
  355. }]
  356. };
  357. var moonData = {
  358. names: ["Ganymede", "Callisto", "Io", "Europa"],
  359. masses: [14819000, 10759000, 8931900, 4800000],
  360. distances: [1070.412, 1882.709, 421.700, 671.034],
  361. diameters: [5262.4, 4820.6, 3637.4, 3121.6]
  362. };
  363. var eventsData = {
  364. labels: ["Ganymede", "Callisto", "Io", "Europa"],
  365. datasets: [{
  366. "values": moonData.distances,
  367. "formatted": moonData.distances.map(function (d) {
  368. return d * 1000 + " km";
  369. })
  370. }]
  371. };
  372. // const jupiterMoons = {
  373. // 'Ganymede': {
  374. // mass: '14819000 x 10^16 kg',
  375. // 'semi-major-axis': '1070412 km',
  376. // 'diameter': '5262.4 km'
  377. // },
  378. // 'Callisto': {
  379. // mass: '10759000 x 10^16 kg',
  380. // 'semi-major-axis': '1882709 km',
  381. // 'diameter': '4820.6 km'
  382. // },
  383. // 'Io': {
  384. // mass: '8931900 x 10^16 kg',
  385. // 'semi-major-axis': '421700 km',
  386. // 'diameter': '3637.4 km'
  387. // },
  388. // 'Europa': {
  389. // mass: '4800000 x 10^16 kg',
  390. // 'semi-major-axis': '671034 km',
  391. // 'diameter': '3121.6 km'
  392. // },
  393. // };
  394. // ================================================================================
  395. var today = new Date();
  396. var start = clone(today);
  397. addDays(start, 4);
  398. var end = clone(start);
  399. start.setFullYear(start.getFullYear() - 2);
  400. end.setFullYear(end.getFullYear() - 1);
  401. var dataPoints = {};
  402. var startTs = timestampSec(start);
  403. var endTs = timestampSec(end);
  404. startTs = timestampToMidnight(startTs);
  405. endTs = timestampToMidnight(endTs, true);
  406. while (startTs < endTs) {
  407. dataPoints[parseInt(startTs)] = Math.floor(getRandomBias(0, 5, 0.2, 1));
  408. startTs += SEC_IN_DAY;
  409. }
  410. var heatmapData = {
  411. dataPoints: dataPoints,
  412. start: start,
  413. end: end
  414. };
  415. var sampleData = {
  416. 0: {
  417. labels: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
  418. datasets: [{ values: [18, 40, 30, 35, 8, 52, 17, -4] }]
  419. },
  420. 1: {
  421. labels: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
  422. datasets: [{ name: "Dataset 1", values: [18, 40, 30, 35, 8, 52, 17, -4] }, { name: "Dataset 2", values: [30, 50, -10, 15, 18, 32, 27, 14] }]
  423. }
  424. };
  425. var lineComposite = {
  426. config: {
  427. title: "Fireball/Bolide Events - Yearly (reported)",
  428. data: lineCompositeData,
  429. type: "line",
  430. height: 190,
  431. colors: ["green"],
  432. isNavigable: 1,
  433. valuesOverPoints: 1,
  434. lineOptions: {
  435. dotSize: 8
  436. }
  437. }
  438. };
  439. var barComposite = {
  440. config: {
  441. data: barCompositeData,
  442. type: "bar",
  443. height: 210,
  444. colors: ["violet", "light-blue", "#46a9f9"],
  445. valuesOverPoints: 1,
  446. axisOptions: {
  447. xAxisMode: "tick"
  448. },
  449. barOptions: {
  450. stacked: 1
  451. }
  452. }
  453. };
  454. var demoSections = [{
  455. title: "Create a Chart",
  456. name: "demo-main",
  457. contentBlocks: [{
  458. type: "code",
  459. lang: "html",
  460. content: ' &lt!--HTML--&gt;\n &lt;figure id="frost-chart"&gt;&lt;/figure&gt;'
  461. }, {
  462. type: "code",
  463. lang: "javascript",
  464. 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();'
  465. }, {
  466. type: "demo",
  467. config: {
  468. title: "My Awesome Chart",
  469. data: typeData,
  470. type: "axis-mixed",
  471. height: 300,
  472. colors: ["purple", "magenta", "light-blue"],
  473. maxSlices: 10,
  474. tooltipOptions: {
  475. formatTooltipX: function formatTooltipX(d) {
  476. return (d + '').toUpperCase();
  477. },
  478. formatTooltipY: function formatTooltipY(d) {
  479. return d + ' pts';
  480. }
  481. }
  482. },
  483. options: [{
  484. name: "type",
  485. path: ["type"],
  486. type: "string",
  487. states: {
  488. "Mixed": 'axis-mixed',
  489. "Line": 'line',
  490. "Bar": 'bar',
  491. "Pie Chart": 'pie',
  492. "Percentage Chart": 'percentage'
  493. },
  494. activeState: "Mixed"
  495. }],
  496. actions: [{ name: "Export ...", fn: "export", args: [] }]
  497. }]
  498. }, {
  499. title: "Update Values",
  500. name: "updates-chart",
  501. contentBlocks: [{
  502. type: "demo",
  503. config: {
  504. data: getUpdateData(),
  505. type: 'line',
  506. height: 300,
  507. colors: ['#ff6c03'],
  508. lineOptions: {
  509. regionFill: 1
  510. }
  511. },
  512. actions: [{
  513. name: "Random Data",
  514. fn: "update",
  515. args: [getUpdateData()]
  516. }, {
  517. name: "Add Value",
  518. fn: "addDataPoint",
  519. args: getAddUpdateData()
  520. }, {
  521. name: "Remove Value",
  522. fn: "removeDataPoint",
  523. args: []
  524. }, {
  525. name: "Export ...",
  526. fn: "export",
  527. args: []
  528. }]
  529. }]
  530. }, {
  531. title: "Plot Trends",
  532. name: "trends-plot",
  533. contentBlocks: [{
  534. type: "demo",
  535. config: {
  536. title: "Mean Total Sunspot Count - Yearly",
  537. data: trendsData,
  538. type: 'line',
  539. height: 300,
  540. colors: ['#238e38'],
  541. axisOptions: {
  542. xAxisMode: 'tick',
  543. yAxisMode: 'span',
  544. xIsSeries: 1
  545. }
  546. },
  547. options: [{
  548. name: "lineOptions",
  549. path: ["lineOptions"],
  550. type: "map",
  551. mapKeys: ['hideLine', 'hideDots', 'heatline', 'regionFill'],
  552. states: {
  553. "Line": [0, 1, 0, 0],
  554. "Dots": [1, 0, 0, 0],
  555. "HeatLine": [0, 1, 1, 0],
  556. "Region": [0, 1, 0, 1]
  557. },
  558. activeState: "HeatLine"
  559. }],
  560. actions: [{ name: "Export ...", fn: "export", args: [] }]
  561. }]
  562. }, {
  563. title: "Listen to state change",
  564. name: "state-change",
  565. contentBlocks: [{
  566. type: "demo",
  567. config: {
  568. title: "Jupiter's Moons: Semi-major Axis (1000 km)",
  569. data: eventsData,
  570. type: 'bar',
  571. height: 330,
  572. colors: ['grey'],
  573. isNavigable: 1
  574. },
  575. sideContent: '<div class="image-container border">\n <img class="moon-image" src="./assets/img/europa.jpg">\n </div>\n <div class="content-data mt1">\n <h6 class="moon-name">Europa</h6>\n <p>Semi-major-axis: <span class="semi-major-axis">671034</span> km</p>\n <p>Mass: <span class="mass">4800000</span> x 10^16 kg</p>\n <p>Diameter: <span class="diameter">3121.6</span> km</p>\n </div>',
  576. postSetup: function postSetup(chart, figure, row) {
  577. chart.parent.addEventListener('data-select', function (e) {
  578. var i = e.index;
  579. var name = moonData.names[i];
  580. row.querySelector('.moon-name').innerHTML = name;
  581. row.querySelector('.semi-major-axis').innerHTML = moonData.distances[i] * 1000;
  582. row.querySelector('.mass').innerHTML = moonData.masses[i];
  583. row.querySelector('.diameter').innerHTML = moonData.diameters[i];
  584. row.querySelector('img').src = "./assets/img/" + name.toLowerCase() + ".jpg";
  585. });
  586. }
  587. }, {
  588. type: "code",
  589. lang: "javascript",
  590. content: ' ...\n isNavigable: 1, // Navigate across data points; default 0\n ...\n\n chart.parent.addEventListener(\'data-select\', (e) => {\n update_moon_data(e.index); // e contains index and value of current datapoint\n });'
  591. }]
  592. }, {
  593. title: "And a Month-wise Heatmap",
  594. name: "heatmap",
  595. contentBlocks: [{
  596. type: "demo",
  597. config: {
  598. title: "Monthly Distribution",
  599. data: heatmapData,
  600. type: 'heatmap',
  601. discreteDomains: 1,
  602. countLabel: 'Level',
  603. colors: HEATMAP_COLORS_BLUE,
  604. legendScale: [0, 1, 2, 4, 5]
  605. },
  606. options: [{
  607. name: "Discrete domains",
  608. path: ["discreteDomains"],
  609. type: 'boolean',
  610. // boolNames: ["Continuous", "Discrete"],
  611. states: { "Discrete": 1, "Continuous": 0 }
  612. }, {
  613. name: "Colors",
  614. path: ["colors"],
  615. type: "object",
  616. states: {
  617. "Green (Default)": [],
  618. "Blue": HEATMAP_COLORS_BLUE,
  619. "GitHub's Halloween": HEATMAP_COLORS_YELLOW
  620. }
  621. }],
  622. actions: [{ name: "Export ...", fn: "export", args: [] }]
  623. }, {
  624. type: "code",
  625. lang: "javascript",
  626. 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 });'
  627. }]
  628. }, {
  629. title: "Demo",
  630. name: "codepen",
  631. contentBlocks: [{
  632. type: "custom",
  633. 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>'
  634. }]
  635. }, {
  636. title: "Available Options",
  637. name: "options",
  638. contentBlocks: [{
  639. type: "code",
  640. lang: "javascript",
  641. 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 '
  642. }]
  643. }, {
  644. title: "Install",
  645. name: "installation",
  646. 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:' }, {
  647. type: "code",
  648. lang: "javascript",
  649. content: ' new Chart(); // ES6 module\n // or\n new frappe.Chart(); // Browser'
  650. }, { type: "text", content: '... or include it directly in your HTML' }, {
  651. type: "code",
  652. lang: "html",
  653. content: ' &lt;script src="https://unpkg.com/frappe-charts@1.1.0"&gt;&lt;/script&gt;'
  654. }]
  655. }];
  656. var docSections = [{
  657. name: "start",
  658. contentBlocks: [{
  659. type: "text",
  660. content: "A chart is generally a 2D rendition of data. For example, f\n\t\t\t\t\tor a set of values across items, the data could look like:"
  661. }, {
  662. type: "code",
  663. content: " data = {\n labels: [\"Sun\", \"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"],\n datasets: [\n\t { values: [18, 40, 30, 35, 8, 52, 17, -4] }\n ]\n }"
  664. }, {
  665. type: "text",
  666. content: "Plug that in with a type 'bar', a color and height:"
  667. }, {
  668. type: "code",
  669. content: " new frappe.Chart( \"#chart\", {\n data: data,\n type: 'bar',\n height: 140,\n colors: ['red']\n });"
  670. }, {
  671. type: "demo",
  672. config: {
  673. data: sampleData[0],
  674. type: 'line',
  675. height: 140,
  676. colors: ['red']
  677. }
  678. }, {
  679. type: "text",
  680. content: "Similar is a 'line' chart:"
  681. }, {
  682. type: "code",
  683. content: " ...\n type: 'line',\n ..."
  684. }, {
  685. type: "demo",
  686. config: {
  687. data: sampleData[0],
  688. type: 'bar',
  689. height: 140,
  690. colors: ['blue']
  691. }
  692. }]
  693. }, {
  694. title: "Adding more datasets",
  695. name: "multi-dataset",
  696. contentBlocks: [{
  697. type: "text",
  698. content: "Having more datasets, as in an axis chart, every dataset is represented individually."
  699. }, {
  700. type: "demo",
  701. config: {
  702. data: sampleData[1],
  703. type: 'line',
  704. height: 200,
  705. colors: ['yellow', 'light-green']
  706. }
  707. }]
  708. }];
  709. 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; }; }();
  710. 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); } }
  711. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  712. var docsBuilder = function () {
  713. function docsBuilder(LIB_OBJ) {
  714. _classCallCheck(this, docsBuilder);
  715. this.LIB_OBJ = LIB_OBJ;
  716. }
  717. _createClass(docsBuilder, [{
  718. key: 'makeSection',
  719. value: function makeSection(parent, sys) {
  720. return new docSection(this.LIB_OBJ, parent, sys);
  721. }
  722. }]);
  723. return docsBuilder;
  724. }();
  725. var docSection = function () {
  726. function docSection(LIB_OBJ, parent, sys) {
  727. _classCallCheck(this, docSection);
  728. this.LIB_OBJ = LIB_OBJ;
  729. this.parent = parent; // should be preferably a section
  730. this.sys = sys;
  731. this.blockMap = {};
  732. this.demos = [];
  733. this.make();
  734. }
  735. _createClass(docSection, [{
  736. key: 'make',
  737. value: function make() {
  738. var _this = this;
  739. // const section = document.querySelector(this.parent);
  740. var s = this.sys;
  741. if (s.title) {
  742. $.create('h6', { inside: this.parent, innerHTML: s.title });
  743. }
  744. s.contentBlocks.forEach(function (blockConf, index) {
  745. _this.blockMap[index] = _this.getBlock(blockConf);
  746. });
  747. }
  748. }, {
  749. key: 'getBlock',
  750. value: function getBlock(blockConf) {
  751. var fnName = 'get' + toTitleCase(blockConf.type);
  752. if (this[fnName]) {
  753. return this[fnName](blockConf);
  754. } else {
  755. throw new Error('Unknown section block type \'' + blockConf.type + '\'.');
  756. }
  757. }
  758. }, {
  759. key: 'getText',
  760. value: function getText(blockConf) {
  761. return $.create('p', {
  762. inside: this.parent,
  763. innerHTML: blockConf.content
  764. });
  765. }
  766. }, {
  767. key: 'getCode',
  768. value: function getCode(blockConf) {
  769. var pre = $.create('pre', { inside: this.parent });
  770. var lang = blockConf.lang || 'javascript';
  771. var code = $.create('code', {
  772. inside: pre,
  773. className: 'hljs ' + lang,
  774. innerHTML: blockConf.content
  775. });
  776. }
  777. }, {
  778. key: 'getCustom',
  779. value: function getCustom(blockConf) {
  780. this.parent.innerHTML += blockConf.html;
  781. }
  782. }, {
  783. key: 'getDemo',
  784. value: function getDemo(blockConf) {
  785. var bc = blockConf;
  786. var args = bc.config;
  787. var figure = void 0,
  788. row = void 0;
  789. if (!bc.sideContent) {
  790. figure = $.create('figure', { inside: this.parent });
  791. } else {
  792. row = $.create('div', {
  793. inside: this.parent,
  794. className: "row",
  795. innerHTML: '<div class="col-sm-8"></div>\n\t\t\t\t\t<div class="col-sm-4"></div>'
  796. });
  797. figure = $.create('figure', { inside: row.querySelector('.col-sm-8') });
  798. row.querySelector('.col-sm-4').innerHTML += bc.sideContent;
  799. }
  800. var libObj = new this.LIB_OBJ(figure, args);
  801. var demoIndex = this.demos.length;
  802. this.demos.push(libObj);
  803. if (bc.postSetup) {
  804. bc.postSetup(this.demos[demoIndex], figure, row);
  805. }
  806. this.getDemoOptions(demoIndex, bc.options, args, figure);
  807. this.getDemoActions(demoIndex, bc.actions, args);
  808. }
  809. }, {
  810. key: 'getDemoOptions',
  811. value: function getDemoOptions(demoIndex) {
  812. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  813. var _this2 = this;
  814. var args = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  815. var figure = arguments[3];
  816. options.forEach(function (o) {
  817. var btnGroup = $.create('div', {
  818. inside: _this2.parent,
  819. className: 'btn-group ' + o.name
  820. });
  821. var mapKeys = o.mapKeys;
  822. if (o.type === "map") {
  823. args[o.path[0]] = {};
  824. }
  825. Object.keys(o.states).forEach(function (key) {
  826. var state = o.states[key];
  827. var activeClass = key === o.activeState ? 'active' : '';
  828. var button = $.create('button', {
  829. inside: btnGroup,
  830. className: 'btn btn-sm btn-secondary ' + activeClass,
  831. innerHTML: key,
  832. onClick: function onClick(e) {
  833. // map
  834. if (o.type === "map") {
  835. mapKeys.forEach(function (attr, i) {
  836. args[o.path[0]][attr] = state[i];
  837. });
  838. } else {
  839. // boolean, string, number, object
  840. args[o.path[0]] = state;
  841. }
  842. _this2.demos[demoIndex] = new _this2.LIB_OBJ(figure, args);
  843. }
  844. });
  845. if (activeClass) {
  846. button.click();
  847. }
  848. });
  849. });
  850. }
  851. }, {
  852. key: 'getDemoActions',
  853. value: function getDemoActions(demoIndex) {
  854. var _this3 = this;
  855. var actions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  856. actions.forEach(function (o) {
  857. var args = o.args || [];
  858. $.create('button', {
  859. inside: _this3.parent,
  860. className: 'btn btn-action btn-sm btn-secondary',
  861. innerHTML: o.name,
  862. onClick: function onClick() {
  863. var _demos$demoIndex;
  864. (_demos$demoIndex = _this3.demos[demoIndex])[o.fn].apply(_demos$demoIndex, _toConsumableArray(args));
  865. }
  866. });
  867. });
  868. }
  869. }]);
  870. return docSection;
  871. }();
  872. var Chart = frappe.Chart; // eslint-disable-line no-undef
  873. var dbd = new docsBuilder(Chart);
  874. var currentElement = document.querySelector('header');
  875. var sections = void 0;
  876. if (window.location.pathname.split("/").pop().includes('index')) {
  877. var lineCompositeChart = new Chart("#line-composite-1", lineComposite.config);
  878. var barCompositeChart = new Chart("#bar-composite-1", barComposite.config);
  879. lineCompositeChart.parent.addEventListener('data-select', function (e) {
  880. var i = e.index;
  881. barCompositeChart.updateDatasets([fireballOver25[i], fireball_5_25[i], fireball_2_5[i]]);
  882. });
  883. sections = demoSections;
  884. } else {
  885. sections = docSections;
  886. }
  887. sections.forEach(function (sectionConf) {
  888. var sectionEl = $.create('section', { className: sectionConf.name || sectionConf.title });
  889. insertAfter(sectionEl, currentElement);
  890. currentElement = sectionEl;
  891. dbd.makeSection(sectionEl, sectionConf);
  892. });
  893. }());