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.

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