Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

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