Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

3271 wiersze
90 KiB

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