Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

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