vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1320 lines
41 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. var util = require('../../util');
  2. var DOMutil = require('../../DOMutil');
  3. var DataSet = require('../../DataSet');
  4. var DataView = require('../../DataView');
  5. var Component = require('./Component');
  6. var DataAxis = require('./DataAxis');
  7. var GraphGroup = require('./GraphGroup');
  8. var Legend = require('./Legend');
  9. var UNGROUPED = '__ungrouped__'; // reserved group id for ungrouped items
  10. /**
  11. * This is the constructor of the LineGraph. It requires a Timeline body and options.
  12. *
  13. * @param body
  14. * @param options
  15. * @constructor
  16. */
  17. function LineGraph(body, options) {
  18. this.id = util.randomUUID();
  19. this.body = body;
  20. this.defaultOptions = {
  21. yAxisOrientation: 'left',
  22. defaultGroup: 'default',
  23. sort: true,
  24. sampling: true,
  25. graphHeight: '400px',
  26. shaded: {
  27. enabled: false,
  28. orientation: 'bottom' // top, bottom
  29. },
  30. style: 'line', // line, bar
  31. barChart: {
  32. width: 50,
  33. handleOverlap: 'overlap',
  34. align: 'center' // left, center, right
  35. },
  36. catmullRom: {
  37. enabled: true,
  38. parametrization: 'centripetal', // uniform (alpha = 0.0), chordal (alpha = 1.0), centripetal (alpha = 0.5)
  39. alpha: 0.5
  40. },
  41. drawPoints: {
  42. enabled: true,
  43. size: 6,
  44. style: 'square' // square, circle
  45. },
  46. dataAxis: {
  47. showMinorLabels: true,
  48. showMajorLabels: true,
  49. icons: false,
  50. width: '40px',
  51. visible: true,
  52. customRange: {
  53. left: {min:undefined, max:undefined},
  54. right: {min:undefined, max:undefined}
  55. }
  56. },
  57. legend: {
  58. enabled: false,
  59. icons: true,
  60. left: {
  61. visible: true,
  62. position: 'top-left' // top/bottom - left,right
  63. },
  64. right: {
  65. visible: true,
  66. position: 'top-right' // top/bottom - left,right
  67. }
  68. },
  69. groups: {
  70. visibility: {}
  71. }
  72. };
  73. // options is shared by this ItemSet and all its items
  74. this.options = util.extend({}, this.defaultOptions);
  75. this.dom = {};
  76. this.props = {};
  77. this.hammer = null;
  78. this.groups = {};
  79. this.abortedGraphUpdate = false;
  80. this.autoSizeSVG = false;
  81. var me = this;
  82. this.itemsData = null; // DataSet
  83. this.groupsData = null; // DataSet
  84. // listeners for the DataSet of the items
  85. this.itemListeners = {
  86. 'add': function (event, params, senderId) {
  87. me._onAdd(params.items);
  88. },
  89. 'update': function (event, params, senderId) {
  90. me._onUpdate(params.items);
  91. },
  92. 'remove': function (event, params, senderId) {
  93. me._onRemove(params.items);
  94. }
  95. };
  96. // listeners for the DataSet of the groups
  97. this.groupListeners = {
  98. 'add': function (event, params, senderId) {
  99. me._onAddGroups(params.items);
  100. },
  101. 'update': function (event, params, senderId) {
  102. me._onUpdateGroups(params.items);
  103. },
  104. 'remove': function (event, params, senderId) {
  105. me._onRemoveGroups(params.items);
  106. }
  107. };
  108. this.items = {}; // object with an Item for every data item
  109. this.selection = []; // list with the ids of all selected nodes
  110. this.lastStart = this.body.range.start;
  111. this.touchParams = {}; // stores properties while dragging
  112. this.svgElements = {};
  113. this.setOptions(options);
  114. this.groupsUsingDefaultStyles = [0];
  115. this.body.emitter.on('rangechanged', function() {
  116. me.lastStart = me.body.range.start;
  117. me.svg.style.left = util.option.asSize(-me.width);
  118. me._updateGraph.apply(me);
  119. });
  120. // create the HTML DOM
  121. this._create();
  122. this.body.emitter.emit('change');
  123. }
  124. LineGraph.prototype = new Component();
  125. /**
  126. * Create the HTML DOM for the ItemSet
  127. */
  128. LineGraph.prototype._create = function(){
  129. var frame = document.createElement('div');
  130. frame.className = 'LineGraph';
  131. this.dom.frame = frame;
  132. // create svg element for graph drawing.
  133. this.svg = document.createElementNS('http://www.w3.org/2000/svg','svg');
  134. this.svg.style.position = 'relative';
  135. this.svg.style.height = ('' + this.options.graphHeight).replace('px','') + 'px';
  136. this.svg.style.display = 'block';
  137. frame.appendChild(this.svg);
  138. // data axis
  139. this.options.dataAxis.orientation = 'left';
  140. this.yAxisLeft = new DataAxis(this.body, this.options.dataAxis, this.svg, this.options.groups);
  141. this.options.dataAxis.orientation = 'right';
  142. this.yAxisRight = new DataAxis(this.body, this.options.dataAxis, this.svg, this.options.groups);
  143. delete this.options.dataAxis.orientation;
  144. // legends
  145. this.legendLeft = new Legend(this.body, this.options.legend, 'left', this.options.groups);
  146. this.legendRight = new Legend(this.body, this.options.legend, 'right', this.options.groups);
  147. this.show();
  148. };
  149. /**
  150. * set the options of the LineGraph. the mergeOptions is used for subObjects that have an enabled element.
  151. * @param options
  152. */
  153. LineGraph.prototype.setOptions = function(options) {
  154. if (options) {
  155. var fields = ['sampling','defaultGroup','graphHeight','yAxisOrientation','style','barChart','dataAxis','sort','groups'];
  156. if (options.graphHeight === undefined && options.height !== undefined && this.body.domProps.centerContainer.height !== undefined) {
  157. this.autoSizeSVG = true;
  158. }
  159. else if (this.body.domProps.centerContainer.height !== undefined && parseInt(options.graphHeight.replace("px",'')) < this.body.domProps.centerContainer.height) {
  160. this.autoSizeSVG = true;
  161. }
  162. util.selectiveDeepExtend(fields, this.options, options);
  163. util.mergeOptions(this.options, options,'catmullRom');
  164. util.mergeOptions(this.options, options,'drawPoints');
  165. util.mergeOptions(this.options, options,'shaded');
  166. util.mergeOptions(this.options, options,'legend');
  167. if (options.catmullRom) {
  168. if (typeof options.catmullRom == 'object') {
  169. if (options.catmullRom.parametrization) {
  170. if (options.catmullRom.parametrization == 'uniform') {
  171. this.options.catmullRom.alpha = 0;
  172. }
  173. else if (options.catmullRom.parametrization == 'chordal') {
  174. this.options.catmullRom.alpha = 1.0;
  175. }
  176. else {
  177. this.options.catmullRom.parametrization = 'centripetal';
  178. this.options.catmullRom.alpha = 0.5;
  179. }
  180. }
  181. }
  182. }
  183. if (this.yAxisLeft) {
  184. if (options.dataAxis !== undefined) {
  185. this.yAxisLeft.setOptions(this.options.dataAxis);
  186. this.yAxisRight.setOptions(this.options.dataAxis);
  187. }
  188. }
  189. if (this.legendLeft) {
  190. if (options.legend !== undefined) {
  191. this.legendLeft.setOptions(this.options.legend);
  192. this.legendRight.setOptions(this.options.legend);
  193. }
  194. }
  195. if (this.groups.hasOwnProperty(UNGROUPED)) {
  196. this.groups[UNGROUPED].setOptions(options);
  197. }
  198. }
  199. if (this.dom.frame) {
  200. this._updateGraph();
  201. }
  202. };
  203. /**
  204. * Hide the component from the DOM
  205. */
  206. LineGraph.prototype.hide = function() {
  207. // remove the frame containing the items
  208. if (this.dom.frame.parentNode) {
  209. this.dom.frame.parentNode.removeChild(this.dom.frame);
  210. }
  211. };
  212. /**
  213. * Show the component in the DOM (when not already visible).
  214. * @return {Boolean} changed
  215. */
  216. LineGraph.prototype.show = function() {
  217. // show frame containing the items
  218. if (!this.dom.frame.parentNode) {
  219. this.body.dom.center.appendChild(this.dom.frame);
  220. }
  221. };
  222. /**
  223. * Set items
  224. * @param {vis.DataSet | null} items
  225. */
  226. LineGraph.prototype.setItems = function(items) {
  227. var me = this,
  228. ids,
  229. oldItemsData = this.itemsData;
  230. // replace the dataset
  231. if (!items) {
  232. this.itemsData = null;
  233. }
  234. else if (items instanceof DataSet || items instanceof DataView) {
  235. this.itemsData = items;
  236. }
  237. else {
  238. throw new TypeError('Data must be an instance of DataSet or DataView');
  239. }
  240. if (oldItemsData) {
  241. // unsubscribe from old dataset
  242. util.forEach(this.itemListeners, function (callback, event) {
  243. oldItemsData.off(event, callback);
  244. });
  245. // remove all drawn items
  246. ids = oldItemsData.getIds();
  247. this._onRemove(ids);
  248. }
  249. if (this.itemsData) {
  250. // subscribe to new dataset
  251. var id = this.id;
  252. util.forEach(this.itemListeners, function (callback, event) {
  253. me.itemsData.on(event, callback, id);
  254. });
  255. // add all new items
  256. ids = this.itemsData.getIds();
  257. this._onAdd(ids);
  258. }
  259. this._updateUngrouped();
  260. this._updateGraph();
  261. this.redraw();
  262. };
  263. /**
  264. * Set groups
  265. * @param {vis.DataSet} groups
  266. */
  267. LineGraph.prototype.setGroups = function(groups) {
  268. var me = this;
  269. var ids;
  270. // unsubscribe from current dataset
  271. if (this.groupsData) {
  272. util.forEach(this.groupListeners, function (callback, event) {
  273. me.groupsData.unsubscribe(event, callback);
  274. });
  275. // remove all drawn groups
  276. ids = this.groupsData.getIds();
  277. this.groupsData = null;
  278. this._onRemoveGroups(ids); // note: this will cause a redraw
  279. }
  280. // replace the dataset
  281. if (!groups) {
  282. this.groupsData = null;
  283. }
  284. else if (groups instanceof DataSet || groups instanceof DataView) {
  285. this.groupsData = groups;
  286. }
  287. else {
  288. throw new TypeError('Data must be an instance of DataSet or DataView');
  289. }
  290. if (this.groupsData) {
  291. // subscribe to new dataset
  292. var id = this.id;
  293. util.forEach(this.groupListeners, function (callback, event) {
  294. me.groupsData.on(event, callback, id);
  295. });
  296. // draw all ms
  297. ids = this.groupsData.getIds();
  298. this._onAddGroups(ids);
  299. }
  300. this._onUpdate();
  301. };
  302. /**
  303. * Update the datapoints
  304. * @param [ids]
  305. * @private
  306. */
  307. LineGraph.prototype._onUpdate = function(ids) {
  308. this._updateUngrouped();
  309. this._updateAllGroupData();
  310. this._updateGraph();
  311. this.redraw();
  312. };
  313. LineGraph.prototype._onAdd = function (ids) {this._onUpdate(ids);};
  314. LineGraph.prototype._onRemove = function (ids) {this._onUpdate(ids);};
  315. LineGraph.prototype._onUpdateGroups = function (groupIds) {
  316. for (var i = 0; i < groupIds.length; i++) {
  317. var group = this.groupsData.get(groupIds[i]);
  318. this._updateGroup(group, groupIds[i]);
  319. }
  320. this._updateGraph();
  321. this.redraw();
  322. };
  323. LineGraph.prototype._onAddGroups = function (groupIds) {this._onUpdateGroups(groupIds);};
  324. LineGraph.prototype._onRemoveGroups = function (groupIds) {
  325. for (var i = 0; i < groupIds.length; i++) {
  326. if (this.groups.hasOwnProperty(groupIds[i])) {
  327. if (this.groups[groupIds[i]].options.yAxisOrientation == 'right') {
  328. this.yAxisRight.removeGroup(groupIds[i]);
  329. this.legendRight.removeGroup(groupIds[i]);
  330. this.legendRight.redraw();
  331. }
  332. else {
  333. this.yAxisLeft.removeGroup(groupIds[i]);
  334. this.legendLeft.removeGroup(groupIds[i]);
  335. this.legendLeft.redraw();
  336. }
  337. delete this.groups[groupIds[i]];
  338. }
  339. }
  340. this._updateUngrouped();
  341. this._updateGraph();
  342. this.redraw();
  343. };
  344. /**
  345. * update a group object
  346. *
  347. * @param group
  348. * @param groupId
  349. * @private
  350. */
  351. LineGraph.prototype._updateGroup = function (group, groupId) {
  352. if (!this.groups.hasOwnProperty(groupId)) {
  353. this.groups[groupId] = new GraphGroup(group, groupId, this.options, this.groupsUsingDefaultStyles);
  354. if (this.groups[groupId].options.yAxisOrientation == 'right') {
  355. this.yAxisRight.addGroup(groupId, this.groups[groupId]);
  356. this.legendRight.addGroup(groupId, this.groups[groupId]);
  357. }
  358. else {
  359. this.yAxisLeft.addGroup(groupId, this.groups[groupId]);
  360. this.legendLeft.addGroup(groupId, this.groups[groupId]);
  361. }
  362. }
  363. else {
  364. this.groups[groupId].update(group);
  365. if (this.groups[groupId].options.yAxisOrientation == 'right') {
  366. this.yAxisRight.updateGroup(groupId, this.groups[groupId]);
  367. this.legendRight.updateGroup(groupId, this.groups[groupId]);
  368. }
  369. else {
  370. this.yAxisLeft.updateGroup(groupId, this.groups[groupId]);
  371. this.legendLeft.updateGroup(groupId, this.groups[groupId]);
  372. }
  373. }
  374. this.legendLeft.redraw();
  375. this.legendRight.redraw();
  376. };
  377. LineGraph.prototype._updateAllGroupData = function () {
  378. if (this.itemsData != null) {
  379. var groupsContent = {};
  380. var groupId;
  381. for (groupId in this.groups) {
  382. if (this.groups.hasOwnProperty(groupId)) {
  383. groupsContent[groupId] = [];
  384. }
  385. }
  386. for (var itemId in this.itemsData._data) {
  387. if (this.itemsData._data.hasOwnProperty(itemId)) {
  388. var item = this.itemsData._data[itemId];
  389. if (groupsContent[item.group] === undefined) {
  390. throw new Error('Cannot find referenced group. Possible reason: items added before groups? Groups need to be added before items, as items refer to groups.')
  391. }
  392. item.x = util.convert(item.x,'Date');
  393. groupsContent[item.group].push(item);
  394. }
  395. }
  396. for (groupId in this.groups) {
  397. if (this.groups.hasOwnProperty(groupId)) {
  398. this.groups[groupId].setItems(groupsContent[groupId]);
  399. }
  400. }
  401. }
  402. };
  403. /**
  404. * Create or delete the group holding all ungrouped items. This group is used when
  405. * there are no groups specified. This anonymous group is called 'graph'.
  406. * @protected
  407. */
  408. LineGraph.prototype._updateUngrouped = function() {
  409. if (this.itemsData && this.itemsData != null) {
  410. var ungroupedCounter = 0;
  411. for (var itemId in this.itemsData._data) {
  412. if (this.itemsData._data.hasOwnProperty(itemId)) {
  413. var item = this.itemsData._data[itemId];
  414. if (item != undefined) {
  415. if (item.hasOwnProperty('group')) {
  416. if (item.group === undefined) {
  417. item.group = UNGROUPED;
  418. }
  419. }
  420. else {
  421. item.group = UNGROUPED;
  422. }
  423. ungroupedCounter = item.group == UNGROUPED ? ungroupedCounter + 1 : ungroupedCounter;
  424. }
  425. }
  426. }
  427. if (ungroupedCounter == 0) {
  428. delete this.groups[UNGROUPED];
  429. this.legendLeft.removeGroup(UNGROUPED);
  430. this.legendRight.removeGroup(UNGROUPED);
  431. this.yAxisLeft.removeGroup(UNGROUPED);
  432. this.yAxisRight.removeGroup(UNGROUPED);
  433. }
  434. else {
  435. var group = {id: UNGROUPED, content: this.options.defaultGroup};
  436. this._updateGroup(group, UNGROUPED);
  437. }
  438. }
  439. else {
  440. delete this.groups[UNGROUPED];
  441. this.legendLeft.removeGroup(UNGROUPED);
  442. this.legendRight.removeGroup(UNGROUPED);
  443. this.yAxisLeft.removeGroup(UNGROUPED);
  444. this.yAxisRight.removeGroup(UNGROUPED);
  445. }
  446. this.legendLeft.redraw();
  447. this.legendRight.redraw();
  448. };
  449. /**
  450. * Redraw the component, mandatory function
  451. * @return {boolean} Returns true if the component is resized
  452. */
  453. LineGraph.prototype.redraw = function() {
  454. var resized = false;
  455. this.svg.style.height = ('' + this.options.graphHeight).replace('px','') + 'px';
  456. if (this.lastWidth === undefined && this.width || this.lastWidth != this.width) {
  457. resized = true;
  458. }
  459. // check if this component is resized
  460. resized = this._isResized() || resized;
  461. // check whether zoomed (in that case we need to re-stack everything)
  462. var visibleInterval = this.body.range.end - this.body.range.start;
  463. var zoomed = (visibleInterval != this.lastVisibleInterval) || (this.width != this.lastWidth);
  464. this.lastVisibleInterval = visibleInterval;
  465. this.lastWidth = this.width;
  466. // calculate actual size and position
  467. this.width = this.dom.frame.offsetWidth;
  468. // the svg element is three times as big as the width, this allows for fully dragging left and right
  469. // without reloading the graph. the controls for this are bound to events in the constructor
  470. if (resized == true) {
  471. this.svg.style.width = util.option.asSize(3*this.width);
  472. this.svg.style.left = util.option.asSize(-this.width);
  473. }
  474. if (zoomed == true || this.abortedGraphUpdate == true) {
  475. this._updateGraph();
  476. }
  477. else {
  478. // move the whole svg while dragging
  479. if (this.lastStart != 0) {
  480. var offset = this.body.range.start - this.lastStart;
  481. var range = this.body.range.end - this.body.range.start;
  482. if (this.width != 0) {
  483. var rangePerPixelInv = this.width/range;
  484. var xOffset = offset * rangePerPixelInv;
  485. this.svg.style.left = (-this.width - xOffset) + 'px';
  486. }
  487. }
  488. }
  489. this.legendLeft.redraw();
  490. this.legendRight.redraw();
  491. return resized;
  492. };
  493. /**
  494. * Update and redraw the graph.
  495. *
  496. */
  497. LineGraph.prototype._updateGraph = function () {
  498. // reset the svg elements
  499. DOMutil.prepareElements(this.svgElements);
  500. if (this.width != 0 && this.itemsData != null) {
  501. var group, i;
  502. var preprocessedGroupData = {};
  503. var processedGroupData = {};
  504. var groupRanges = {};
  505. var changeCalled = false;
  506. // update the height of the graph on each redraw of the graph.
  507. if (this.autoSizeSVG == true) {
  508. if (this.options.graphHeight != this.body.domProps.centerContainer.height + 'px') {
  509. this.options.graphHeight = this.body.domProps.centerContainer.height + 'px';
  510. this.svg.style.height = this.body.domProps.centerContainer.height + 'px';
  511. }
  512. }
  513. // getting group Ids
  514. var groupIds = [];
  515. for (var groupId in this.groups) {
  516. if (this.groups.hasOwnProperty(groupId)) {
  517. group = this.groups[groupId];
  518. if (group.visible == true && (this.options.groups.visibility[groupId] === undefined || this.options.groups.visibility[groupId] == true)) {
  519. groupIds.push(groupId);
  520. }
  521. }
  522. }
  523. if (groupIds.length > 0) {
  524. // this is the range of the SVG canvas
  525. var minDate = this.body.util.toGlobalTime(- this.body.domProps.root.width);
  526. var maxDate = this.body.util.toGlobalTime(2 * this.body.domProps.root.width);
  527. var groupsData = {};
  528. // fill groups data
  529. this._getRelevantData(groupIds, groupsData, minDate, maxDate);
  530. // we transform the X coordinates to detect collisions
  531. for (i = 0; i < groupIds.length; i++) {
  532. preprocessedGroupData[groupIds[i]] = this._convertXcoordinates(groupsData[groupIds[i]]);
  533. }
  534. // now all needed data has been collected we start the processing.
  535. this._getYRanges(groupIds, preprocessedGroupData, groupRanges);
  536. // update the Y axis first, we use this data to draw at the correct Y points
  537. // changeCalled is required to clean the SVG on a change emit.
  538. changeCalled = this._updateYAxis(groupIds, groupRanges);
  539. if (changeCalled == true) {
  540. DOMutil.cleanupElements(this.svgElements);
  541. this.abortedGraphUpdate = true;
  542. this.body.emitter.emit('change');
  543. return;
  544. }
  545. this.abortedGraphUpdate = false;
  546. // With the yAxis scaled correctly, use this to get the Y values of the points.
  547. for (i = 0; i < groupIds.length; i++) {
  548. group = this.groups[groupIds[i]];
  549. processedGroupData[groupIds[i]] = this._convertYcoordinates(groupsData[groupIds[i]], group);
  550. }
  551. // draw the groups
  552. for (i = 0; i < groupIds.length; i++) {
  553. group = this.groups[groupIds[i]];
  554. if (group.options.style == 'line') {
  555. this._drawLineGraph(processedGroupData[groupIds[i]], group);
  556. }
  557. }
  558. this._drawBarGraphs(groupIds, processedGroupData);
  559. }
  560. }
  561. // cleanup unused svg elements
  562. DOMutil.cleanupElements(this.svgElements);
  563. };
  564. LineGraph.prototype._getRelevantData = function (groupIds, groupsData, minDate, maxDate) {
  565. // first select and preprocess the data from the datasets.
  566. // the groups have their preselection of data, we now loop over this data to see
  567. // what data we need to draw. Sorted data is much faster.
  568. // more optimization is possible by doing the sampling before and using the binary search
  569. // to find the end date to determine the increment.
  570. var group, i, j, item;
  571. if (groupIds.length > 0) {
  572. for (i = 0; i < groupIds.length; i++) {
  573. group = this.groups[groupIds[i]];
  574. groupsData[groupIds[i]] = [];
  575. var dataContainer = groupsData[groupIds[i]];
  576. // optimization for sorted data
  577. if (group.options.sort == true) {
  578. var guess = Math.max(0, util.binarySearchGeneric(group.itemsData, minDate, 'x', 'before'));
  579. for (j = guess; j < group.itemsData.length; j++) {
  580. item = group.itemsData[j];
  581. if (item !== undefined) {
  582. if (item.x > maxDate) {
  583. dataContainer.push(item);
  584. break;
  585. }
  586. else {
  587. dataContainer.push(item);
  588. }
  589. }
  590. }
  591. }
  592. else {
  593. for (j = 0; j < group.itemsData.length; j++) {
  594. item = group.itemsData[j];
  595. if (item !== undefined) {
  596. if (item.x > minDate && item.x < maxDate) {
  597. dataContainer.push(item);
  598. }
  599. }
  600. }
  601. }
  602. }
  603. }
  604. this._applySampling(groupIds, groupsData);
  605. };
  606. LineGraph.prototype._applySampling = function (groupIds, groupsData) {
  607. var group;
  608. if (groupIds.length > 0) {
  609. for (var i = 0; i < groupIds.length; i++) {
  610. group = this.groups[groupIds[i]];
  611. if (group.options.sampling == true) {
  612. var dataContainer = groupsData[groupIds[i]];
  613. if (dataContainer.length > 0) {
  614. var increment = 1;
  615. var amountOfPoints = dataContainer.length;
  616. // the global screen is used because changing the width of the yAxis may affect the increment, resulting in an endless loop
  617. // of width changing of the yAxis.
  618. var xDistance = this.body.util.toGlobalScreen(dataContainer[dataContainer.length - 1].x) - this.body.util.toGlobalScreen(dataContainer[0].x);
  619. var pointsPerPixel = amountOfPoints / xDistance;
  620. increment = Math.min(Math.ceil(0.2 * amountOfPoints), Math.max(1, Math.round(pointsPerPixel)));
  621. var sampledData = [];
  622. for (var j = 0; j < amountOfPoints; j += increment) {
  623. sampledData.push(dataContainer[j]);
  624. }
  625. groupsData[groupIds[i]] = sampledData;
  626. }
  627. }
  628. }
  629. }
  630. };
  631. LineGraph.prototype._getYRanges = function (groupIds, groupsData, groupRanges) {
  632. var groupData, group, i,j;
  633. var barCombinedDataLeft = [];
  634. var barCombinedDataRight = [];
  635. var barCombinedData;
  636. if (groupIds.length > 0) {
  637. for (i = 0; i < groupIds.length; i++) {
  638. groupData = groupsData[groupIds[i]];
  639. if (groupData.length > 0) {
  640. group = this.groups[groupIds[i]];
  641. if (group.options.style == 'line' || group.options.barChart.handleOverlap != 'stack') {
  642. var yMin = groupData[0].y;
  643. var yMax = groupData[0].y;
  644. for (j = 0; j < groupData.length; j++) {
  645. yMin = yMin > groupData[j].y ? groupData[j].y : yMin;
  646. yMax = yMax < groupData[j].y ? groupData[j].y : yMax;
  647. }
  648. groupRanges[groupIds[i]] = {min: yMin, max: yMax, yAxisOrientation: group.options.yAxisOrientation};
  649. }
  650. else if (group.options.style == 'bar') {
  651. if (group.options.yAxisOrientation == 'left') {
  652. barCombinedData = barCombinedDataLeft;
  653. }
  654. else {
  655. barCombinedData = barCombinedDataRight;
  656. }
  657. groupRanges[groupIds[i]] = {min: 0, max: 0, yAxisOrientation: group.options.yAxisOrientation, ignore: true};
  658. // combine data
  659. for (j = 0; j < groupData.length; j++) {
  660. barCombinedData.push({
  661. x: groupData[j].x,
  662. y: groupData[j].y,
  663. groupId: groupIds[i]
  664. });
  665. }
  666. }
  667. }
  668. }
  669. var intersections;
  670. if (barCombinedDataLeft.length > 0) {
  671. // sort by time and by group
  672. barCombinedDataLeft.sort(function (a, b) {
  673. if (a.x == b.x) {
  674. return a.groupId - b.groupId;
  675. } else {
  676. return a.x - b.x;
  677. }
  678. });
  679. intersections = {};
  680. this._getDataIntersections(intersections, barCombinedDataLeft);
  681. groupRanges['__barchartLeft'] = this._getStackedBarYRange(intersections, barCombinedDataLeft);
  682. groupRanges['__barchartLeft'].yAxisOrientation = 'left';
  683. groupIds.push('__barchartLeft');
  684. }
  685. if (barCombinedDataRight.length > 0) {
  686. // sort by time and by group
  687. barCombinedDataRight.sort(function (a, b) {
  688. if (a.x == b.x) {
  689. return a.groupId - b.groupId;
  690. } else {
  691. return a.x - b.x;
  692. }
  693. });
  694. intersections = {};
  695. this._getDataIntersections(intersections, barCombinedDataRight);
  696. groupRanges['__barchartRight'] = this._getStackedBarYRange(intersections, barCombinedDataRight);
  697. groupRanges['__barchartRight'].yAxisOrientation = 'right';
  698. groupIds.push('__barchartRight');
  699. }
  700. }
  701. };
  702. LineGraph.prototype._getStackedBarYRange = function (intersections, combinedData) {
  703. var key;
  704. var yMin = combinedData[0].y;
  705. var yMax = combinedData[0].y;
  706. for (var i = 0; i < combinedData.length; i++) {
  707. key = combinedData[i].x;
  708. if (intersections[key] === undefined) {
  709. yMin = yMin > combinedData[i].y ? combinedData[i].y : yMin;
  710. yMax = yMax < combinedData[i].y ? combinedData[i].y : yMax;
  711. }
  712. else {
  713. intersections[key].accumulated += combinedData[i].y;
  714. }
  715. }
  716. for (var xpos in intersections) {
  717. if (intersections.hasOwnProperty(xpos)) {
  718. yMin = yMin > intersections[xpos].accumulated ? intersections[xpos].accumulated : yMin;
  719. yMax = yMax < intersections[xpos].accumulated ? intersections[xpos].accumulated : yMax;
  720. }
  721. }
  722. return {min: yMin, max: yMax};
  723. };
  724. /**
  725. * this sets the Y ranges for the Y axis. It also determines which of the axis should be shown or hidden.
  726. * @param {Array} groupIds
  727. * @param {Object} groupRanges
  728. * @private
  729. */
  730. LineGraph.prototype._updateYAxis = function (groupIds, groupRanges) {
  731. var changeCalled = false;
  732. var yAxisLeftUsed = false;
  733. var yAxisRightUsed = false;
  734. var minLeft = 1e9, minRight = 1e9, maxLeft = -1e9, maxRight = -1e9, minVal, maxVal;
  735. // if groups are present
  736. if (groupIds.length > 0) {
  737. for (var i = 0; i < groupIds.length; i++) {
  738. if (groupRanges.hasOwnProperty(groupIds[i])) {
  739. if (groupRanges[groupIds[i]].ignore !== true) {
  740. minVal = groupRanges[groupIds[i]].min;
  741. maxVal = groupRanges[groupIds[i]].max;
  742. if (groupRanges[groupIds[i]].yAxisOrientation == 'left') {
  743. yAxisLeftUsed = true;
  744. minLeft = minLeft > minVal ? minVal : minLeft;
  745. maxLeft = maxLeft < maxVal ? maxVal : maxLeft;
  746. }
  747. else {
  748. yAxisRightUsed = true;
  749. minRight = minRight > minVal ? minVal : minRight;
  750. maxRight = maxRight < maxVal ? maxVal : maxRight;
  751. }
  752. }
  753. }
  754. }
  755. if (yAxisLeftUsed == true) {
  756. this.yAxisLeft.setRange(minLeft, maxLeft);
  757. }
  758. if (yAxisRightUsed == true) {
  759. this.yAxisRight.setRange(minRight, maxRight);
  760. }
  761. }
  762. changeCalled = this._toggleAxisVisiblity(yAxisLeftUsed , this.yAxisLeft) || changeCalled;
  763. changeCalled = this._toggleAxisVisiblity(yAxisRightUsed, this.yAxisRight) || changeCalled;
  764. if (yAxisRightUsed == true && yAxisLeftUsed == true) {
  765. this.yAxisLeft.drawIcons = true;
  766. this.yAxisRight.drawIcons = true;
  767. }
  768. else {
  769. this.yAxisLeft.drawIcons = false;
  770. this.yAxisRight.drawIcons = false;
  771. }
  772. this.yAxisRight.master = !yAxisLeftUsed;
  773. if (this.yAxisRight.master == false) {
  774. if (yAxisRightUsed == true) {this.yAxisLeft.lineOffset = this.yAxisRight.width;}
  775. else {this.yAxisLeft.lineOffset = 0;}
  776. changeCalled = this.yAxisLeft.redraw() || changeCalled;
  777. this.yAxisRight.stepPixelsForced = this.yAxisLeft.stepPixels;
  778. changeCalled = this.yAxisRight.redraw() || changeCalled;
  779. }
  780. else {
  781. changeCalled = this.yAxisRight.redraw() || changeCalled;
  782. }
  783. // clean the accumulated lists
  784. if (groupIds.indexOf('__barchartLeft') != -1) {
  785. groupIds.splice(groupIds.indexOf('__barchartLeft'),1);
  786. }
  787. if (groupIds.indexOf('__barchartRight') != -1) {
  788. groupIds.splice(groupIds.indexOf('__barchartRight'),1);
  789. }
  790. return changeCalled;
  791. };
  792. /**
  793. * This shows or hides the Y axis if needed. If there is a change, the changed event is emitted by the updateYAxis function
  794. *
  795. * @param {boolean} axisUsed
  796. * @returns {boolean}
  797. * @private
  798. * @param axis
  799. */
  800. LineGraph.prototype._toggleAxisVisiblity = function (axisUsed, axis) {
  801. var changed = false;
  802. if (axisUsed == false) {
  803. if (axis.dom.frame.parentNode) {
  804. axis.hide();
  805. changed = true;
  806. }
  807. }
  808. else {
  809. if (!axis.dom.frame.parentNode) {
  810. axis.show();
  811. changed = true;
  812. }
  813. }
  814. return changed;
  815. };
  816. /**
  817. * draw a bar graph
  818. *
  819. * @param groupIds
  820. * @param processedGroupData
  821. */
  822. LineGraph.prototype._drawBarGraphs = function (groupIds, processedGroupData) {
  823. var combinedData = [];
  824. var intersections = {};
  825. var coreDistance;
  826. var key, drawData;
  827. var group;
  828. var i,j;
  829. var barPoints = 0;
  830. // combine all barchart data
  831. for (i = 0; i < groupIds.length; i++) {
  832. group = this.groups[groupIds[i]];
  833. if (group.options.style == 'bar') {
  834. if (group.visible == true && (this.options.groups.visibility[groupIds[i]] === undefined || this.options.groups.visibility[groupIds[i]] == true)) {
  835. for (j = 0; j < processedGroupData[groupIds[i]].length; j++) {
  836. combinedData.push({
  837. x: processedGroupData[groupIds[i]][j].x,
  838. y: processedGroupData[groupIds[i]][j].y,
  839. groupId: groupIds[i]
  840. });
  841. barPoints += 1;
  842. }
  843. }
  844. }
  845. }
  846. if (barPoints == 0) {return;}
  847. // sort by time and by group
  848. combinedData.sort(function (a, b) {
  849. if (a.x == b.x) {
  850. return a.groupId - b.groupId;
  851. } else {
  852. return a.x - b.x;
  853. }
  854. });
  855. // get intersections
  856. this._getDataIntersections(intersections, combinedData);
  857. // plot barchart
  858. for (i = 0; i < combinedData.length; i++) {
  859. group = this.groups[combinedData[i].groupId];
  860. var minWidth = 0.1 * group.options.barChart.width;
  861. key = combinedData[i].x;
  862. var heightOffset = 0;
  863. if (intersections[key] === undefined) {
  864. if (i+1 < combinedData.length) {coreDistance = Math.abs(combinedData[i+1].x - key);}
  865. if (i > 0) {coreDistance = Math.min(coreDistance,Math.abs(combinedData[i-1].x - key));}
  866. drawData = this._getSafeDrawData(coreDistance, group, minWidth);
  867. }
  868. else {
  869. var nextKey = i + (intersections[key].amount - intersections[key].resolved);
  870. var prevKey = i - (intersections[key].resolved + 1);
  871. if (nextKey < combinedData.length) {coreDistance = Math.abs(combinedData[nextKey].x - key);}
  872. if (prevKey > 0) {coreDistance = Math.min(coreDistance,Math.abs(combinedData[prevKey].x - key));}
  873. drawData = this._getSafeDrawData(coreDistance, group, minWidth);
  874. intersections[key].resolved += 1;
  875. if (group.options.barChart.handleOverlap == 'stack') {
  876. heightOffset = intersections[key].accumulated;
  877. intersections[key].accumulated += group.zeroPosition - combinedData[i].y;
  878. }
  879. else if (group.options.barChart.handleOverlap == 'sideBySide') {
  880. drawData.width = drawData.width / intersections[key].amount;
  881. drawData.offset += (intersections[key].resolved) * drawData.width - (0.5*drawData.width * (intersections[key].amount+1));
  882. if (group.options.barChart.align == 'left') {drawData.offset -= 0.5*drawData.width;}
  883. else if (group.options.barChart.align == 'right') {drawData.offset += 0.5*drawData.width;}
  884. }
  885. }
  886. DOMutil.drawBar(combinedData[i].x + drawData.offset, combinedData[i].y - heightOffset, drawData.width, group.zeroPosition - combinedData[i].y, group.className + ' bar', this.svgElements, this.svg);
  887. // draw points
  888. if (group.options.drawPoints.enabled == true) {
  889. DOMutil.drawPoint(combinedData[i].x + drawData.offset, combinedData[i].y - heightOffset, group, this.svgElements, this.svg);
  890. }
  891. }
  892. };
  893. /**
  894. * Fill the intersections object with counters of how many datapoints share the same x coordinates
  895. * @param intersections
  896. * @param combinedData
  897. * @private
  898. */
  899. LineGraph.prototype._getDataIntersections = function (intersections, combinedData) {
  900. // get intersections
  901. var coreDistance;
  902. for (var i = 0; i < combinedData.length; i++) {
  903. if (i + 1 < combinedData.length) {
  904. coreDistance = Math.abs(combinedData[i + 1].x - combinedData[i].x);
  905. }
  906. if (i > 0) {
  907. coreDistance = Math.min(coreDistance, Math.abs(combinedData[i - 1].x - combinedData[i].x));
  908. }
  909. if (coreDistance == 0) {
  910. if (intersections[combinedData[i].x] === undefined) {
  911. intersections[combinedData[i].x] = {amount: 0, resolved: 0, accumulated: 0};
  912. }
  913. intersections[combinedData[i].x].amount += 1;
  914. }
  915. }
  916. };
  917. /**
  918. * Get the width and offset for bargraphs based on the coredistance between datapoints
  919. *
  920. * @param coreDistance
  921. * @param group
  922. * @param minWidth
  923. * @returns {{width: Number, offset: Number}}
  924. * @private
  925. */
  926. LineGraph.prototype._getSafeDrawData = function (coreDistance, group, minWidth) {
  927. var width, offset;
  928. if (coreDistance < group.options.barChart.width && coreDistance > 0) {
  929. width = coreDistance < minWidth ? minWidth : coreDistance;
  930. offset = 0; // recalculate offset with the new width;
  931. if (group.options.barChart.align == 'left') {
  932. offset -= 0.5 * coreDistance;
  933. }
  934. else if (group.options.barChart.align == 'right') {
  935. offset += 0.5 * coreDistance;
  936. }
  937. }
  938. else {
  939. // default settings
  940. width = group.options.barChart.width;
  941. offset = 0;
  942. if (group.options.barChart.align == 'left') {
  943. offset -= 0.5 * group.options.barChart.width;
  944. }
  945. else if (group.options.barChart.align == 'right') {
  946. offset += 0.5 * group.options.barChart.width;
  947. }
  948. }
  949. return {width: width, offset: offset};
  950. };
  951. /**
  952. * draw a line graph
  953. *
  954. * @param dataset
  955. * @param group
  956. */
  957. LineGraph.prototype._drawLineGraph = function (dataset, group) {
  958. if (dataset != null) {
  959. if (dataset.length > 0) {
  960. var path, d;
  961. var svgHeight = Number(this.svg.style.height.replace('px',''));
  962. path = DOMutil.getSVGElement('path', this.svgElements, this.svg);
  963. path.setAttributeNS(null, 'class', group.className);
  964. // construct path from dataset
  965. if (group.options.catmullRom.enabled == true) {
  966. d = this._catmullRom(dataset, group);
  967. }
  968. else {
  969. d = this._linear(dataset);
  970. }
  971. // append with points for fill and finalize the path
  972. if (group.options.shaded.enabled == true) {
  973. var fillPath = DOMutil.getSVGElement('path',this.svgElements, this.svg);
  974. var dFill;
  975. if (group.options.shaded.orientation == 'top') {
  976. dFill = 'M' + dataset[0].x + ',' + 0 + ' ' + d + 'L' + dataset[dataset.length - 1].x + ',' + 0;
  977. }
  978. else {
  979. dFill = 'M' + dataset[0].x + ',' + svgHeight + ' ' + d + 'L' + dataset[dataset.length - 1].x + ',' + svgHeight;
  980. }
  981. fillPath.setAttributeNS(null, 'class', group.className + ' fill');
  982. fillPath.setAttributeNS(null, 'd', dFill);
  983. }
  984. // copy properties to path for drawing.
  985. path.setAttributeNS(null, 'd', 'M' + d);
  986. // draw points
  987. if (group.options.drawPoints.enabled == true) {
  988. this._drawPoints(dataset, group, this.svgElements, this.svg);
  989. }
  990. }
  991. }
  992. };
  993. /**
  994. * draw the data points
  995. *
  996. * @param {Array} dataset
  997. * @param {Object} JSONcontainer
  998. * @param {Object} svg | SVG DOM element
  999. * @param {GraphGroup} group
  1000. * @param {Number} [offset]
  1001. */
  1002. LineGraph.prototype._drawPoints = function (dataset, group, JSONcontainer, svg, offset) {
  1003. if (offset === undefined) {offset = 0;}
  1004. for (var i = 0; i < dataset.length; i++) {
  1005. DOMutil.drawPoint(dataset[i].x + offset, dataset[i].y, group, JSONcontainer, svg);
  1006. }
  1007. };
  1008. /**
  1009. * This uses the DataAxis object to generate the correct X coordinate on the SVG window. It uses the
  1010. * util function toScreen to get the x coordinate from the timestamp. It also pre-filters the data and get the minMax ranges for
  1011. * the yAxis.
  1012. *
  1013. * @param datapoints
  1014. * @returns {Array}
  1015. * @private
  1016. */
  1017. LineGraph.prototype._convertXcoordinates = function (datapoints) {
  1018. var extractedData = [];
  1019. var xValue, yValue;
  1020. var toScreen = this.body.util.toScreen;
  1021. for (var i = 0; i < datapoints.length; i++) {
  1022. xValue = toScreen(datapoints[i].x) + this.width;
  1023. yValue = datapoints[i].y;
  1024. extractedData.push({x: xValue, y: yValue});
  1025. }
  1026. return extractedData;
  1027. };
  1028. /**
  1029. * This uses the DataAxis object to generate the correct X coordinate on the SVG window. It uses the
  1030. * util function toScreen to get the x coordinate from the timestamp. It also pre-filters the data and get the minMax ranges for
  1031. * the yAxis.
  1032. *
  1033. * @param datapoints
  1034. * @returns {Array}
  1035. * @private
  1036. */
  1037. LineGraph.prototype._convertYcoordinates = function (datapoints, group) {
  1038. var extractedData = [];
  1039. var xValue, yValue;
  1040. var toScreen = this.body.util.toScreen;
  1041. var axis = this.yAxisLeft;
  1042. var svgHeight = Number(this.svg.style.height.replace('px',''));
  1043. if (group.options.yAxisOrientation == 'right') {
  1044. axis = this.yAxisRight;
  1045. }
  1046. for (var i = 0; i < datapoints.length; i++) {
  1047. xValue = toScreen(datapoints[i].x) + this.width;
  1048. yValue = Math.round(axis.convertValue(datapoints[i].y));
  1049. extractedData.push({x: xValue, y: yValue});
  1050. }
  1051. group.setZeroPosition(Math.min(svgHeight, axis.convertValue(0)));
  1052. return extractedData;
  1053. };
  1054. /**
  1055. * This uses an uniform parametrization of the CatmullRom algorithm:
  1056. * 'On the Parameterization of Catmull-Rom Curves' by Cem Yuksel et al.
  1057. * @param data
  1058. * @returns {string}
  1059. * @private
  1060. */
  1061. LineGraph.prototype._catmullRomUniform = function(data) {
  1062. // catmull rom
  1063. var p0, p1, p2, p3, bp1, bp2;
  1064. var d = Math.round(data[0].x) + ',' + Math.round(data[0].y) + ' ';
  1065. var normalization = 1/6;
  1066. var length = data.length;
  1067. for (var i = 0; i < length - 1; i++) {
  1068. p0 = (i == 0) ? data[0] : data[i-1];
  1069. p1 = data[i];
  1070. p2 = data[i+1];
  1071. p3 = (i + 2 < length) ? data[i+2] : p2;
  1072. // Catmull-Rom to Cubic Bezier conversion matrix
  1073. // 0 1 0 0
  1074. // -1/6 1 1/6 0
  1075. // 0 1/6 1 -1/6
  1076. // 0 0 1 0
  1077. // bp0 = { x: p1.x, y: p1.y };
  1078. bp1 = { x: ((-p0.x + 6*p1.x + p2.x) *normalization), y: ((-p0.y + 6*p1.y + p2.y) *normalization)};
  1079. bp2 = { x: (( p1.x + 6*p2.x - p3.x) *normalization), y: (( p1.y + 6*p2.y - p3.y) *normalization)};
  1080. // bp0 = { x: p2.x, y: p2.y };
  1081. d += 'C' +
  1082. bp1.x + ',' +
  1083. bp1.y + ' ' +
  1084. bp2.x + ',' +
  1085. bp2.y + ' ' +
  1086. p2.x + ',' +
  1087. p2.y + ' ';
  1088. }
  1089. return d;
  1090. };
  1091. /**
  1092. * This uses either the chordal or centripetal parameterization of the catmull-rom algorithm.
  1093. * By default, the centripetal parameterization is used because this gives the nicest results.
  1094. * These parameterizations are relatively heavy because the distance between 4 points have to be calculated.
  1095. *
  1096. * One optimization can be used to reuse distances since this is a sliding window approach.
  1097. * @param data
  1098. * @returns {string}
  1099. * @private
  1100. */
  1101. LineGraph.prototype._catmullRom = function(data, group) {
  1102. var alpha = group.options.catmullRom.alpha;
  1103. if (alpha == 0 || alpha === undefined) {
  1104. return this._catmullRomUniform(data);
  1105. }
  1106. else {
  1107. var p0, p1, p2, p3, bp1, bp2, d1,d2,d3, A, B, N, M;
  1108. var d3powA, d2powA, d3pow2A, d2pow2A, d1pow2A, d1powA;
  1109. var d = Math.round(data[0].x) + ',' + Math.round(data[0].y) + ' ';
  1110. var length = data.length;
  1111. for (var i = 0; i < length - 1; i++) {
  1112. p0 = (i == 0) ? data[0] : data[i-1];
  1113. p1 = data[i];
  1114. p2 = data[i+1];
  1115. p3 = (i + 2 < length) ? data[i+2] : p2;
  1116. d1 = Math.sqrt(Math.pow(p0.x - p1.x,2) + Math.pow(p0.y - p1.y,2));
  1117. d2 = Math.sqrt(Math.pow(p1.x - p2.x,2) + Math.pow(p1.y - p2.y,2));
  1118. d3 = Math.sqrt(Math.pow(p2.x - p3.x,2) + Math.pow(p2.y - p3.y,2));
  1119. // Catmull-Rom to Cubic Bezier conversion matrix
  1120. //
  1121. // A = 2d1^2a + 3d1^a * d2^a + d3^2a
  1122. // B = 2d3^2a + 3d3^a * d2^a + d2^2a
  1123. //
  1124. // [ 0 1 0 0 ]
  1125. // [ -d2^2a/N A/N d1^2a/N 0 ]
  1126. // [ 0 d3^2a/M B/M -d2^2a/M ]
  1127. // [ 0 0 1 0 ]
  1128. // [ 0 1 0 0 ]
  1129. // [ -d2pow2a/N A/N d1pow2a/N 0 ]
  1130. // [ 0 d3pow2a/M B/M -d2pow2a/M ]
  1131. // [ 0 0 1 0 ]
  1132. d3powA = Math.pow(d3, alpha);
  1133. d3pow2A = Math.pow(d3,2*alpha);
  1134. d2powA = Math.pow(d2, alpha);
  1135. d2pow2A = Math.pow(d2,2*alpha);
  1136. d1powA = Math.pow(d1, alpha);
  1137. d1pow2A = Math.pow(d1,2*alpha);
  1138. A = 2*d1pow2A + 3*d1powA * d2powA + d2pow2A;
  1139. B = 2*d3pow2A + 3*d3powA * d2powA + d2pow2A;
  1140. N = 3*d1powA * (d1powA + d2powA);
  1141. if (N > 0) {N = 1 / N;}
  1142. M = 3*d3powA * (d3powA + d2powA);
  1143. if (M > 0) {M = 1 / M;}
  1144. bp1 = { x: ((-d2pow2A * p0.x + A*p1.x + d1pow2A * p2.x) * N),
  1145. y: ((-d2pow2A * p0.y + A*p1.y + d1pow2A * p2.y) * N)};
  1146. bp2 = { x: (( d3pow2A * p1.x + B*p2.x - d2pow2A * p3.x) * M),
  1147. y: (( d3pow2A * p1.y + B*p2.y - d2pow2A * p3.y) * M)};
  1148. if (bp1.x == 0 && bp1.y == 0) {bp1 = p1;}
  1149. if (bp2.x == 0 && bp2.y == 0) {bp2 = p2;}
  1150. d += 'C' +
  1151. bp1.x + ',' +
  1152. bp1.y + ' ' +
  1153. bp2.x + ',' +
  1154. bp2.y + ' ' +
  1155. p2.x + ',' +
  1156. p2.y + ' ';
  1157. }
  1158. return d;
  1159. }
  1160. };
  1161. /**
  1162. * this generates the SVG path for a linear drawing between datapoints.
  1163. * @param data
  1164. * @returns {string}
  1165. * @private
  1166. */
  1167. LineGraph.prototype._linear = function(data) {
  1168. // linear
  1169. var d = '';
  1170. for (var i = 0; i < data.length; i++) {
  1171. if (i == 0) {
  1172. d += data[i].x + ',' + data[i].y;
  1173. }
  1174. else {
  1175. d += ' ' + data[i].x + ',' + data[i].y;
  1176. }
  1177. }
  1178. return d;
  1179. };
  1180. module.exports = LineGraph;