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

3272 行
90 KiB

  1. 'use strict';
  2. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
  3. return typeof obj;
  4. } : function (obj) {
  5. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  6. };
  7. var asyncGenerator = function () {
  8. function AwaitValue(value) {
  9. this.value = value;
  10. }
  11. function AsyncGenerator(gen) {
  12. var front, back;
  13. function send(key, arg) {
  14. return new Promise(function (resolve, reject) {
  15. var request = {
  16. key: key,
  17. arg: arg,
  18. resolve: resolve,
  19. reject: reject,
  20. next: null
  21. };
  22. if (back) {
  23. back = back.next = request;
  24. } else {
  25. front = back = request;
  26. resume(key, arg);
  27. }
  28. });
  29. }
  30. function resume(key, arg) {
  31. try {
  32. var result = gen[key](arg);
  33. var value = result.value;
  34. if (value instanceof AwaitValue) {
  35. Promise.resolve(value.value).then(function (arg) {
  36. resume("next", arg);
  37. }, function (arg) {
  38. resume("throw", arg);
  39. });
  40. } else {
  41. settle(result.done ? "return" : "normal", result.value);
  42. }
  43. } catch (err) {
  44. settle("throw", err);
  45. }
  46. }
  47. function settle(type, value) {
  48. switch (type) {
  49. case "return":
  50. front.resolve({
  51. value: value,
  52. done: true
  53. });
  54. break;
  55. case "throw":
  56. front.reject(value);
  57. break;
  58. default:
  59. front.resolve({
  60. value: value,
  61. done: false
  62. });
  63. break;
  64. }
  65. front = front.next;
  66. if (front) {
  67. resume(front.key, front.arg);
  68. } else {
  69. back = null;
  70. }
  71. }
  72. this._invoke = send;
  73. if (typeof gen.return !== "function") {
  74. this.return = undefined;
  75. }
  76. }
  77. if (typeof Symbol === "function" && Symbol.asyncIterator) {
  78. AsyncGenerator.prototype[Symbol.asyncIterator] = function () {
  79. return this;
  80. };
  81. }
  82. AsyncGenerator.prototype.next = function (arg) {
  83. return this._invoke("next", arg);
  84. };
  85. AsyncGenerator.prototype.throw = function (arg) {
  86. return this._invoke("throw", arg);
  87. };
  88. AsyncGenerator.prototype.return = function (arg) {
  89. return this._invoke("return", arg);
  90. };
  91. return {
  92. wrap: function (fn) {
  93. return function () {
  94. return new AsyncGenerator(fn.apply(this, arguments));
  95. };
  96. },
  97. await: function (value) {
  98. return new AwaitValue(value);
  99. }
  100. };
  101. }();
  102. var classCallCheck = function (instance, Constructor) {
  103. if (!(instance instanceof Constructor)) {
  104. throw new TypeError("Cannot call a class as a function");
  105. }
  106. };
  107. var createClass = function () {
  108. function defineProperties(target, props) {
  109. for (var i = 0; i < props.length; i++) {
  110. var descriptor = props[i];
  111. descriptor.enumerable = descriptor.enumerable || false;
  112. descriptor.configurable = true;
  113. if ("value" in descriptor) descriptor.writable = true;
  114. Object.defineProperty(target, descriptor.key, descriptor);
  115. }
  116. }
  117. return function (Constructor, protoProps, staticProps) {
  118. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  119. if (staticProps) defineProperties(Constructor, staticProps);
  120. return Constructor;
  121. };
  122. }();
  123. var get = function get(object, property, receiver) {
  124. if (object === null) object = Function.prototype;
  125. var desc = Object.getOwnPropertyDescriptor(object, property);
  126. if (desc === undefined) {
  127. var parent = Object.getPrototypeOf(object);
  128. if (parent === null) {
  129. return undefined;
  130. } else {
  131. return get(parent, property, receiver);
  132. }
  133. } else if ("value" in desc) {
  134. return desc.value;
  135. } else {
  136. var getter = desc.get;
  137. if (getter === undefined) {
  138. return undefined;
  139. }
  140. return getter.call(receiver);
  141. }
  142. };
  143. var inherits = function (subClass, superClass) {
  144. if (typeof superClass !== "function" && superClass !== null) {
  145. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  146. }
  147. subClass.prototype = Object.create(superClass && superClass.prototype, {
  148. constructor: {
  149. value: subClass,
  150. enumerable: false,
  151. writable: true,
  152. configurable: true
  153. }
  154. });
  155. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  156. };
  157. var possibleConstructorReturn = function (self, call) {
  158. if (!self) {
  159. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  160. }
  161. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  162. };
  163. var slicedToArray = function () {
  164. function sliceIterator(arr, i) {
  165. var _arr = [];
  166. var _n = true;
  167. var _d = false;
  168. var _e = undefined;
  169. try {
  170. for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
  171. _arr.push(_s.value);
  172. if (i && _arr.length === i) break;
  173. }
  174. } catch (err) {
  175. _d = true;
  176. _e = err;
  177. } finally {
  178. try {
  179. if (!_n && _i["return"]) _i["return"]();
  180. } finally {
  181. if (_d) throw _e;
  182. }
  183. }
  184. return _arr;
  185. }
  186. return function (arr, i) {
  187. if (Array.isArray(arr)) {
  188. return arr;
  189. } else if (Symbol.iterator in Object(arr)) {
  190. return sliceIterator(arr, i);
  191. } else {
  192. throw new TypeError("Invalid attempt to destructure non-iterable instance");
  193. }
  194. };
  195. }();
  196. var toConsumableArray = function (arr) {
  197. if (Array.isArray(arr)) {
  198. for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
  199. return arr2;
  200. } else {
  201. return Array.from(arr);
  202. }
  203. };
  204. function $(expr, con) {
  205. return typeof expr === "string" ? (con || document).querySelector(expr) : expr || null;
  206. }
  207. var EASING = {
  208. ease: "0.25 0.1 0.25 1",
  209. linear: "0 0 1 1",
  210. // easein: "0.42 0 1 1",
  211. easein: "0.1 0.8 0.2 1",
  212. easeout: "0 0 0.58 1",
  213. easeinout: "0.42 0 0.58 1"
  214. };
  215. $.findNodeIndex = function (node) {
  216. var i = 0;
  217. while (node.previousSibling) {
  218. node = node.previousSibling;
  219. i++;
  220. }
  221. return i;
  222. };
  223. $.create = function (tag, o) {
  224. var element = document.createElement(tag);
  225. for (var i in o) {
  226. var val = o[i];
  227. if (i === "inside") {
  228. $(val).appendChild(element);
  229. } else if (i === "around") {
  230. var ref = $(val);
  231. ref.parentNode.insertBefore(element, ref);
  232. element.appendChild(ref);
  233. } else if (i === "styles") {
  234. if ((typeof val === "undefined" ? "undefined" : _typeof(val)) === "object") {
  235. Object.keys(val).map(function (prop) {
  236. element.style[prop] = val[prop];
  237. });
  238. }
  239. } else if (i in element) {
  240. element[i] = val;
  241. } else {
  242. element.setAttribute(i, val);
  243. }
  244. }
  245. return element;
  246. };
  247. $.createSVG = function (tag, o) {
  248. var element = document.createElementNS("http://www.w3.org/2000/svg", tag);
  249. for (var i in o) {
  250. var val = o[i];
  251. if (i === "inside") {
  252. $(val).appendChild(element);
  253. } else if (i === "around") {
  254. var ref = $(val);
  255. ref.parentNode.insertBefore(element, ref);
  256. element.appendChild(ref);
  257. } else {
  258. if (i === "className") {
  259. i = "class";
  260. }
  261. if (i === "innerHTML") {
  262. element['textContent'] = val;
  263. } else {
  264. element.setAttribute(i, val);
  265. }
  266. }
  267. }
  268. return element;
  269. };
  270. $.runSVGAnimation = function (svg_container, elements) {
  271. // let parent = elements[0][0]['unit'].parentNode;
  272. var new_elements = [];
  273. var anim_elements = [];
  274. elements.map(function (element) {
  275. var obj = element[0];
  276. var parent = obj.unit.parentNode;
  277. // let index = let findNodeIndex(obj.unit);
  278. var anim_element = void 0,
  279. new_element = void 0;
  280. element[0] = obj.unit;
  281. var _$$animateSVG = $.animateSVG.apply($, toConsumableArray(element));
  282. var _$$animateSVG2 = slicedToArray(_$$animateSVG, 2);
  283. anim_element = _$$animateSVG2[0];
  284. new_element = _$$animateSVG2[1];
  285. new_elements.push(new_element);
  286. anim_elements.push([anim_element, parent]);
  287. parent.replaceChild(anim_element, obj.unit);
  288. if (obj.array) {
  289. obj.array[obj.index] = new_element;
  290. } else {
  291. obj.object[obj.key] = new_element;
  292. }
  293. });
  294. var anim_svg = svg_container.cloneNode(true);
  295. anim_elements.map(function (anim_element, i) {
  296. anim_element[1].replaceChild(new_elements[i], anim_element[0]);
  297. elements[i][0] = new_elements[i];
  298. });
  299. return anim_svg;
  300. };
  301. $.transform = function (element, style) {
  302. element.style.transform = style;
  303. element.style.webkitTransform = style;
  304. element.style.msTransform = style;
  305. element.style.mozTransform = style;
  306. element.style.oTransform = style;
  307. };
  308. $.animateSVG = function (element, props, dur) {
  309. var easing_type = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : "linear";
  310. var type = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : undefined;
  311. var old_values = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : {};
  312. var anim_element = element.cloneNode(true);
  313. var new_element = element.cloneNode(true);
  314. for (var attributeName in props) {
  315. var animate_element = void 0;
  316. if (attributeName === 'transform') {
  317. animate_element = document.createElementNS("http://www.w3.org/2000/svg", "animateTransform");
  318. } else {
  319. animate_element = document.createElementNS("http://www.w3.org/2000/svg", "animate");
  320. }
  321. var current_value = old_values[attributeName] || element.getAttribute(attributeName);
  322. var value = props[attributeName];
  323. var anim_attr = {
  324. attributeName: attributeName,
  325. from: current_value,
  326. to: value,
  327. begin: "0s",
  328. dur: dur / 1000 + "s",
  329. values: current_value + ";" + value,
  330. keySplines: EASING[easing_type],
  331. keyTimes: "0;1",
  332. calcMode: "spline",
  333. fill: 'freeze'
  334. };
  335. if (type) {
  336. anim_attr["type"] = type;
  337. }
  338. for (var i in anim_attr) {
  339. animate_element.setAttribute(i, anim_attr[i]);
  340. }
  341. anim_element.appendChild(animate_element);
  342. if (type) {
  343. new_element.setAttribute(attributeName, "translate(" + value + ")");
  344. } else {
  345. new_element.setAttribute(attributeName, value);
  346. }
  347. }
  348. return [anim_element, new_element];
  349. };
  350. $.offset = function (element) {
  351. var rect = element.getBoundingClientRect();
  352. return {
  353. // https://stackoverflow.com/a/7436602/6495043
  354. // rect.top varies with scroll, so we add whatever has been
  355. // scrolled to it to get absolute distance from actual page top
  356. top: rect.top + (document.documentElement.scrollTop || document.body.scrollTop),
  357. left: rect.left + (document.documentElement.scrollLeft || document.body.scrollLeft)
  358. };
  359. };
  360. $.isElementInViewport = function (el) {
  361. // Although straightforward: https://stackoverflow.com/a/7557433/6495043
  362. var rect = el.getBoundingClientRect();
  363. return rect.top >= 0 && rect.left >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
  364. rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
  365. ;
  366. };
  367. $.bind = function (element, o) {
  368. if (element) {
  369. for (var event in o) {
  370. var callback = o[event];
  371. event.split(/\s+/).forEach(function (event) {
  372. element.addEventListener(event, callback);
  373. });
  374. }
  375. }
  376. };
  377. $.unbind = function (element, o) {
  378. if (element) {
  379. for (var event in o) {
  380. var callback = o[event];
  381. event.split(/\s+/).forEach(function (event) {
  382. element.removeEventListener(event, callback);
  383. });
  384. }
  385. }
  386. };
  387. $.fire = function (target, type, properties) {
  388. var evt = document.createEvent("HTMLEvents");
  389. evt.initEvent(type, true, true);
  390. for (var j in properties) {
  391. evt[j] = properties[j];
  392. }
  393. return target.dispatchEvent(evt);
  394. };
  395. function float_2(d) {
  396. return parseFloat(d.toFixed(2));
  397. }
  398. function arrays_equal(arr1, arr2) {
  399. if (arr1.length !== arr2.length) return false;
  400. var are_equal = true;
  401. arr1.map(function (d, i) {
  402. if (arr2[i] !== d) are_equal = false;
  403. });
  404. return are_equal;
  405. }
  406. function limitColor(r) {
  407. if (r > 255) return 255;else if (r < 0) return 0;
  408. return r;
  409. }
  410. function lightenDarkenColor(col, amt) {
  411. var usePound = false;
  412. if (col[0] == "#") {
  413. col = col.slice(1);
  414. usePound = true;
  415. }
  416. var num = parseInt(col, 16);
  417. var r = limitColor((num >> 16) + amt);
  418. var b = limitColor((num >> 8 & 0x00FF) + amt);
  419. var g = limitColor((num & 0x0000FF) + amt);
  420. return (usePound ? "#" : "") + (g | b << 8 | r << 16).toString(16);
  421. }
  422. /**
  423. * Shuffles array in place. ES6 version
  424. * @param {Array} a items An array containing the items.
  425. */
  426. var SvgTip = function () {
  427. function SvgTip(_ref) {
  428. var _ref$parent = _ref.parent,
  429. parent = _ref$parent === undefined ? null : _ref$parent;
  430. classCallCheck(this, SvgTip);
  431. this.parent = parent;
  432. this.title_name = '';
  433. this.title_value = '';
  434. this.list_values = [];
  435. this.title_value_first = 0;
  436. this.x = 0;
  437. this.y = 0;
  438. this.top = 0;
  439. this.left = 0;
  440. this.setup();
  441. }
  442. createClass(SvgTip, [{
  443. key: 'setup',
  444. value: function setup() {
  445. this.make_tooltip();
  446. }
  447. }, {
  448. key: 'refresh',
  449. value: function refresh() {
  450. this.fill();
  451. this.calc_position();
  452. // this.show_tip();
  453. }
  454. }, {
  455. key: 'make_tooltip',
  456. value: function make_tooltip() {
  457. var _this = this;
  458. this.container = $.create('div', {
  459. inside: this.parent,
  460. className: 'graph-svg-tip comparison',
  461. innerHTML: '<span class="title"></span>\n\t\t\t\t<ul class="data-point-list"></ul>\n\t\t\t\t<div class="svg-pointer"></div>'
  462. });
  463. this.hide_tip();
  464. this.title = this.container.querySelector('.title');
  465. this.data_point_list = this.container.querySelector('.data-point-list');
  466. this.parent.addEventListener('mouseleave', function () {
  467. _this.hide_tip();
  468. });
  469. }
  470. }, {
  471. key: 'fill',
  472. value: function fill() {
  473. var _this2 = this;
  474. var title = void 0;
  475. if (this.title_value_first) {
  476. title = '<strong>' + this.title_value + '</strong>' + this.title_name;
  477. } else {
  478. title = this.title_name + '<strong>' + this.title_value + '</strong>';
  479. }
  480. this.title.innerHTML = title;
  481. this.data_point_list.innerHTML = '';
  482. this.list_values.map(function (set$$1) {
  483. var li = $.create('li', {
  484. className: 'border-top ' + (set$$1.color || 'black'),
  485. innerHTML: '<strong style="display: block;">' + (isNaN(set$$1.value) ? '' : set$$1.value) + '</strong>\n\t\t\t\t\t' + (set$$1.title ? set$$1.title : '')
  486. });
  487. _this2.data_point_list.appendChild(li);
  488. });
  489. }
  490. }, {
  491. key: 'calc_position',
  492. value: function calc_position() {
  493. var width = this.container.offsetWidth;
  494. this.top = this.y - this.container.offsetHeight;
  495. this.left = this.x - width / 2;
  496. var max_left = this.parent.offsetWidth - width;
  497. var pointer = this.container.querySelector('.svg-pointer');
  498. if (this.left < 0) {
  499. pointer.style.left = 'calc(50% - ' + -1 * this.left + 'px)';
  500. this.left = 0;
  501. } else if (this.left > max_left) {
  502. var delta = this.left - max_left;
  503. var pointer_offset = 'calc(50% + ' + delta + 'px)';
  504. pointer.style.left = pointer_offset;
  505. this.left = max_left;
  506. } else {
  507. pointer.style.left = '50%';
  508. }
  509. }
  510. }, {
  511. key: 'set_values',
  512. value: function set_values(x, y) {
  513. var title_name = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
  514. var title_value = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
  515. var list_values = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [];
  516. var title_value_first = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 0;
  517. this.title_name = title_name;
  518. this.title_value = title_value;
  519. this.list_values = list_values;
  520. this.x = x;
  521. this.y = y;
  522. this.title_value_first = title_value_first;
  523. this.refresh();
  524. }
  525. }, {
  526. key: 'hide_tip',
  527. value: function hide_tip() {
  528. this.container.style.top = '0px';
  529. this.container.style.left = '0px';
  530. this.container.style.opacity = '0';
  531. }
  532. }, {
  533. key: 'show_tip',
  534. value: function show_tip() {
  535. this.container.style.top = this.top + 'px';
  536. this.container.style.left = this.left + 'px';
  537. this.container.style.opacity = '1';
  538. }
  539. }]);
  540. return SvgTip;
  541. }();
  542. var BaseChart = function () {
  543. function BaseChart(_ref) {
  544. var _ref$height = _ref.height,
  545. height = _ref$height === undefined ? 240 : _ref$height,
  546. _ref$title = _ref.title,
  547. title = _ref$title === undefined ? '' : _ref$title,
  548. _ref$subtitle = _ref.subtitle,
  549. subtitle = _ref$subtitle === undefined ? '' : _ref$subtitle,
  550. _ref$format_lambdas = _ref.format_lambdas,
  551. format_lambdas = _ref$format_lambdas === undefined ? {} : _ref$format_lambdas,
  552. _ref$summary = _ref.summary,
  553. summary = _ref$summary === undefined ? [] : _ref$summary,
  554. _ref$is_navigable = _ref.is_navigable,
  555. is_navigable = _ref$is_navigable === undefined ? 0 : _ref$is_navigable,
  556. _ref$has_legend = _ref.has_legend,
  557. has_legend = _ref$has_legend === undefined ? 0 : _ref$has_legend,
  558. _ref$type = _ref.type,
  559. type = _ref$type === undefined ? '' : _ref$type,
  560. parent = _ref.parent,
  561. data = _ref.data;
  562. classCallCheck(this, BaseChart);
  563. this.raw_chart_args = arguments[0];
  564. this.parent = typeof parent === 'string' ? document.querySelector(parent) : parent;
  565. this.title = title;
  566. this.subtitle = subtitle;
  567. this.data = data;
  568. this.format_lambdas = format_lambdas;
  569. this.specific_values = data.specific_values || [];
  570. this.summary = summary;
  571. this.is_navigable = is_navigable;
  572. if (this.is_navigable) {
  573. this.current_index = 0;
  574. }
  575. this.has_legend = has_legend;
  576. this.chart_types = ['line', 'scatter', 'bar', 'percentage', 'heatmap', 'pie'];
  577. this.set_margins(height);
  578. }
  579. createClass(BaseChart, [{
  580. key: 'get_different_chart',
  581. value: function get_different_chart(type) {
  582. if (!this.chart_types.includes(type)) {
  583. console.error('\'' + type + '\' is not a valid chart type.');
  584. }
  585. if (type === this.type) return;
  586. // Only across compatible types
  587. var compatible_types = {
  588. bar: ['line', 'scatter', 'percentage', 'pie'],
  589. line: ['scatter', 'bar', 'percentage', 'pie'],
  590. pie: ['line', 'scatter', 'percentage', 'bar'],
  591. scatter: ['line', 'bar', 'percentage', 'pie'],
  592. percentage: ['bar', 'line', 'scatter', 'pie'],
  593. heatmap: []
  594. };
  595. if (!compatible_types[this.type].includes(type)) {
  596. console.error('\'' + this.type + '\' chart cannot be converted to a \'' + type + '\' chart.');
  597. }
  598. // Okay, this is anticlimactic
  599. // this function will need to actually be 'change_chart_type(type)'
  600. // that will update only the required elements, but for now ...
  601. return new Chart({
  602. parent: this.raw_chart_args.parent,
  603. title: this.title,
  604. data: this.raw_chart_args.data,
  605. type: type,
  606. height: this.raw_chart_args.height
  607. });
  608. }
  609. }, {
  610. key: 'set_margins',
  611. value: function set_margins(height) {
  612. this.base_height = height;
  613. this.height = height - 40;
  614. this.translate_x = 60;
  615. this.translate_y = 10;
  616. }
  617. }, {
  618. key: 'setup',
  619. value: function setup() {
  620. if (!this.parent) {
  621. console.error("No parent element to render on was provided.");
  622. return;
  623. }
  624. this.bind_window_events();
  625. this.refresh(true);
  626. }
  627. }, {
  628. key: 'bind_window_events',
  629. value: function bind_window_events() {
  630. var _this = this;
  631. window.addEventListener('resize', function () {
  632. return _this.refresh();
  633. });
  634. window.addEventListener('orientationchange', function () {
  635. return _this.refresh();
  636. });
  637. }
  638. }, {
  639. key: 'refresh',
  640. value: function refresh() {
  641. var init = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  642. this.setup_base_values();
  643. this.set_width();
  644. this.setup_container();
  645. this.setup_components();
  646. this.setup_values();
  647. this.setup_utils();
  648. this.make_graph_components(init);
  649. this.make_tooltip();
  650. if (this.summary.length > 0) {
  651. this.show_custom_summary();
  652. } else {
  653. this.show_summary();
  654. }
  655. if (this.is_navigable) {
  656. this.setup_navigation(init);
  657. }
  658. }
  659. }, {
  660. key: 'set_width',
  661. value: function set_width() {
  662. var _this2 = this;
  663. var special_values_width = 0;
  664. this.specific_values.map(function (val) {
  665. if (_this2.get_strwidth(val.title) > special_values_width) {
  666. special_values_width = _this2.get_strwidth(val.title) - 40;
  667. }
  668. });
  669. this.base_width = this.parent.offsetWidth - special_values_width;
  670. this.width = this.base_width - this.translate_x * 2;
  671. }
  672. }, {
  673. key: 'setup_base_values',
  674. value: function setup_base_values() {}
  675. }, {
  676. key: 'setup_container',
  677. value: function setup_container() {
  678. this.container = $.create('div', {
  679. className: 'chart-container',
  680. innerHTML: '<h6 class="title">' + this.title + '</h6>\n\t\t\t\t<h6 class="sub-title uppercase">' + this.subtitle + '</h6>\n\t\t\t\t<div class="frappe-chart graphics"></div>\n\t\t\t\t<div class="graph-stats-container"></div>'
  681. });
  682. // Chart needs a dedicated parent element
  683. this.parent.innerHTML = '';
  684. this.parent.appendChild(this.container);
  685. this.chart_wrapper = this.container.querySelector('.frappe-chart');
  686. this.stats_wrapper = this.container.querySelector('.graph-stats-container');
  687. this.make_chart_area();
  688. this.make_draw_area();
  689. }
  690. }, {
  691. key: 'make_chart_area',
  692. value: function make_chart_area() {
  693. this.svg = $.createSVG('svg', {
  694. className: 'chart',
  695. inside: this.chart_wrapper,
  696. width: this.base_width,
  697. height: this.base_height
  698. });
  699. this.svg_defs = $.createSVG('defs', {
  700. inside: this.svg
  701. });
  702. return this.svg;
  703. }
  704. }, {
  705. key: 'make_draw_area',
  706. value: function make_draw_area() {
  707. this.draw_area = $.createSVG("g", {
  708. className: this.type + '-chart',
  709. inside: this.svg,
  710. transform: 'translate(' + this.translate_x + ', ' + this.translate_y + ')'
  711. });
  712. }
  713. }, {
  714. key: 'setup_components',
  715. value: function setup_components() {}
  716. }, {
  717. key: 'make_tooltip',
  718. value: function make_tooltip() {
  719. this.tip = new SvgTip({
  720. parent: this.chart_wrapper
  721. });
  722. this.bind_tooltip();
  723. }
  724. }, {
  725. key: 'show_summary',
  726. value: function show_summary() {}
  727. }, {
  728. key: 'show_custom_summary',
  729. value: function show_custom_summary() {
  730. var _this3 = this;
  731. this.summary.map(function (d) {
  732. var stats = $.create('div', {
  733. className: 'stats',
  734. innerHTML: '<span class="indicator ' + d.color + '">' + d.title + ': ' + d.value + '</span>'
  735. });
  736. _this3.stats_wrapper.appendChild(stats);
  737. });
  738. }
  739. }, {
  740. key: 'setup_navigation',
  741. value: function setup_navigation() {
  742. var _this4 = this;
  743. var init = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  744. this.make_overlay();
  745. if (init) {
  746. this.bind_overlay();
  747. document.addEventListener('keydown', function (e) {
  748. if ($.isElementInViewport(_this4.chart_wrapper)) {
  749. e = e || window.event;
  750. if (e.keyCode == '37') {
  751. _this4.on_left_arrow();
  752. } else if (e.keyCode == '39') {
  753. _this4.on_right_arrow();
  754. } else if (e.keyCode == '38') {
  755. _this4.on_up_arrow();
  756. } else if (e.keyCode == '40') {
  757. _this4.on_down_arrow();
  758. } else if (e.keyCode == '13') {
  759. _this4.on_enter_key();
  760. }
  761. }
  762. });
  763. }
  764. }
  765. }, {
  766. key: 'make_overlay',
  767. value: function make_overlay() {}
  768. }, {
  769. key: 'bind_overlay',
  770. value: function bind_overlay() {}
  771. }, {
  772. key: 'bind_units',
  773. value: function bind_units() {}
  774. }, {
  775. key: 'on_left_arrow',
  776. value: function on_left_arrow() {}
  777. }, {
  778. key: 'on_right_arrow',
  779. value: function on_right_arrow() {}
  780. }, {
  781. key: 'on_up_arrow',
  782. value: function on_up_arrow() {}
  783. }, {
  784. key: 'on_down_arrow',
  785. value: function on_down_arrow() {}
  786. }, {
  787. key: 'on_enter_key',
  788. value: function on_enter_key() {}
  789. }, {
  790. key: 'get_data_point',
  791. value: function get_data_point() {
  792. var index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.current_index;
  793. // check for length
  794. var data_point = {
  795. index: index
  796. };
  797. var y = this.y[0];
  798. ['svg_units', 'y_tops', 'values'].map(function (key) {
  799. var data_key = key.slice(0, key.length - 1);
  800. data_point[data_key] = y[key][index];
  801. });
  802. data_point.label = this.x[index];
  803. return data_point;
  804. }
  805. }, {
  806. key: 'update_current_data_point',
  807. value: function update_current_data_point(index) {
  808. index = parseInt(index);
  809. if (index < 0) index = 0;
  810. if (index >= this.x.length) index = this.x.length - 1;
  811. if (index === this.current_index) return;
  812. this.current_index = index;
  813. $.fire(this.parent, "data-select", this.get_data_point());
  814. }
  815. // Helpers
  816. }, {
  817. key: 'get_strwidth',
  818. value: function get_strwidth(string) {
  819. return (string + "").length * 8;
  820. }
  821. // Objects
  822. }, {
  823. key: 'setup_utils',
  824. value: function setup_utils() {}
  825. }]);
  826. return BaseChart;
  827. }();
  828. var AxisChart = function (_BaseChart) {
  829. inherits(AxisChart, _BaseChart);
  830. function AxisChart(args) {
  831. classCallCheck(this, AxisChart);
  832. var _this = possibleConstructorReturn(this, (AxisChart.__proto__ || Object.getPrototypeOf(AxisChart)).call(this, args));
  833. _this.x = _this.data.labels;
  834. _this.y = _this.data.datasets;
  835. _this.is_series = args.is_series;
  836. _this.get_y_label = _this.format_lambdas.y_label;
  837. _this.get_y_tooltip = _this.format_lambdas.y_tooltip;
  838. _this.get_x_tooltip = _this.format_lambdas.x_tooltip;
  839. _this.colors = ['green', 'blue', 'violet', 'red', 'orange', 'yellow', 'light-blue', 'light-green', 'purple', 'magenta'];
  840. _this.zero_line = _this.height;
  841. return _this;
  842. }
  843. createClass(AxisChart, [{
  844. key: 'setup_values',
  845. value: function setup_values() {
  846. this.data.datasets.map(function (d) {
  847. d.values = d.values.map(function (val) {
  848. return !isNaN(val) ? val : 0;
  849. });
  850. });
  851. this.setup_x();
  852. this.setup_y();
  853. }
  854. }, {
  855. key: 'setup_x',
  856. value: function setup_x() {
  857. var _this2 = this;
  858. this.set_avg_unit_width_and_x_offset();
  859. if (this.x_axis_positions) {
  860. this.x_old_axis_positions = this.x_axis_positions.slice();
  861. }
  862. this.x_axis_positions = this.x.map(function (d, i) {
  863. return float_2(_this2.x_offset + i * _this2.avg_unit_width);
  864. });
  865. if (!this.x_old_axis_positions) {
  866. this.x_old_axis_positions = this.x_axis_positions.slice();
  867. }
  868. }
  869. }, {
  870. key: 'setup_y',
  871. value: function setup_y() {
  872. if (this.y_axis_values) {
  873. this.y_old_axis_values = this.y_axis_values.slice();
  874. }
  875. var values = this.get_all_y_values();
  876. if (this.y_sums && this.y_sums.length > 0) {
  877. values = values.concat(this.y_sums);
  878. }
  879. this.y_axis_values = this.get_y_axis_points(values);
  880. if (!this.y_old_axis_values) {
  881. this.y_old_axis_values = this.y_axis_values.slice();
  882. }
  883. var y_pts = this.y_axis_values;
  884. var value_range = y_pts[y_pts.length - 1] - y_pts[0];
  885. if (this.multiplier) this.old_multiplier = this.multiplier;
  886. this.multiplier = this.height / value_range;
  887. if (!this.old_multiplier) this.old_multiplier = this.multiplier;
  888. var zero_index = y_pts.indexOf(0);
  889. var interval = y_pts[1] - y_pts[0];
  890. var interval_height = interval * this.multiplier;
  891. if (this.zero_line) this.old_zero_line = this.zero_line;
  892. this.zero_line = this.height - zero_index * interval_height;
  893. if (!this.old_zero_line) this.old_zero_line = this.zero_line;
  894. }
  895. }, {
  896. key: 'setup_components',
  897. value: function setup_components() {
  898. get(AxisChart.prototype.__proto__ || Object.getPrototypeOf(AxisChart.prototype), 'setup_components', this).call(this);
  899. this.setup_marker_components();
  900. this.setup_aggregation_components();
  901. this.setup_graph_components();
  902. }
  903. }, {
  904. key: 'setup_marker_components',
  905. value: function setup_marker_components() {
  906. this.y_axis_group = $.createSVG('g', { className: 'y axis', inside: this.draw_area });
  907. this.x_axis_group = $.createSVG('g', { className: 'x axis', inside: this.draw_area });
  908. this.specific_y_group = $.createSVG('g', { className: 'specific axis', inside: this.draw_area });
  909. }
  910. }, {
  911. key: 'setup_aggregation_components',
  912. value: function setup_aggregation_components() {
  913. this.sum_group = $.createSVG('g', { className: 'data-points', inside: this.draw_area });
  914. this.average_group = $.createSVG('g', { className: 'chart-area', inside: this.draw_area });
  915. }
  916. }, {
  917. key: 'setup_graph_components',
  918. value: function setup_graph_components() {
  919. var _this3 = this;
  920. this.svg_units_groups = [];
  921. this.y.map(function (d, i) {
  922. _this3.svg_units_groups[i] = $.createSVG('g', {
  923. className: 'data-points data-points-' + i,
  924. inside: _this3.draw_area
  925. });
  926. });
  927. }
  928. }, {
  929. key: 'make_graph_components',
  930. value: function make_graph_components() {
  931. var init = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  932. this.make_y_axis();
  933. this.make_x_axis();
  934. this.draw_graph(init);
  935. this.make_y_specifics();
  936. }
  937. // make VERTICAL lines for x values
  938. }, {
  939. key: 'make_x_axis',
  940. value: function make_x_axis() {
  941. var _this4 = this;
  942. var animate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  943. var start_at = void 0,
  944. height = void 0,
  945. text_start_at = void 0,
  946. axis_line_class = '';
  947. if (this.x_axis_mode === 'span') {
  948. // long spanning lines
  949. start_at = -7;
  950. height = this.height + 15;
  951. text_start_at = this.height + 25;
  952. } else if (this.x_axis_mode === 'tick') {
  953. // short label lines
  954. start_at = this.height;
  955. height = 6;
  956. text_start_at = 9;
  957. axis_line_class = 'x-axis-label';
  958. }
  959. this.x_axis_group.setAttribute('transform', 'translate(0,' + start_at + ')');
  960. if (animate) {
  961. this.make_anim_x_axis(height, text_start_at, axis_line_class);
  962. return;
  963. }
  964. var allowed_space = this.avg_unit_width * 1.5;
  965. var allowed_letters = allowed_space / 8;
  966. this.x_axis_group.textContent = '';
  967. this.x.map(function (point, i) {
  968. var space_taken = _this4.get_strwidth(point) + 2;
  969. if (space_taken > allowed_space) {
  970. if (_this4.is_series) {
  971. // Skip some axis lines if X axis is a series
  972. var skips = 1;
  973. while (space_taken / skips * 2 > allowed_space) {
  974. skips++;
  975. }
  976. if (i % skips !== 0) {
  977. return;
  978. }
  979. } else {
  980. point = point.slice(0, allowed_letters - 3) + " ...";
  981. }
  982. }
  983. _this4.x_axis_group.appendChild(_this4.make_x_line(height, text_start_at, point, 'x-value-text', axis_line_class, _this4.x_axis_positions[i]));
  984. });
  985. }
  986. // make HORIZONTAL lines for y values
  987. }, {
  988. key: 'make_y_axis',
  989. value: function make_y_axis() {
  990. var _this5 = this;
  991. var animate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  992. if (animate) {
  993. this.make_anim_y_axis();
  994. this.make_anim_y_specifics();
  995. return;
  996. }
  997. var _get_y_axis_line_prop = this.get_y_axis_line_props(),
  998. _get_y_axis_line_prop2 = slicedToArray(_get_y_axis_line_prop, 4),
  999. width = _get_y_axis_line_prop2[0],
  1000. text_end_at = _get_y_axis_line_prop2[1],
  1001. axis_line_class = _get_y_axis_line_prop2[2],
  1002. start_at = _get_y_axis_line_prop2[3];
  1003. this.y_axis_group.textContent = '';
  1004. this.y_axis_values.map(function (value, i) {
  1005. _this5.y_axis_group.appendChild(_this5.make_y_line(start_at, width, text_end_at, value, 'y-value-text', axis_line_class, _this5.zero_line - value * _this5.multiplier, value === 0 && i !== 0 // Non-first Zero line
  1006. ));
  1007. });
  1008. }
  1009. }, {
  1010. key: 'get_y_axis_line_props',
  1011. value: function get_y_axis_line_props() {
  1012. var specific = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  1013. if (specific) {
  1014. return [this.width, this.width + 5, 'specific-value', 0];
  1015. }
  1016. var width = void 0,
  1017. text_end_at = -9,
  1018. axis_line_class = '',
  1019. start_at = 0;
  1020. if (this.y_axis_mode === 'span') {
  1021. // long spanning lines
  1022. width = this.width + 6;
  1023. start_at = -6;
  1024. } else if (this.y_axis_mode === 'tick') {
  1025. // short label lines
  1026. width = -6;
  1027. axis_line_class = 'y-axis-label';
  1028. }
  1029. return [width, text_end_at, axis_line_class, start_at];
  1030. }
  1031. }, {
  1032. key: 'draw_graph',
  1033. value: function draw_graph() {
  1034. var _this6 = this;
  1035. var init = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  1036. if (this.raw_chart_args.hasOwnProperty("init") && !this.raw_chart_args.init) {
  1037. this.y.map(function (d, i) {
  1038. d.svg_units = [];
  1039. _this6.make_path && _this6.make_path(d, i, _this6.x_axis_positions, d.y_tops, d.color || _this6.colors[i]);
  1040. _this6.make_new_units(d, i);
  1041. _this6.calc_y_dependencies();
  1042. });
  1043. return;
  1044. }
  1045. if (init) {
  1046. this.draw_new_graph_and_animate();
  1047. return;
  1048. }
  1049. this.y.map(function (d, i) {
  1050. d.svg_units = [];
  1051. _this6.make_path && _this6.make_path(d, i, _this6.x_axis_positions, d.y_tops, d.color || _this6.colors[i]);
  1052. _this6.make_new_units(d, i);
  1053. });
  1054. }
  1055. }, {
  1056. key: 'draw_new_graph_and_animate',
  1057. value: function draw_new_graph_and_animate() {
  1058. var _this7 = this;
  1059. var data = [];
  1060. this.y.map(function (d, i) {
  1061. // Anim: Don't draw initial values, store them and update later
  1062. d.y_tops = new Array(d.values.length).fill(_this7.zero_line); // no value
  1063. data.push({ values: d.values });
  1064. d.svg_units = [];
  1065. _this7.make_path && _this7.make_path(d, i, _this7.x_axis_positions, d.y_tops, d.color || _this7.colors[i]);
  1066. _this7.make_new_units(d, i);
  1067. });
  1068. setTimeout(function () {
  1069. _this7.update_values(data);
  1070. }, 350);
  1071. }
  1072. }, {
  1073. key: 'setup_navigation',
  1074. value: function setup_navigation(init) {
  1075. var _this8 = this;
  1076. if (init) {
  1077. // Hack: defer nav till initial update_values
  1078. setTimeout(function () {
  1079. get(AxisChart.prototype.__proto__ || Object.getPrototypeOf(AxisChart.prototype), 'setup_navigation', _this8).call(_this8, init);
  1080. }, 500);
  1081. } else {
  1082. get(AxisChart.prototype.__proto__ || Object.getPrototypeOf(AxisChart.prototype), 'setup_navigation', this).call(this, init);
  1083. }
  1084. }
  1085. }, {
  1086. key: 'make_new_units',
  1087. value: function make_new_units(d, i) {
  1088. this.make_new_units_for_dataset(this.x_axis_positions, d.y_tops, d.color || this.colors[i], i, this.y.length);
  1089. }
  1090. }, {
  1091. key: 'make_new_units_for_dataset',
  1092. value: function make_new_units_for_dataset(x_values, y_values, color, dataset_index, no_of_datasets, units_group, units_array, unit) {
  1093. var _this9 = this;
  1094. if (!units_group) units_group = this.svg_units_groups[dataset_index];
  1095. if (!units_array) units_array = this.y[dataset_index].svg_units;
  1096. if (!unit) unit = this.unit_args;
  1097. units_group.textContent = '';
  1098. units_array.length = 0;
  1099. y_values.map(function (y, i) {
  1100. var data_unit = _this9.draw[unit.type](x_values[i], y, unit.args, color, i, dataset_index, no_of_datasets);
  1101. units_group.appendChild(data_unit);
  1102. units_array.push(data_unit);
  1103. });
  1104. if (this.is_navigable) {
  1105. this.bind_units(units_array);
  1106. }
  1107. }
  1108. }, {
  1109. key: 'make_y_specifics',
  1110. value: function make_y_specifics() {
  1111. var _this10 = this;
  1112. this.specific_y_group.textContent = '';
  1113. this.specific_values.map(function (d) {
  1114. _this10.specific_y_group.appendChild(_this10.make_y_line(0, _this10.width, _this10.width + 5, d.title.toUpperCase(), 'specific-value', 'specific-value', _this10.zero_line - d.value * _this10.multiplier, false, d.line_type));
  1115. });
  1116. }
  1117. }, {
  1118. key: 'bind_tooltip',
  1119. value: function bind_tooltip() {
  1120. var _this11 = this;
  1121. // TODO: could be in tooltip itself, as it is a given functionality for its parent
  1122. this.chart_wrapper.addEventListener('mousemove', function (e) {
  1123. var offset = $.offset(_this11.chart_wrapper);
  1124. var relX = e.pageX - offset.left - _this11.translate_x;
  1125. var relY = e.pageY - offset.top - _this11.translate_y;
  1126. if (relY < _this11.height + _this11.translate_y * 2) {
  1127. _this11.map_tooltip_x_position_and_show(relX);
  1128. } else {
  1129. _this11.tip.hide_tip();
  1130. }
  1131. });
  1132. }
  1133. }, {
  1134. key: 'map_tooltip_x_position_and_show',
  1135. value: function map_tooltip_x_position_and_show(relX) {
  1136. var _this12 = this;
  1137. if (!this.y_min_tops) return;
  1138. for (var i = this.x_axis_positions.length - 1; i >= 0; i--) {
  1139. var x_val = this.x_axis_positions[i];
  1140. // let delta = i === 0 ? this.avg_unit_width : x_val - this.x_axis_positions[i-1];
  1141. if (relX > x_val - this.avg_unit_width / 2) {
  1142. var x = x_val + this.translate_x;
  1143. var y = this.y_min_tops[i] + this.translate_y;
  1144. var title = this.x.formatted && this.x.formatted.length > 0 ? this.x.formatted[i] : this.x[i];
  1145. var values = this.y.map(function (set$$1, j) {
  1146. return {
  1147. title: set$$1.title,
  1148. value: set$$1.formatted ? set$$1.formatted[i] : set$$1.values[i],
  1149. color: set$$1.color || _this12.colors[j]
  1150. };
  1151. });
  1152. // TODO: upside-down tooltips for negative values?
  1153. this.tip.set_values(x, y, title, '', values);
  1154. this.tip.show_tip();
  1155. break;
  1156. }
  1157. }
  1158. }
  1159. // API
  1160. }, {
  1161. key: 'show_sums',
  1162. value: function show_sums() {
  1163. var _this13 = this;
  1164. this.updating = true;
  1165. this.y_sums = new Array(this.x_axis_positions.length).fill(0);
  1166. this.y.map(function (d) {
  1167. d.values.map(function (value, i) {
  1168. _this13.y_sums[i] += value;
  1169. });
  1170. });
  1171. // Remake y axis, animate
  1172. this.update_values();
  1173. // Then make sum units, don't animate
  1174. this.sum_units = [];
  1175. this.make_new_units_for_dataset(this.x_axis_positions, this.y_sums.map(function (val) {
  1176. return float_2(_this13.zero_line - val * _this13.multiplier);
  1177. }), 'light-grey', 0, 1, this.sum_group, this.sum_units);
  1178. // this.make_path && this.make_path(d, i, old_x, old_y, d.color || this.colors[i]);
  1179. this.updating = false;
  1180. }
  1181. }, {
  1182. key: 'hide_sums',
  1183. value: function hide_sums() {
  1184. if (this.updating) return;
  1185. this.y_sums = [];
  1186. this.sum_group.textContent = '';
  1187. this.sum_units = [];
  1188. this.update_values();
  1189. }
  1190. }, {
  1191. key: 'show_averages',
  1192. value: function show_averages() {
  1193. var _this14 = this;
  1194. this.old_specific_values = this.specific_values.slice();
  1195. this.y.map(function (d, i) {
  1196. var sum = 0;
  1197. d.values.map(function (e) {
  1198. sum += e;
  1199. });
  1200. var average = sum / d.values.length;
  1201. _this14.specific_values.push({
  1202. title: "AVG" + " " + (i + 1),
  1203. line_type: "dashed",
  1204. value: average,
  1205. auto: 1
  1206. });
  1207. });
  1208. this.update_values();
  1209. }
  1210. }, {
  1211. key: 'hide_averages',
  1212. value: function hide_averages() {
  1213. var _this15 = this;
  1214. this.old_specific_values = this.specific_values.slice();
  1215. var indices_to_remove = [];
  1216. this.specific_values.map(function (d, i) {
  1217. if (d.auto) indices_to_remove.unshift(i);
  1218. });
  1219. indices_to_remove.map(function (index) {
  1220. _this15.specific_values.splice(index, 1);
  1221. });
  1222. this.update_values();
  1223. }
  1224. }, {
  1225. key: 'update_values',
  1226. value: function update_values(new_y, new_x) {
  1227. var _this16 = this;
  1228. if (!new_x) {
  1229. new_x = this.x;
  1230. }
  1231. this.elements_to_animate = [];
  1232. this.updating = true;
  1233. this.old_x_values = this.x.slice();
  1234. this.old_y_axis_tops = this.y.map(function (d) {
  1235. return d.y_tops.slice();
  1236. });
  1237. this.old_y_values = this.y.map(function (d) {
  1238. return d.values;
  1239. });
  1240. this.no_of_extra_pts = new_x.length - this.x.length;
  1241. // Just update values prop, setup_x/y() will do the rest
  1242. if (new_y) this.y.map(function (d, i) {
  1243. d.values = new_y[i].values;
  1244. });
  1245. if (new_x) this.x = new_x;
  1246. this.setup_x();
  1247. this.setup_y();
  1248. // Animate only if positions have changed
  1249. if (!arrays_equal(this.x_old_axis_positions, this.x_axis_positions)) {
  1250. this.make_x_axis(true);
  1251. setTimeout(function () {
  1252. if (!_this16.updating) _this16.make_x_axis();
  1253. }, 350);
  1254. }
  1255. if (!arrays_equal(this.y_old_axis_values, this.y_axis_values) || this.old_specific_values && !arrays_equal(this.old_specific_values, this.specific_values)) {
  1256. this.make_y_axis(true);
  1257. setTimeout(function () {
  1258. if (!_this16.updating) {
  1259. _this16.make_y_axis();
  1260. _this16.make_y_specifics();
  1261. }
  1262. }, 350);
  1263. }
  1264. // Change in data, so calculate dependencies
  1265. this.calc_y_dependencies();
  1266. this.animate_graphs();
  1267. // Trigger animation with the animatable elements in this.elements_to_animate
  1268. this.run_animation();
  1269. this.updating = false;
  1270. }
  1271. }, {
  1272. key: 'add_data_point',
  1273. value: function add_data_point(y_point, x_point) {
  1274. var index = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this.x.length;
  1275. var new_y = this.y.map(function (data_set) {
  1276. return { values: data_set.values };
  1277. });
  1278. new_y.map(function (d, i) {
  1279. d.values.splice(index, 0, y_point[i]);
  1280. });
  1281. var new_x = this.x.slice();
  1282. new_x.splice(index, 0, x_point);
  1283. this.update_values(new_y, new_x);
  1284. }
  1285. }, {
  1286. key: 'remove_data_point',
  1287. value: function remove_data_point() {
  1288. var index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.x.length - 1;
  1289. if (this.x.length < 3) return;
  1290. var new_y = this.y.map(function (data_set) {
  1291. return { values: data_set.values };
  1292. });
  1293. new_y.map(function (d) {
  1294. d.values.splice(index, 1);
  1295. });
  1296. var new_x = this.x.slice();
  1297. new_x.splice(index, 1);
  1298. this.update_values(new_y, new_x);
  1299. }
  1300. }, {
  1301. key: 'run_animation',
  1302. value: function run_animation() {
  1303. var _this17 = this;
  1304. var anim_svg = $.runSVGAnimation(this.svg, this.elements_to_animate);
  1305. if (this.svg.parentNode == this.chart_wrapper) {
  1306. this.chart_wrapper.removeChild(this.svg);
  1307. this.chart_wrapper.appendChild(anim_svg);
  1308. }
  1309. // Replace the new svg (data has long been replaced)
  1310. setTimeout(function () {
  1311. if (anim_svg.parentNode == _this17.chart_wrapper) {
  1312. _this17.chart_wrapper.removeChild(anim_svg);
  1313. _this17.chart_wrapper.appendChild(_this17.svg);
  1314. }
  1315. }, 250);
  1316. }
  1317. }, {
  1318. key: 'animate_graphs',
  1319. value: function animate_graphs() {
  1320. var _this18 = this;
  1321. this.y.map(function (d, i) {
  1322. // Pre-prep, equilize no of positions between old and new
  1323. var _calc_old_and_new_pos = _this18.calc_old_and_new_postions(d, i),
  1324. _calc_old_and_new_pos2 = slicedToArray(_calc_old_and_new_pos, 4),
  1325. old_x = _calc_old_and_new_pos2[0],
  1326. old_y = _calc_old_and_new_pos2[1],
  1327. new_x = _calc_old_and_new_pos2[2],
  1328. new_y = _calc_old_and_new_pos2[3];
  1329. if (_this18.no_of_extra_pts >= 0) {
  1330. _this18.make_path && _this18.make_path(d, i, old_x, old_y, d.color || _this18.colors[i]);
  1331. _this18.make_new_units_for_dataset(old_x, old_y, d.color || _this18.colors[i], i, _this18.y.length);
  1332. }
  1333. d.path && _this18.animate_path(d, i, old_x, old_y, new_x, new_y);
  1334. _this18.animate_units(d, i, old_x, old_y, new_x, new_y);
  1335. });
  1336. // TODO: replace with real units
  1337. setTimeout(function () {
  1338. _this18.y.map(function (d, i) {
  1339. _this18.make_path && _this18.make_path(d, i, _this18.x_axis_positions, d.y_tops, d.color || _this18.colors[i]);
  1340. _this18.make_new_units(d, i);
  1341. });
  1342. }, 400);
  1343. }
  1344. }, {
  1345. key: 'animate_path',
  1346. value: function animate_path(d, i, old_x, old_y, new_x, new_y) {
  1347. // Animate path
  1348. var new_points_list = new_y.map(function (y, i) {
  1349. return new_x[i] + ',' + y;
  1350. });
  1351. var new_path_str = new_points_list.join("L");
  1352. var path_args = [{ unit: d.path, object: d, key: 'path' }, { d: "M" + new_path_str }, 350, "easein"];
  1353. this.elements_to_animate.push(path_args);
  1354. // Animate region
  1355. if (d.region_path) {
  1356. var reg_start_pt = '0,' + this.zero_line + 'L';
  1357. var reg_end_pt = 'L' + this.width + ',' + this.zero_line;
  1358. var region_args = [{ unit: d.region_path, object: d, key: 'region_path' }, { d: "M" + reg_start_pt + new_path_str + reg_end_pt }, 350, "easein"];
  1359. this.elements_to_animate.push(region_args);
  1360. }
  1361. }
  1362. }, {
  1363. key: 'animate_units',
  1364. value: function animate_units(d, index, old_x, old_y, new_x, new_y) {
  1365. var _this19 = this;
  1366. var type = this.unit_args.type;
  1367. d.svg_units.map(function (unit, i) {
  1368. if (new_x[i] === undefined || new_y[i] === undefined) return;
  1369. _this19.elements_to_animate.push(_this19.animate[type]({ unit: unit, array: d.svg_units, index: i }, // unit, with info to replace where it came from in the data
  1370. new_x[i], new_y[i], index));
  1371. });
  1372. }
  1373. }, {
  1374. key: 'calc_old_and_new_postions',
  1375. value: function calc_old_and_new_postions(d, i) {
  1376. var old_x = this.x_old_axis_positions.slice();
  1377. var new_x = this.x_axis_positions.slice();
  1378. var old_y = this.old_y_axis_tops[i].slice();
  1379. var new_y = d.y_tops.slice();
  1380. var last_old_x_pos = old_x[old_x.length - 1];
  1381. var last_old_y_pos = old_y[old_y.length - 1];
  1382. var last_new_x_pos = new_x[new_x.length - 1];
  1383. var last_new_y_pos = new_y[new_y.length - 1];
  1384. if (this.no_of_extra_pts >= 0) {
  1385. // First substitute current path with a squiggled one (looking the same but
  1386. // having more points at end),
  1387. // then animate to stretch it later to new points
  1388. // (new points already have more points)
  1389. // Hence, the extra end points will correspond to current(old) positions
  1390. var filler_x = new Array(Math.abs(this.no_of_extra_pts)).fill(last_old_x_pos);
  1391. var filler_y = new Array(Math.abs(this.no_of_extra_pts)).fill(last_old_y_pos);
  1392. old_x = old_x.concat(filler_x);
  1393. old_y = old_y.concat(filler_y);
  1394. } else {
  1395. // Just modify the new points to have extra points
  1396. // with the same position at end
  1397. var _filler_x = new Array(Math.abs(this.no_of_extra_pts)).fill(last_new_x_pos);
  1398. var _filler_y = new Array(Math.abs(this.no_of_extra_pts)).fill(last_new_y_pos);
  1399. new_x = new_x.concat(_filler_x);
  1400. new_y = new_y.concat(_filler_y);
  1401. }
  1402. return [old_x, old_y, new_x, new_y];
  1403. }
  1404. }, {
  1405. key: 'make_anim_x_axis',
  1406. value: function make_anim_x_axis(height, text_start_at, axis_line_class) {
  1407. var _this20 = this;
  1408. // Animate X AXIS to account for more or less axis lines
  1409. var old_pos = this.x_old_axis_positions;
  1410. var new_pos = this.x_axis_positions;
  1411. var old_vals = this.old_x_values;
  1412. var new_vals = this.x;
  1413. var last_line_pos = old_pos[old_pos.length - 1];
  1414. var add_and_animate_line = function add_and_animate_line(value, old_pos, new_pos) {
  1415. if (typeof new_pos === 'string') {
  1416. new_pos = parseInt(new_pos.substring(0, new_pos.length - 1));
  1417. }
  1418. var x_line = _this20.make_x_line(height, text_start_at, value, // new value
  1419. 'x-value-text', axis_line_class, old_pos // old position
  1420. );
  1421. _this20.x_axis_group.appendChild(x_line);
  1422. _this20.elements_to_animate && _this20.elements_to_animate.push([{ unit: x_line, array: [0], index: 0 }, { transform: new_pos + ', 0' }, 350, "easein", "translate", { transform: old_pos + ', 0' }]);
  1423. };
  1424. this.x_axis_group.textContent = '';
  1425. this.make_new_axis_anim_lines(old_pos, new_pos, old_vals, new_vals, last_line_pos, add_and_animate_line);
  1426. }
  1427. }, {
  1428. key: 'make_anim_y_axis',
  1429. value: function make_anim_y_axis() {
  1430. var _this21 = this;
  1431. // Animate Y AXIS to account for more or less axis lines
  1432. var old_pos = this.y_old_axis_values.map(function (value) {
  1433. return _this21.zero_line - value * _this21.multiplier;
  1434. });
  1435. var new_pos = this.y_axis_values.map(function (value) {
  1436. return _this21.zero_line - value * _this21.multiplier;
  1437. });
  1438. var old_vals = this.y_old_axis_values;
  1439. var new_vals = this.y_axis_values;
  1440. var last_line_pos = old_pos[old_pos.length - 1];
  1441. this.y_axis_group.textContent = '';
  1442. this.make_new_axis_anim_lines(old_pos, new_pos, old_vals, new_vals, last_line_pos, this.add_and_animate_y_line.bind(this), this.y_axis_group);
  1443. }
  1444. }, {
  1445. key: 'make_anim_y_specifics',
  1446. value: function make_anim_y_specifics() {
  1447. var _this22 = this;
  1448. this.specific_y_group.textContent = '';
  1449. this.specific_values.map(function (d) {
  1450. _this22.add_and_animate_y_line(d.title, _this22.old_zero_line - d.value * _this22.old_multiplier, _this22.zero_line - d.value * _this22.multiplier, 0, _this22.specific_y_group, d.line_type, true);
  1451. });
  1452. }
  1453. }, {
  1454. key: 'make_new_axis_anim_lines',
  1455. value: function make_new_axis_anim_lines(old_pos, new_pos, old_vals, new_vals, last_line_pos, add_and_animate_line, group) {
  1456. var superimposed_positions = void 0,
  1457. superimposed_values = void 0;
  1458. var no_of_extras = new_vals.length - old_vals.length;
  1459. if (no_of_extras > 0) {
  1460. // More axis are needed
  1461. // First make only the superimposed (same position) ones
  1462. // Add in the extras at the end later
  1463. superimposed_positions = new_pos.slice(0, old_pos.length);
  1464. superimposed_values = new_vals.slice(0, old_vals.length);
  1465. } else {
  1466. // Axis have to be reduced
  1467. // Fake it by moving all current extra axis to the last position
  1468. // You'll need filler positions and values in the new arrays
  1469. var filler_vals = new Array(Math.abs(no_of_extras)).fill("");
  1470. superimposed_values = new_vals.concat(filler_vals);
  1471. var filler_pos = new Array(Math.abs(no_of_extras)).fill(last_line_pos + "F");
  1472. superimposed_positions = new_pos.concat(filler_pos);
  1473. }
  1474. superimposed_values.map(function (value, i) {
  1475. add_and_animate_line(value, old_pos[i], superimposed_positions[i], i, group);
  1476. });
  1477. if (no_of_extras > 0) {
  1478. // Add in extra axis in the end
  1479. // and then animate to new positions
  1480. var extra_values = new_vals.slice(old_vals.length);
  1481. var extra_positions = new_pos.slice(old_pos.length);
  1482. extra_values.map(function (value, i) {
  1483. add_and_animate_line(value, last_line_pos, extra_positions[i], i, group);
  1484. });
  1485. }
  1486. }
  1487. }, {
  1488. key: 'make_x_line',
  1489. value: function make_x_line(height, text_start_at, point, label_class, axis_line_class, x_pos) {
  1490. var line = $.createSVG('line', {
  1491. x1: 0,
  1492. x2: 0,
  1493. y1: 0,
  1494. y2: height
  1495. });
  1496. var text = $.createSVG('text', {
  1497. className: label_class,
  1498. x: 0,
  1499. y: text_start_at,
  1500. dy: '.71em',
  1501. innerHTML: point
  1502. });
  1503. var x_level = $.createSVG('g', {
  1504. className: 'tick ' + axis_line_class,
  1505. transform: 'translate(' + x_pos + ', 0)'
  1506. });
  1507. x_level.appendChild(line);
  1508. x_level.appendChild(text);
  1509. return x_level;
  1510. }
  1511. }, {
  1512. key: 'make_y_line',
  1513. value: function make_y_line(start_at, width, text_end_at, point, label_class, axis_line_class, y_pos) {
  1514. var darker = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : false;
  1515. var line_type = arguments.length > 8 && arguments[8] !== undefined ? arguments[8] : "";
  1516. var line = $.createSVG('line', {
  1517. className: line_type === "dashed" ? "dashed" : "",
  1518. x1: start_at,
  1519. x2: width,
  1520. y1: 0,
  1521. y2: 0
  1522. });
  1523. var text = $.createSVG('text', {
  1524. className: label_class,
  1525. x: text_end_at,
  1526. y: 0,
  1527. dy: '.32em',
  1528. innerHTML: point + ""
  1529. });
  1530. var y_level = $.createSVG('g', {
  1531. className: 'tick ' + axis_line_class,
  1532. transform: 'translate(0, ' + y_pos + ')',
  1533. 'stroke-opacity': 1
  1534. });
  1535. if (darker) {
  1536. line.style.stroke = "rgba(27, 31, 35, 0.6)";
  1537. }
  1538. y_level.appendChild(line);
  1539. y_level.appendChild(text);
  1540. return y_level;
  1541. }
  1542. }, {
  1543. key: 'add_and_animate_y_line',
  1544. value: function add_and_animate_y_line(value, old_pos, new_pos, i, group, type) {
  1545. var specific = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : false;
  1546. var filler = false;
  1547. if (typeof new_pos === 'string') {
  1548. new_pos = parseInt(new_pos.substring(0, new_pos.length - 1));
  1549. filler = true;
  1550. }
  1551. var new_props = { transform: '0, ' + new_pos };
  1552. var old_props = { transform: '0, ' + old_pos };
  1553. if (filler) {
  1554. new_props['stroke-opacity'] = 0;
  1555. // old_props['stroke-opacity'] = 1;
  1556. }
  1557. var _get_y_axis_line_prop3 = this.get_y_axis_line_props(specific),
  1558. _get_y_axis_line_prop4 = slicedToArray(_get_y_axis_line_prop3, 4),
  1559. width = _get_y_axis_line_prop4[0],
  1560. text_end_at = _get_y_axis_line_prop4[1],
  1561. axis_line_class = _get_y_axis_line_prop4[2],
  1562. start_at = _get_y_axis_line_prop4[3];
  1563. var axis_label_class = !specific ? 'y-value-text' : 'specific-value';
  1564. value = !specific ? value : (value + "").toUpperCase();
  1565. var y_line = this.make_y_line(start_at, width, text_end_at, value, axis_label_class, axis_line_class, old_pos, // old position
  1566. value === 0 && i !== 0, // Non-first Zero line
  1567. type);
  1568. group.appendChild(y_line);
  1569. this.elements_to_animate && this.elements_to_animate.push([{ unit: y_line, array: [0], index: 0 }, new_props, 350, "easein", "translate", old_props]);
  1570. }
  1571. }, {
  1572. key: 'get_y_axis_points',
  1573. value: function get_y_axis_points(array) {
  1574. var _this23 = this;
  1575. //*** Where the magic happens ***
  1576. // Calculates best-fit y intervals from given values
  1577. // and returns the interval array
  1578. // TODO: Fractions
  1579. var max_bound = void 0,
  1580. min_bound = void 0,
  1581. pos_no_of_parts = void 0,
  1582. neg_no_of_parts = void 0,
  1583. part_size = void 0; // eslint-disable-line no-unused-vars
  1584. // Critical values
  1585. var max_val = parseInt(Math.max.apply(Math, toConsumableArray(array)));
  1586. var min_val = parseInt(Math.min.apply(Math, toConsumableArray(array)));
  1587. if (min_val >= 0) {
  1588. min_val = 0;
  1589. }
  1590. var get_params = function get_params(value1, value2) {
  1591. var bound1 = void 0,
  1592. bound2 = void 0,
  1593. no_of_parts_1 = void 0,
  1594. no_of_parts_2 = void 0,
  1595. interval_size = void 0;
  1596. if ((value1 + "").length <= 1) {
  1597. bound1 = 10;
  1598. no_of_parts_1 = 5;
  1599. } else {
  1600. var _calc_upper_bound_and = _this23.calc_upper_bound_and_no_of_parts(value1);
  1601. var _calc_upper_bound_and2 = slicedToArray(_calc_upper_bound_and, 2);
  1602. bound1 = _calc_upper_bound_and2[0];
  1603. no_of_parts_1 = _calc_upper_bound_and2[1];
  1604. }
  1605. interval_size = bound1 / no_of_parts_1;
  1606. no_of_parts_2 = _this23.calc_no_of_parts(value2, interval_size);
  1607. bound2 = no_of_parts_2 * interval_size;
  1608. return [bound1, bound2, no_of_parts_1, no_of_parts_2, interval_size];
  1609. };
  1610. var abs_min_val = min_val * -1;
  1611. if (abs_min_val <= max_val) {
  1612. var _get_params = get_params(max_val, abs_min_val);
  1613. // Get the positive region intervals
  1614. // then calc negative ones accordingly
  1615. var _get_params2 = slicedToArray(_get_params, 5);
  1616. min_bound = _get_params2[1];
  1617. pos_no_of_parts = _get_params2[2];
  1618. neg_no_of_parts = _get_params2[3];
  1619. part_size = _get_params2[4];
  1620. if (abs_min_val === 0) {
  1621. min_bound = 0;neg_no_of_parts = 0;
  1622. }
  1623. } else {
  1624. var _get_params3 = get_params(abs_min_val, max_val);
  1625. // Get the negative region here first
  1626. var _get_params4 = slicedToArray(_get_params3, 5);
  1627. min_bound = _get_params4[0];
  1628. neg_no_of_parts = _get_params4[2];
  1629. pos_no_of_parts = _get_params4[3];
  1630. part_size = _get_params4[4];
  1631. }
  1632. // Make both region parts even
  1633. if (pos_no_of_parts % 2 !== 0 && neg_no_of_parts > 0) pos_no_of_parts++;
  1634. if (neg_no_of_parts % 2 !== 0) {
  1635. // every increase in no_of_parts entails an increase in corresponding bound
  1636. // except here, it happens implicitly after every calc_no_of_parts() call
  1637. neg_no_of_parts++;
  1638. min_bound += part_size;
  1639. }
  1640. var no_of_parts = pos_no_of_parts + neg_no_of_parts;
  1641. if (no_of_parts > 5) {
  1642. no_of_parts /= 2;
  1643. part_size *= 2;
  1644. pos_no_of_parts /= 2;
  1645. }
  1646. if (max_val < (pos_no_of_parts - 1) * part_size) {
  1647. no_of_parts--;
  1648. }
  1649. return this.get_intervals(-1 * min_bound, part_size, no_of_parts);
  1650. }
  1651. }, {
  1652. key: 'get_intervals',
  1653. value: function get_intervals(start, interval_size, count) {
  1654. var intervals = [];
  1655. for (var i = 0; i <= count; i++) {
  1656. intervals.push(start);
  1657. start += interval_size;
  1658. }
  1659. return intervals;
  1660. }
  1661. }, {
  1662. key: 'calc_upper_bound_and_no_of_parts',
  1663. value: function calc_upper_bound_and_no_of_parts(max_val) {
  1664. // Given a positive value, calculates a nice-number upper bound
  1665. // and a consequent optimal number of parts
  1666. var part_size = Math.pow(10, (max_val + "").length - 1);
  1667. var no_of_parts = this.calc_no_of_parts(max_val, part_size);
  1668. // Use it to get a nice even upper bound
  1669. var upper_bound = part_size * no_of_parts;
  1670. return [upper_bound, no_of_parts];
  1671. }
  1672. }, {
  1673. key: 'calc_no_of_parts',
  1674. value: function calc_no_of_parts(value, divisor) {
  1675. // value should be a positive number, divisor should be greater than 0
  1676. // returns an even no of parts
  1677. var no_of_parts = Math.ceil(value / divisor);
  1678. if (no_of_parts % 2 !== 0) no_of_parts++; // Make it an even number
  1679. return no_of_parts;
  1680. }
  1681. }, {
  1682. key: 'get_optimal_no_of_parts',
  1683. value: function get_optimal_no_of_parts(no_of_parts) {
  1684. // aka Divide by 2 if too large
  1685. return no_of_parts < 5 ? no_of_parts : no_of_parts / 2;
  1686. }
  1687. }, {
  1688. key: 'set_avg_unit_width_and_x_offset',
  1689. value: function set_avg_unit_width_and_x_offset() {
  1690. // Set the ... you get it
  1691. this.avg_unit_width = this.width / (this.x.length - 1);
  1692. this.x_offset = 0;
  1693. }
  1694. }, {
  1695. key: 'get_all_y_values',
  1696. value: function get_all_y_values() {
  1697. var all_values = [];
  1698. // Add in all the y values in the datasets
  1699. this.y.map(function (d) {
  1700. all_values = all_values.concat(d.values);
  1701. });
  1702. // Add in all the specific values
  1703. return all_values.concat(this.specific_values.map(function (d) {
  1704. return d.value;
  1705. }));
  1706. }
  1707. }, {
  1708. key: 'calc_y_dependencies',
  1709. value: function calc_y_dependencies() {
  1710. var _this24 = this;
  1711. this.y_min_tops = new Array(this.x_axis_positions.length).fill(9999);
  1712. this.y.map(function (d) {
  1713. d.y_tops = d.values.map(function (val) {
  1714. return float_2(_this24.zero_line - val * _this24.multiplier);
  1715. });
  1716. d.y_tops.map(function (y_top, i) {
  1717. if (y_top < _this24.y_min_tops[i]) {
  1718. _this24.y_min_tops[i] = y_top;
  1719. }
  1720. });
  1721. });
  1722. // this.chart_wrapper.removeChild(this.tip.container);
  1723. // this.make_tooltip();
  1724. }
  1725. }, {
  1726. key: 'get_bar_height_and_y_attr',
  1727. value: function get_bar_height_and_y_attr(y_top) {
  1728. var height = void 0,
  1729. y = void 0;
  1730. if (y_top <= this.zero_line) {
  1731. height = this.zero_line - y_top;
  1732. y = y_top;
  1733. // In case of invisible bars
  1734. if (height === 0) {
  1735. height = this.height * 0.01;
  1736. y -= height;
  1737. }
  1738. } else {
  1739. height = y_top - this.zero_line;
  1740. y = this.zero_line;
  1741. // In case of invisible bars
  1742. if (height === 0) {
  1743. height = this.height * 0.01;
  1744. }
  1745. }
  1746. return [height, y];
  1747. }
  1748. }, {
  1749. key: 'setup_utils',
  1750. value: function setup_utils() {
  1751. var _this25 = this;
  1752. this.draw = {
  1753. 'bar': function bar(x, y_top, args, color, index, dataset_index, no_of_datasets) {
  1754. var total_width = _this25.avg_unit_width - args.space_width;
  1755. var start_x = x - total_width / 2;
  1756. var width = total_width / no_of_datasets;
  1757. var current_x = start_x + width * dataset_index;
  1758. var _get_bar_height_and_y = _this25.get_bar_height_and_y_attr(y_top),
  1759. _get_bar_height_and_y2 = slicedToArray(_get_bar_height_and_y, 2),
  1760. height = _get_bar_height_and_y2[0],
  1761. y = _get_bar_height_and_y2[1];
  1762. return $.createSVG('rect', {
  1763. className: 'bar mini fill ' + color,
  1764. 'data-point-index': index,
  1765. x: current_x,
  1766. y: y,
  1767. width: width,
  1768. height: height
  1769. });
  1770. },
  1771. 'dot': function dot(x, y, args, color, index) {
  1772. return $.createSVG('circle', {
  1773. className: 'fill ' + color,
  1774. 'data-point-index': index,
  1775. cx: x,
  1776. cy: y,
  1777. r: args.radius
  1778. });
  1779. }
  1780. };
  1781. this.animate = {
  1782. 'bar': function bar(bar_obj, x, y_top, index) {
  1783. var start = x - _this25.avg_unit_width / 4;
  1784. var width = _this25.avg_unit_width / 2 / _this25.y.length;
  1785. var _get_bar_height_and_y3 = _this25.get_bar_height_and_y_attr(y_top),
  1786. _get_bar_height_and_y4 = slicedToArray(_get_bar_height_and_y3, 2),
  1787. height = _get_bar_height_and_y4[0],
  1788. y = _get_bar_height_and_y4[1];
  1789. x = start + width * index;
  1790. return [bar_obj, { width: width, height: height, x: x, y: y }, 350, "easein"];
  1791. // bar.animate({height: args.new_height, y: y_top}, 350, mina.easein);
  1792. },
  1793. 'dot': function dot(dot_obj, x, y_top) {
  1794. return [dot_obj, { cx: x, cy: y_top }, 350, "easein"];
  1795. // dot.animate({cy: y_top}, 350, mina.easein);
  1796. }
  1797. };
  1798. }
  1799. }]);
  1800. return AxisChart;
  1801. }(BaseChart);
  1802. var BarChart = function (_AxisChart) {
  1803. inherits(BarChart, _AxisChart);
  1804. function BarChart(args) {
  1805. classCallCheck(this, BarChart);
  1806. var _this = possibleConstructorReturn(this, (BarChart.__proto__ || Object.getPrototypeOf(BarChart)).call(this, args));
  1807. _this.type = 'bar';
  1808. _this.x_axis_mode = args.x_axis_mode || 'tick';
  1809. _this.y_axis_mode = args.y_axis_mode || 'span';
  1810. _this.setup();
  1811. return _this;
  1812. }
  1813. createClass(BarChart, [{
  1814. key: 'setup_values',
  1815. value: function setup_values() {
  1816. get(BarChart.prototype.__proto__ || Object.getPrototypeOf(BarChart.prototype), 'setup_values', this).call(this);
  1817. this.x_offset = this.avg_unit_width;
  1818. this.unit_args = {
  1819. type: 'bar',
  1820. args: {
  1821. space_width: this.avg_unit_width / 2
  1822. }
  1823. };
  1824. }
  1825. }, {
  1826. key: 'make_overlay',
  1827. value: function make_overlay() {
  1828. // Just make one out of the first element
  1829. var index = this.x.length - 1;
  1830. var unit = this.y[0].svg_units[index];
  1831. this.update_current_data_point(index);
  1832. if (this.overlay) {
  1833. this.overlay.parentNode.removeChild(this.overlay);
  1834. }
  1835. this.overlay = unit.cloneNode();
  1836. this.overlay.style.fill = '#000000';
  1837. this.overlay.style.opacity = '0.4';
  1838. this.draw_area.appendChild(this.overlay);
  1839. }
  1840. }, {
  1841. key: 'bind_overlay',
  1842. value: function bind_overlay() {
  1843. var _this2 = this;
  1844. // on event, update overlay
  1845. this.parent.addEventListener('data-select', function (e) {
  1846. _this2.update_overlay(e.svg_unit);
  1847. });
  1848. }
  1849. }, {
  1850. key: 'bind_units',
  1851. value: function bind_units(units_array) {
  1852. var _this3 = this;
  1853. units_array.map(function (unit) {
  1854. unit.addEventListener('click', function () {
  1855. var index = unit.getAttribute('data-point-index');
  1856. _this3.update_current_data_point(index);
  1857. });
  1858. });
  1859. }
  1860. }, {
  1861. key: 'update_overlay',
  1862. value: function update_overlay(unit) {
  1863. var _this4 = this;
  1864. var attributes = [];
  1865. Object.keys(unit.attributes).map(function (index) {
  1866. attributes.push(unit.attributes[index]);
  1867. });
  1868. attributes.filter(function (attr) {
  1869. return attr.specified;
  1870. }).map(function (attr) {
  1871. _this4.overlay.setAttribute(attr.name, attr.nodeValue);
  1872. });
  1873. }
  1874. }, {
  1875. key: 'on_left_arrow',
  1876. value: function on_left_arrow() {
  1877. this.update_current_data_point(this.current_index - 1);
  1878. }
  1879. }, {
  1880. key: 'on_right_arrow',
  1881. value: function on_right_arrow() {
  1882. this.update_current_data_point(this.current_index + 1);
  1883. }
  1884. }, {
  1885. key: 'set_avg_unit_width_and_x_offset',
  1886. value: function set_avg_unit_width_and_x_offset() {
  1887. this.avg_unit_width = this.width / (this.x.length + 1);
  1888. this.x_offset = this.avg_unit_width;
  1889. }
  1890. }]);
  1891. return BarChart;
  1892. }(AxisChart);
  1893. var LineChart = function (_AxisChart) {
  1894. inherits(LineChart, _AxisChart);
  1895. function LineChart(args) {
  1896. classCallCheck(this, LineChart);
  1897. var _this = possibleConstructorReturn(this, (LineChart.__proto__ || Object.getPrototypeOf(LineChart)).call(this, args));
  1898. _this.x_axis_mode = args.x_axis_mode || 'span';
  1899. _this.y_axis_mode = args.y_axis_mode || 'span';
  1900. if (args.hasOwnProperty('show_dots')) {
  1901. _this.show_dots = args.show_dots;
  1902. } else {
  1903. _this.show_dots = 1;
  1904. }
  1905. _this.region_fill = args.region_fill;
  1906. if (Object.getPrototypeOf(_this) !== LineChart.prototype) {
  1907. return possibleConstructorReturn(_this);
  1908. }
  1909. _this.dot_radius = args.dot_radius || 4;
  1910. _this.heatline = args.heatline;
  1911. _this.type = 'line';
  1912. _this.setup();
  1913. return _this;
  1914. }
  1915. createClass(LineChart, [{
  1916. key: 'setup_graph_components',
  1917. value: function setup_graph_components() {
  1918. this.setup_path_groups();
  1919. get(LineChart.prototype.__proto__ || Object.getPrototypeOf(LineChart.prototype), 'setup_graph_components', this).call(this);
  1920. }
  1921. }, {
  1922. key: 'setup_path_groups',
  1923. value: function setup_path_groups() {
  1924. var _this2 = this;
  1925. this.paths_groups = [];
  1926. this.y.map(function (d, i) {
  1927. _this2.paths_groups[i] = $.createSVG('g', {
  1928. className: 'path-group path-group-' + i,
  1929. inside: _this2.draw_area
  1930. });
  1931. });
  1932. }
  1933. }, {
  1934. key: 'setup_values',
  1935. value: function setup_values() {
  1936. get(LineChart.prototype.__proto__ || Object.getPrototypeOf(LineChart.prototype), 'setup_values', this).call(this);
  1937. this.unit_args = {
  1938. type: 'dot',
  1939. args: { radius: this.dot_radius }
  1940. };
  1941. }
  1942. }, {
  1943. key: 'make_new_units_for_dataset',
  1944. value: function make_new_units_for_dataset(x_values, y_values, color, dataset_index, no_of_datasets, units_group, units_array, unit) {
  1945. if (this.show_dots) {
  1946. get(LineChart.prototype.__proto__ || Object.getPrototypeOf(LineChart.prototype), 'make_new_units_for_dataset', this).call(this, x_values, y_values, color, dataset_index, no_of_datasets, units_group, units_array, unit);
  1947. }
  1948. }
  1949. }, {
  1950. key: 'make_paths',
  1951. value: function make_paths() {
  1952. var _this3 = this;
  1953. this.y.map(function (d, i) {
  1954. _this3.make_path(d, i, _this3.x_axis_positions, d.y_tops, d.color || _this3.colors[i]);
  1955. });
  1956. }
  1957. }, {
  1958. key: 'make_path',
  1959. value: function make_path(d, i, x_positions, y_positions, color) {
  1960. var points_list = y_positions.map(function (y, i) {
  1961. return x_positions[i] + ',' + y;
  1962. });
  1963. var points_str = points_list.join("L");
  1964. this.paths_groups[i].textContent = '';
  1965. d.path = $.createSVG('path', {
  1966. inside: this.paths_groups[i],
  1967. className: 'stroke ' + color,
  1968. d: "M" + points_str
  1969. });
  1970. if (this.heatline) {
  1971. var gradient_id = this.make_gradient(color);
  1972. d.path.style.stroke = 'url(#' + gradient_id + ')';
  1973. }
  1974. if (this.region_fill) {
  1975. this.fill_region_for_dataset(d, i, color, points_str);
  1976. }
  1977. }
  1978. }, {
  1979. key: 'fill_region_for_dataset',
  1980. value: function fill_region_for_dataset(d, i, color, points_str) {
  1981. var gradient_id = this.make_gradient(color, true);
  1982. d.region_path = $.createSVG('path', {
  1983. inside: this.paths_groups[i],
  1984. className: 'region-fill',
  1985. d: "M" + ('0,' + this.zero_line + 'L') + points_str + ('L' + this.width + ',' + this.zero_line)
  1986. });
  1987. d.region_path.style.stroke = "none";
  1988. d.region_path.style.fill = 'url(#' + gradient_id + ')';
  1989. }
  1990. }, {
  1991. key: 'make_gradient',
  1992. value: function make_gradient(color) {
  1993. var lighter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  1994. var gradient_id = 'path-fill-gradient' + '-' + color;
  1995. var gradient_def = $.createSVG('linearGradient', {
  1996. inside: this.svg_defs,
  1997. id: gradient_id,
  1998. x1: 0,
  1999. x2: 0,
  2000. y1: 0,
  2001. y2: 1
  2002. });
  2003. var set_gradient_stop = function set_gradient_stop(grad_elem, offset, color, opacity) {
  2004. $.createSVG('stop', {
  2005. 'className': 'stop-color ' + color,
  2006. 'inside': grad_elem,
  2007. 'offset': offset,
  2008. 'stop-opacity': opacity
  2009. });
  2010. };
  2011. var opacities = [1, 0.6, 0.2];
  2012. if (lighter) {
  2013. opacities = [0.4, 0.2, 0];
  2014. }
  2015. set_gradient_stop(gradient_def, "0%", color, opacities[0]);
  2016. set_gradient_stop(gradient_def, "50%", color, opacities[1]);
  2017. set_gradient_stop(gradient_def, "100%", color, opacities[2]);
  2018. return gradient_id;
  2019. }
  2020. }]);
  2021. return LineChart;
  2022. }(AxisChart);
  2023. var ScatterChart = function (_LineChart) {
  2024. inherits(ScatterChart, _LineChart);
  2025. function ScatterChart(args) {
  2026. classCallCheck(this, ScatterChart);
  2027. var _this = possibleConstructorReturn(this, (ScatterChart.__proto__ || Object.getPrototypeOf(ScatterChart)).call(this, args));
  2028. _this.type = 'scatter';
  2029. if (!args.dot_radius) {
  2030. _this.dot_radius = 8;
  2031. } else {
  2032. _this.dot_radius = args.dot_radius;
  2033. }
  2034. _this.setup();
  2035. return _this;
  2036. }
  2037. createClass(ScatterChart, [{
  2038. key: 'setup_graph_components',
  2039. value: function setup_graph_components() {
  2040. this.setup_path_groups();
  2041. get(ScatterChart.prototype.__proto__ || Object.getPrototypeOf(ScatterChart.prototype), 'setup_graph_components', this).call(this);
  2042. }
  2043. }, {
  2044. key: 'setup_path_groups',
  2045. value: function setup_path_groups() {}
  2046. }, {
  2047. key: 'setup_values',
  2048. value: function setup_values() {
  2049. get(ScatterChart.prototype.__proto__ || Object.getPrototypeOf(ScatterChart.prototype), 'setup_values', this).call(this);
  2050. this.unit_args = {
  2051. type: 'dot',
  2052. args: { radius: this.dot_radius }
  2053. };
  2054. }
  2055. }, {
  2056. key: 'make_paths',
  2057. value: function make_paths() {}
  2058. }, {
  2059. key: 'make_path',
  2060. value: function make_path() {}
  2061. }]);
  2062. return ScatterChart;
  2063. }(LineChart);
  2064. var PercentageChart = function (_BaseChart) {
  2065. inherits(PercentageChart, _BaseChart);
  2066. function PercentageChart(args) {
  2067. classCallCheck(this, PercentageChart);
  2068. var _this = possibleConstructorReturn(this, (PercentageChart.__proto__ || Object.getPrototypeOf(PercentageChart)).call(this, args));
  2069. _this.type = 'percentage';
  2070. _this.get_y_label = _this.format_lambdas.y_label;
  2071. _this.get_x_tooltip = _this.format_lambdas.x_tooltip;
  2072. _this.get_y_tooltip = _this.format_lambdas.y_tooltip;
  2073. _this.max_slices = 10;
  2074. _this.max_legend_points = 6;
  2075. _this.colors = args.colors;
  2076. if (!_this.colors || _this.colors.length < _this.data.labels.length) {
  2077. _this.colors = ['light-blue', 'blue', 'violet', 'red', 'orange', 'yellow', 'green', 'light-green', 'purple', 'magenta'];
  2078. }
  2079. _this.setup();
  2080. return _this;
  2081. }
  2082. createClass(PercentageChart, [{
  2083. key: 'make_chart_area',
  2084. value: function make_chart_area() {
  2085. this.chart_wrapper.className += ' ' + 'graph-focus-margin';
  2086. this.chart_wrapper.style.marginTop = '45px';
  2087. this.stats_wrapper.className += ' ' + 'graph-focus-margin';
  2088. this.stats_wrapper.style.marginBottom = '30px';
  2089. this.stats_wrapper.style.paddingTop = '0px';
  2090. }
  2091. }, {
  2092. key: 'make_draw_area',
  2093. value: function make_draw_area() {
  2094. this.chart_div = $.create('div', {
  2095. className: 'div',
  2096. inside: this.chart_wrapper
  2097. });
  2098. this.chart = $.create('div', {
  2099. className: 'progress-chart',
  2100. inside: this.chart_div
  2101. });
  2102. }
  2103. }, {
  2104. key: 'setup_components',
  2105. value: function setup_components() {
  2106. this.percentage_bar = $.create('div', {
  2107. className: 'progress',
  2108. inside: this.chart
  2109. });
  2110. }
  2111. }, {
  2112. key: 'setup_values',
  2113. value: function setup_values() {
  2114. var _this2 = this;
  2115. this.slice_totals = [];
  2116. var all_totals = this.data.labels.map(function (d, i) {
  2117. var total = 0;
  2118. _this2.data.datasets.map(function (e) {
  2119. total += e.values[i];
  2120. });
  2121. return [total, d];
  2122. }).filter(function (d) {
  2123. return d[0] > 0;
  2124. }); // keep only positive results
  2125. var totals = all_totals;
  2126. if (all_totals.length > this.max_slices) {
  2127. all_totals.sort(function (a, b) {
  2128. return b[0] - a[0];
  2129. });
  2130. totals = all_totals.slice(0, this.max_slices - 1);
  2131. var others = all_totals.slice(this.max_slices - 1);
  2132. var sum_of_others = 0;
  2133. others.map(function (d) {
  2134. sum_of_others += d[0];
  2135. });
  2136. totals.push([sum_of_others, 'Rest']);
  2137. this.colors[this.max_slices - 1] = 'grey';
  2138. }
  2139. this.labels = [];
  2140. totals.map(function (d) {
  2141. _this2.slice_totals.push(d[0]);
  2142. _this2.labels.push(d[1]);
  2143. });
  2144. this.legend_totals = this.slice_totals.slice(0, this.max_legend_points);
  2145. }
  2146. }, {
  2147. key: 'setup_utils',
  2148. value: function setup_utils() {}
  2149. }, {
  2150. key: 'make_graph_components',
  2151. value: function make_graph_components() {
  2152. var _this3 = this;
  2153. this.grand_total = this.slice_totals.reduce(function (a, b) {
  2154. return a + b;
  2155. }, 0);
  2156. this.slices = [];
  2157. this.slice_totals.map(function (total, i) {
  2158. var slice = $.create('div', {
  2159. className: 'progress-bar background ' + _this3.colors[i],
  2160. inside: _this3.percentage_bar,
  2161. styles: {
  2162. width: total * 100 / _this3.grand_total + "%"
  2163. }
  2164. });
  2165. _this3.slices.push(slice);
  2166. });
  2167. }
  2168. }, {
  2169. key: 'bind_tooltip',
  2170. value: function bind_tooltip() {
  2171. var _this4 = this;
  2172. this.slices.map(function (slice, i) {
  2173. slice.addEventListener('mouseenter', function () {
  2174. var g_off = $.offset(_this4.chart_wrapper),
  2175. p_off = $.offset(slice);
  2176. var x = p_off.left - g_off.left + slice.offsetWidth / 2;
  2177. var y = p_off.top - g_off.top - 6;
  2178. var title = (_this4.formatted_labels && _this4.formatted_labels.length > 0 ? _this4.formatted_labels[i] : _this4.labels[i]) + ': ';
  2179. var percent = (_this4.slice_totals[i] * 100 / _this4.grand_total).toFixed(1);
  2180. _this4.tip.set_values(x, y, title, percent + "%");
  2181. _this4.tip.show_tip();
  2182. });
  2183. });
  2184. }
  2185. }, {
  2186. key: 'show_summary',
  2187. value: function show_summary() {
  2188. var _this5 = this;
  2189. var x_values = this.formatted_labels && this.formatted_labels.length > 0 ? this.formatted_labels : this.labels;
  2190. this.legend_totals.map(function (d, i) {
  2191. if (d) {
  2192. var stats = $.create('div', {
  2193. className: 'stats',
  2194. inside: _this5.stats_wrapper
  2195. });
  2196. stats.innerHTML = '<span class="indicator ' + _this5.colors[i] + '">\n\t\t\t\t\t<span class="text-muted">' + x_values[i] + ':</span>\n\t\t\t\t\t' + d + '\n\t\t\t\t</span>';
  2197. }
  2198. });
  2199. }
  2200. }]);
  2201. return PercentageChart;
  2202. }(BaseChart);
  2203. var ANGLE_RATIO = Math.PI / 180;
  2204. var FULL_ANGLE = 360;
  2205. var PieChart = function (_BaseChart) {
  2206. inherits(PieChart, _BaseChart);
  2207. function PieChart(args) {
  2208. classCallCheck(this, PieChart);
  2209. var _this = possibleConstructorReturn(this, (PieChart.__proto__ || Object.getPrototypeOf(PieChart)).call(this, args));
  2210. _this.type = 'pie';
  2211. _this.get_y_label = _this.format_lambdas.y_label;
  2212. _this.get_x_tooltip = _this.format_lambdas.x_tooltip;
  2213. _this.get_y_tooltip = _this.format_lambdas.y_tooltip;
  2214. _this.elements_to_animate = null;
  2215. _this.hoverRadio = args.hoverRadio || 0.1;
  2216. _this.max_slices = 10;
  2217. _this.max_legend_points = 6;
  2218. _this.isAnimate = false;
  2219. _this.colors = args.colors;
  2220. _this.startAngle = args.startAngle || 0;
  2221. _this.clockWise = args.clockWise || false;
  2222. if (!_this.colors || _this.colors.length < _this.data.labels.length) {
  2223. _this.colors = ['#7cd6fd', '#5e64ff', '#743ee2', '#ff5858', '#ffa00a', '#FEEF72', '#28a745', '#98d85b', '#b554ff', '#ffa3ef'];
  2224. }
  2225. _this.mouseMove = _this.mouseMove.bind(_this);
  2226. _this.mouseLeave = _this.mouseLeave.bind(_this);
  2227. _this.setup();
  2228. return _this;
  2229. }
  2230. createClass(PieChart, [{
  2231. key: 'setup_values',
  2232. value: function setup_values() {
  2233. var _this2 = this;
  2234. this.centerX = this.width / 2;
  2235. this.centerY = this.height / 2;
  2236. this.radius = this.height > this.width ? this.centerX : this.centerY;
  2237. this.slice_totals = [];
  2238. var all_totals = this.data.labels.map(function (d, i) {
  2239. var total = 0;
  2240. _this2.data.datasets.map(function (e) {
  2241. total += e.values[i];
  2242. });
  2243. return [total, d];
  2244. }).filter(function (d) {
  2245. return d[0] > 0;
  2246. }); // keep only positive results
  2247. var totals = all_totals;
  2248. if (all_totals.length > this.max_slices) {
  2249. all_totals.sort(function (a, b) {
  2250. return b[0] - a[0];
  2251. });
  2252. totals = all_totals.slice(0, this.max_slices - 1);
  2253. var others = all_totals.slice(this.max_slices - 1);
  2254. var sum_of_others = 0;
  2255. others.map(function (d) {
  2256. sum_of_others += d[0];
  2257. });
  2258. totals.push([sum_of_others, 'Rest']);
  2259. this.colors[this.max_slices - 1] = 'grey';
  2260. }
  2261. this.labels = [];
  2262. totals.map(function (d) {
  2263. _this2.slice_totals.push(d[0]);
  2264. _this2.labels.push(d[1]);
  2265. });
  2266. this.legend_totals = this.slice_totals.slice(0, this.max_legend_points);
  2267. }
  2268. }, {
  2269. key: 'setup_utils',
  2270. value: function setup_utils() {}
  2271. }, {
  2272. key: 'makeArcPath',
  2273. value: function makeArcPath(startPosition, endPosition) {
  2274. var centerX = this.centerX,
  2275. centerY = this.centerY,
  2276. radius = this.radius,
  2277. clockWise = this.clockWise;
  2278. return 'M' + centerX + ' ' + centerY + ' L' + (centerX + startPosition.x) + ' ' + (centerY + startPosition.y) + ' A ' + radius + ' ' + radius + ' 0 0 ' + (clockWise ? 1 : 0) + ' ' + (centerX + endPosition.x) + ' ' + (centerY + endPosition.y) + ' z';
  2279. }
  2280. }, {
  2281. key: 'make_graph_components',
  2282. value: function make_graph_components(init) {
  2283. var _this3 = this;
  2284. var radius = this.radius,
  2285. clockWise = this.clockWise;
  2286. this.grand_total = this.slice_totals.reduce(function (a, b) {
  2287. return a + b;
  2288. }, 0);
  2289. var prevSlicesProperties = this.slicesProperties || [];
  2290. this.slices = [];
  2291. this.elements_to_animate = [];
  2292. this.slicesProperties = [];
  2293. var curAngle = 180 - this.startAngle;
  2294. this.slice_totals.map(function (total, i) {
  2295. var startAngle = curAngle;
  2296. var originDiffAngle = total / _this3.grand_total * FULL_ANGLE;
  2297. var diffAngle = clockWise ? -originDiffAngle : originDiffAngle;
  2298. var endAngle = curAngle = curAngle + diffAngle;
  2299. var startPosition = PieChart.getPositionByAngle(startAngle, radius);
  2300. var endPosition = PieChart.getPositionByAngle(endAngle, radius);
  2301. var prevProperty = init && prevSlicesProperties[i];
  2302. var curStart = void 0,
  2303. curEnd = void 0;
  2304. if (init) {
  2305. curStart = prevProperty ? prevProperty.startPosition : startPosition;
  2306. curEnd = prevProperty ? prevProperty.endPosition : startPosition;
  2307. } else {
  2308. curStart = startPosition;
  2309. curEnd = endPosition;
  2310. }
  2311. var curPath = _this3.makeArcPath(curStart, curEnd);
  2312. var slice = $.createSVG('path', {
  2313. inside: _this3.draw_area,
  2314. className: 'pie-path',
  2315. style: 'transition:transform .3s;',
  2316. d: curPath,
  2317. fill: _this3.colors[i]
  2318. });
  2319. _this3.slices.push(slice);
  2320. _this3.slicesProperties.push({
  2321. startPosition: startPosition,
  2322. endPosition: endPosition,
  2323. value: total,
  2324. total: _this3.grand_total,
  2325. startAngle: startAngle,
  2326. endAngle: endAngle,
  2327. angle: diffAngle
  2328. });
  2329. if (init) {
  2330. _this3.elements_to_animate.push([{ unit: slice, array: _this3.slices, index: _this3.slices.length - 1 }, { d: _this3.makeArcPath(startPosition, endPosition) }, 650, "easein", null, {
  2331. d: curPath
  2332. }]);
  2333. }
  2334. });
  2335. if (init) {
  2336. this.run_animation();
  2337. }
  2338. }
  2339. }, {
  2340. key: 'run_animation',
  2341. value: function run_animation() {
  2342. var _this4 = this;
  2343. // if(this.isAnimate) return ;
  2344. // this.isAnimate = true;
  2345. if (!this.elements_to_animate || this.elements_to_animate.length === 0) return;
  2346. var anim_svg = $.runSVGAnimation(this.svg, this.elements_to_animate);
  2347. if (this.svg.parentNode == this.chart_wrapper) {
  2348. this.chart_wrapper.removeChild(this.svg);
  2349. this.chart_wrapper.appendChild(anim_svg);
  2350. }
  2351. // Replace the new svg (data has long been replaced)
  2352. setTimeout(function () {
  2353. // this.isAnimate = false;
  2354. if (anim_svg.parentNode == _this4.chart_wrapper) {
  2355. _this4.chart_wrapper.removeChild(anim_svg);
  2356. _this4.chart_wrapper.appendChild(_this4.svg);
  2357. }
  2358. }, 650);
  2359. }
  2360. }, {
  2361. key: 'calTranslateByAngle',
  2362. value: function calTranslateByAngle(property) {
  2363. var radius = this.radius,
  2364. hoverRadio = this.hoverRadio;
  2365. var position = PieChart.getPositionByAngle(property.startAngle + property.angle / 2, radius);
  2366. return 'translate3d(' + position.x * hoverRadio + 'px,' + position.y * hoverRadio + 'px,0)';
  2367. }
  2368. }, {
  2369. key: 'hoverSlice',
  2370. value: function hoverSlice(path, i, flag, e) {
  2371. if (!path) return;
  2372. if (flag) {
  2373. $.transform(path, this.calTranslateByAngle(this.slicesProperties[i]));
  2374. path.setAttribute('fill', lightenDarkenColor(this.colors[i], 50));
  2375. var g_off = $.offset(this.svg);
  2376. var x = e.pageX - g_off.left + 10;
  2377. var y = e.pageY - g_off.top - 10;
  2378. var title = (this.formatted_labels && this.formatted_labels.length > 0 ? this.formatted_labels[i] : this.labels[i]) + ': ';
  2379. var percent = (this.slice_totals[i] * 100 / this.grand_total).toFixed(1);
  2380. this.tip.set_values(x, y, title, percent + "%");
  2381. this.tip.show_tip();
  2382. } else {
  2383. $.transform(path, 'translate3d(0,0,0)');
  2384. this.tip.hide_tip();
  2385. path.setAttribute('fill', this.colors[i]);
  2386. }
  2387. }
  2388. }, {
  2389. key: 'mouseMove',
  2390. value: function mouseMove(e) {
  2391. var target = e.target;
  2392. var prevIndex = this.curActiveSliceIndex;
  2393. var prevAcitve = this.curActiveSlice;
  2394. for (var i = 0; i < this.slices.length; i++) {
  2395. if (target === this.slices[i]) {
  2396. this.hoverSlice(prevAcitve, prevIndex, false);
  2397. this.curActiveSlice = target;
  2398. this.curActiveSliceIndex = i;
  2399. this.hoverSlice(target, i, true, e);
  2400. break;
  2401. }
  2402. }
  2403. }
  2404. }, {
  2405. key: 'mouseLeave',
  2406. value: function mouseLeave() {
  2407. this.hoverSlice(this.curActiveSlice, this.curActiveSliceIndex, false);
  2408. }
  2409. }, {
  2410. key: 'bind_tooltip',
  2411. value: function bind_tooltip() {
  2412. this.draw_area.addEventListener('mousemove', this.mouseMove);
  2413. this.draw_area.addEventListener('mouseleave', this.mouseLeave);
  2414. }
  2415. }, {
  2416. key: 'show_summary',
  2417. value: function show_summary() {
  2418. var _this5 = this;
  2419. var x_values = this.formatted_labels && this.formatted_labels.length > 0 ? this.formatted_labels : this.labels;
  2420. this.legend_totals.map(function (d, i) {
  2421. if (d) {
  2422. var stats = $.create('div', {
  2423. className: 'stats',
  2424. inside: _this5.stats_wrapper
  2425. });
  2426. stats.innerHTML = '<span class="indicator">\n\t\t\t\t\t<i style="background-color:' + _this5.colors[i] + ';"></i>\n\t\t\t\t\t<span class="text-muted">' + x_values[i] + ':</span>\n\t\t\t\t\t' + d + '\n\t\t\t\t</span>';
  2427. }
  2428. });
  2429. }
  2430. }], [{
  2431. key: 'getPositionByAngle',
  2432. value: function getPositionByAngle(angle, radius) {
  2433. return {
  2434. x: Math.sin(angle * ANGLE_RATIO) * radius,
  2435. y: Math.cos(angle * ANGLE_RATIO) * radius
  2436. };
  2437. }
  2438. }]);
  2439. return PieChart;
  2440. }(BaseChart);
  2441. var Heatmap = function (_BaseChart) {
  2442. inherits(Heatmap, _BaseChart);
  2443. function Heatmap(_ref) {
  2444. var _ref$start = _ref.start,
  2445. start = _ref$start === undefined ? '' : _ref$start,
  2446. _ref$domain = _ref.domain,
  2447. domain = _ref$domain === undefined ? '' : _ref$domain,
  2448. _ref$subdomain = _ref.subdomain,
  2449. subdomain = _ref$subdomain === undefined ? '' : _ref$subdomain,
  2450. _ref$data = _ref.data,
  2451. data = _ref$data === undefined ? {} : _ref$data,
  2452. _ref$discrete_domains = _ref.discrete_domains,
  2453. discrete_domains = _ref$discrete_domains === undefined ? 0 : _ref$discrete_domains,
  2454. _ref$count_label = _ref.count_label,
  2455. count_label = _ref$count_label === undefined ? '' : _ref$count_label;
  2456. classCallCheck(this, Heatmap);
  2457. var _this = possibleConstructorReturn(this, (Heatmap.__proto__ || Object.getPrototypeOf(Heatmap)).call(this, arguments[0]));
  2458. _this.type = 'heatmap';
  2459. _this.domain = domain;
  2460. _this.subdomain = subdomain;
  2461. _this.data = data;
  2462. _this.discrete_domains = discrete_domains;
  2463. _this.count_label = count_label;
  2464. var today = new Date();
  2465. _this.start = start || _this.add_days(today, 365);
  2466. _this.legend_colors = ['#ebedf0', '#c6e48b', '#7bc96f', '#239a3b', '#196127'];
  2467. _this.translate_x = 0;
  2468. _this.setup();
  2469. return _this;
  2470. }
  2471. createClass(Heatmap, [{
  2472. key: 'setup_base_values',
  2473. value: function setup_base_values() {
  2474. this.today = new Date();
  2475. if (!this.start) {
  2476. this.start = new Date();
  2477. this.start.setFullYear(this.start.getFullYear() - 1);
  2478. }
  2479. this.first_week_start = new Date(this.start.toDateString());
  2480. this.last_week_start = new Date(this.today.toDateString());
  2481. if (this.first_week_start.getDay() !== 7) {
  2482. this.add_days(this.first_week_start, -1 * this.first_week_start.getDay());
  2483. }
  2484. if (this.last_week_start.getDay() !== 7) {
  2485. this.add_days(this.last_week_start, -1 * this.last_week_start.getDay());
  2486. }
  2487. this.no_of_cols = this.get_weeks_between(this.first_week_start + '', this.last_week_start + '') + 1;
  2488. }
  2489. }, {
  2490. key: 'set_width',
  2491. value: function set_width() {
  2492. this.base_width = (this.no_of_cols + 3) * 12;
  2493. if (this.discrete_domains) {
  2494. this.base_width += 12 * 12;
  2495. }
  2496. }
  2497. }, {
  2498. key: 'setup_components',
  2499. value: function setup_components() {
  2500. this.domain_label_group = $.createSVG("g", {
  2501. className: "domain-label-group chart-label",
  2502. inside: this.draw_area
  2503. });
  2504. this.data_groups = $.createSVG("g", {
  2505. className: "data-groups",
  2506. inside: this.draw_area,
  2507. transform: 'translate(0, 20)'
  2508. });
  2509. }
  2510. }, {
  2511. key: 'setup_values',
  2512. value: function setup_values() {
  2513. this.domain_label_group.textContent = '';
  2514. this.data_groups.textContent = '';
  2515. this.distribution = this.get_distribution(this.data, this.legend_colors);
  2516. this.month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  2517. this.render_all_weeks_and_store_x_values(this.no_of_cols);
  2518. }
  2519. }, {
  2520. key: 'render_all_weeks_and_store_x_values',
  2521. value: function render_all_weeks_and_store_x_values(no_of_weeks) {
  2522. var current_week_sunday = new Date(this.first_week_start);
  2523. this.week_col = 0;
  2524. this.current_month = current_week_sunday.getMonth();
  2525. this.months = [this.current_month + ''];
  2526. this.month_weeks = {}, this.month_start_points = [];
  2527. this.month_weeks[this.current_month] = 0;
  2528. this.month_start_points.push(13);
  2529. for (var i = 0; i < no_of_weeks; i++) {
  2530. var data_group = void 0,
  2531. month_change = 0;
  2532. var day = new Date(current_week_sunday);
  2533. var _get_week_squares_gro = this.get_week_squares_group(day, this.week_col);
  2534. var _get_week_squares_gro2 = slicedToArray(_get_week_squares_gro, 2);
  2535. data_group = _get_week_squares_gro2[0];
  2536. month_change = _get_week_squares_gro2[1];
  2537. this.data_groups.appendChild(data_group);
  2538. this.week_col += 1 + parseInt(this.discrete_domains && month_change);
  2539. this.month_weeks[this.current_month]++;
  2540. if (month_change) {
  2541. this.current_month = (this.current_month + 1) % 12;
  2542. this.months.push(this.current_month + '');
  2543. this.month_weeks[this.current_month] = 1;
  2544. }
  2545. this.add_days(current_week_sunday, 7);
  2546. }
  2547. this.render_month_labels();
  2548. }
  2549. }, {
  2550. key: 'get_week_squares_group',
  2551. value: function get_week_squares_group(current_date, index) {
  2552. var no_of_weekdays = 7;
  2553. var square_side = 10;
  2554. var cell_padding = 2;
  2555. var step = 1;
  2556. var today_time = this.today.getTime();
  2557. var month_change = 0;
  2558. var week_col_change = 0;
  2559. var data_group = $.createSVG("g", {
  2560. className: "data-group",
  2561. inside: this.data_groups
  2562. });
  2563. for (var y = 0, i = 0; i < no_of_weekdays; i += step, y += square_side + cell_padding) {
  2564. var data_value = 0;
  2565. var color_index = 0;
  2566. var current_timestamp = current_date.getTime() / 1000;
  2567. var timestamp = Math.floor(current_timestamp - current_timestamp % 86400).toFixed(1);
  2568. if (this.data[timestamp]) {
  2569. data_value = this.data[timestamp];
  2570. color_index = this.get_max_checkpoint(data_value, this.distribution);
  2571. }
  2572. if (this.data[Math.round(timestamp)]) {
  2573. data_value = this.data[Math.round(timestamp)];
  2574. color_index = this.get_max_checkpoint(data_value, this.distribution);
  2575. }
  2576. var x = 13 + (index + week_col_change) * 12;
  2577. $.createSVG("rect", {
  2578. className: 'day',
  2579. inside: data_group,
  2580. x: x,
  2581. y: y,
  2582. width: square_side,
  2583. height: square_side,
  2584. fill: this.legend_colors[color_index],
  2585. 'data-date': this.get_dd_mm_yyyy(current_date),
  2586. 'data-value': data_value,
  2587. 'data-day': current_date.getDay()
  2588. });
  2589. var next_date = new Date(current_date);
  2590. this.add_days(next_date, 1);
  2591. if (next_date.getTime() > today_time) break;
  2592. if (next_date.getMonth() - current_date.getMonth()) {
  2593. month_change = 1;
  2594. if (this.discrete_domains) {
  2595. week_col_change = 1;
  2596. }
  2597. this.month_start_points.push(13 + (index + week_col_change) * 12);
  2598. }
  2599. current_date = next_date;
  2600. }
  2601. return [data_group, month_change];
  2602. }
  2603. }, {
  2604. key: 'render_month_labels',
  2605. value: function render_month_labels() {
  2606. var _this2 = this;
  2607. // this.first_month_label = 1;
  2608. // if (this.first_week_start.getDate() > 8) {
  2609. // this.first_month_label = 0;
  2610. // }
  2611. // this.last_month_label = 1;
  2612. // let first_month = this.months.shift();
  2613. // let first_month_start = this.month_start_points.shift();
  2614. // render first month if
  2615. // let last_month = this.months.pop();
  2616. // let last_month_start = this.month_start_points.pop();
  2617. // render last month if
  2618. this.months.shift();
  2619. this.month_start_points.shift();
  2620. this.months.pop();
  2621. this.month_start_points.pop();
  2622. this.month_start_points.map(function (start, i) {
  2623. var month_name = _this2.month_names[_this2.months[i]].substring(0, 3);
  2624. $.createSVG('text', {
  2625. className: 'y-value-text',
  2626. inside: _this2.domain_label_group,
  2627. x: start + 12,
  2628. y: 10,
  2629. dy: '.32em',
  2630. innerHTML: month_name
  2631. });
  2632. });
  2633. }
  2634. }, {
  2635. key: 'make_graph_components',
  2636. value: function make_graph_components() {
  2637. Array.prototype.slice.call(this.container.querySelectorAll('.graph-stats-container, .sub-title, .title')).map(function (d) {
  2638. d.style.display = 'None';
  2639. });
  2640. this.chart_wrapper.style.marginTop = '0px';
  2641. this.chart_wrapper.style.paddingTop = '0px';
  2642. }
  2643. }, {
  2644. key: 'bind_tooltip',
  2645. value: function bind_tooltip() {
  2646. var _this3 = this;
  2647. Array.prototype.slice.call(document.querySelectorAll(".data-group .day")).map(function (el) {
  2648. el.addEventListener('mouseenter', function (e) {
  2649. var count = e.target.getAttribute('data-value');
  2650. var date_parts = e.target.getAttribute('data-date').split('-');
  2651. var month = _this3.month_names[parseInt(date_parts[1]) - 1].substring(0, 3);
  2652. var g_off = _this3.chart_wrapper.getBoundingClientRect(),
  2653. p_off = e.target.getBoundingClientRect();
  2654. var width = parseInt(e.target.getAttribute('width'));
  2655. var x = p_off.left - g_off.left + (width + 2) / 2;
  2656. var y = p_off.top - g_off.top - (width + 2) / 2;
  2657. var value = count + ' ' + _this3.count_label;
  2658. var name = ' on ' + month + ' ' + date_parts[0] + ', ' + date_parts[2];
  2659. _this3.tip.set_values(x, y, name, value, [], 1);
  2660. _this3.tip.show_tip();
  2661. });
  2662. });
  2663. }
  2664. }, {
  2665. key: 'update',
  2666. value: function update(data) {
  2667. this.data = data;
  2668. this.setup_values();
  2669. this.bind_tooltip();
  2670. }
  2671. }, {
  2672. key: 'get_distribution',
  2673. value: function get_distribution() {
  2674. var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  2675. var mapper_array = arguments[1];
  2676. var data_values = Object.keys(data).map(function (key) {
  2677. return data[key];
  2678. });
  2679. var data_max_value = Math.max.apply(Math, toConsumableArray(data_values));
  2680. var distribution_step = 1 / (mapper_array.length - 1);
  2681. var distribution = [];
  2682. mapper_array.map(function (color, i) {
  2683. var checkpoint = data_max_value * (distribution_step * i);
  2684. distribution.push(checkpoint);
  2685. });
  2686. return distribution;
  2687. }
  2688. }, {
  2689. key: 'get_max_checkpoint',
  2690. value: function get_max_checkpoint(value, distribution) {
  2691. return distribution.filter(function (d, i) {
  2692. if (i === 1) {
  2693. return distribution[0] < value;
  2694. }
  2695. return d <= value;
  2696. }).length - 1;
  2697. }
  2698. // TODO: date utils, move these out
  2699. // https://stackoverflow.com/a/11252167/6495043
  2700. }, {
  2701. key: 'treat_as_utc',
  2702. value: function treat_as_utc(date_str) {
  2703. var result = new Date(date_str);
  2704. result.setMinutes(result.getMinutes() - result.getTimezoneOffset());
  2705. return result;
  2706. }
  2707. }, {
  2708. key: 'get_dd_mm_yyyy',
  2709. value: function get_dd_mm_yyyy(date) {
  2710. var dd = date.getDate();
  2711. var mm = date.getMonth() + 1; // getMonth() is zero-based
  2712. return [(dd > 9 ? '' : '0') + dd, (mm > 9 ? '' : '0') + mm, date.getFullYear()].join('-');
  2713. }
  2714. }, {
  2715. key: 'get_weeks_between',
  2716. value: function get_weeks_between(start_date_str, end_date_str) {
  2717. return Math.ceil(this.get_days_between(start_date_str, end_date_str) / 7);
  2718. }
  2719. }, {
  2720. key: 'get_days_between',
  2721. value: function get_days_between(start_date_str, end_date_str) {
  2722. var milliseconds_per_day = 24 * 60 * 60 * 1000;
  2723. return (this.treat_as_utc(end_date_str) - this.treat_as_utc(start_date_str)) / milliseconds_per_day;
  2724. }
  2725. // mutates
  2726. }, {
  2727. key: 'add_days',
  2728. value: function add_days(date, number_of_days) {
  2729. date.setDate(date.getDate() + number_of_days);
  2730. }
  2731. }, {
  2732. key: 'get_month_name',
  2733. value: function get_month_name() {}
  2734. }]);
  2735. return Heatmap;
  2736. }(BaseChart);
  2737. var chartTypes = {
  2738. line: LineChart,
  2739. bar: BarChart,
  2740. scatter: ScatterChart,
  2741. percentage: PercentageChart,
  2742. heatmap: Heatmap,
  2743. pie: PieChart
  2744. };
  2745. function getChartByType() {
  2746. var chartType = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'line';
  2747. var options = arguments[1];
  2748. if (!chartTypes[chartType]) {
  2749. return new LineChart(options);
  2750. }
  2751. return new chartTypes[chartType](options);
  2752. }
  2753. var Chart = function Chart(args) {
  2754. classCallCheck(this, Chart);
  2755. return getChartByType(args.type, arguments[0]);
  2756. };
  2757. module.exports = Chart;