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.

328 lines
9.5 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
  1. var Emitter = require('emitter-component');
  2. var Hammer = require('../module/hammer');
  3. var moment = require('../module/moment');
  4. var util = require('../util');
  5. var DataSet = require('../DataSet');
  6. var DataView = require('../DataView');
  7. var Range = require('./Range');
  8. var Core = require('./Core');
  9. var TimeAxis = require('./component/TimeAxis');
  10. var CurrentTime = require('./component/CurrentTime');
  11. var CustomTime = require('./component/CustomTime');
  12. var LineGraph = require('./component/LineGraph');
  13. var Configurator = require('../shared/Configurator');
  14. var Validator = require('../shared/Validator').default;
  15. var printStyle = require('../shared/Validator').printStyle;
  16. var allOptions = require('./optionsGraph2d').allOptions;
  17. var configureOptions = require('./optionsGraph2d').configureOptions;
  18. /**
  19. * Create a timeline visualization
  20. * @param {HTMLElement} container
  21. * @param {vis.DataSet | Array} [items]
  22. * @param {Object} [options] See Graph2d.setOptions for the available options.
  23. * @constructor
  24. * @extends Core
  25. */
  26. function Graph2d (container, items, groups, options) {
  27. // if the third element is options, the forth is groups (optionally);
  28. if (!(Array.isArray(groups) || groups instanceof DataSet || groups instanceof DataView) && groups instanceof Object) {
  29. var forthArgument = options;
  30. options = groups;
  31. groups = forthArgument;
  32. }
  33. var me = this;
  34. this.defaultOptions = {
  35. start: null,
  36. end: null,
  37. autoResize: true,
  38. orientation: {
  39. axis: 'bottom', // axis orientation: 'bottom', 'top', or 'both'
  40. item: 'bottom' // not relevant for Graph2d
  41. },
  42. moment: moment,
  43. width: null,
  44. height: null,
  45. maxHeight: null,
  46. minHeight: null
  47. };
  48. this.options = util.deepExtend({}, this.defaultOptions);
  49. // Create the DOM, props, and emitter
  50. this._create(container);
  51. // all components listed here will be repainted automatically
  52. this.components = [];
  53. this.body = {
  54. dom: this.dom,
  55. domProps: this.props,
  56. emitter: {
  57. on: this.on.bind(this),
  58. off: this.off.bind(this),
  59. emit: this.emit.bind(this)
  60. },
  61. hiddenDates: [],
  62. util: {
  63. toScreen: me._toScreen.bind(me),
  64. toGlobalScreen: me._toGlobalScreen.bind(me), // this refers to the root.width
  65. toTime: me._toTime.bind(me),
  66. toGlobalTime : me._toGlobalTime.bind(me)
  67. }
  68. };
  69. // range
  70. this.range = new Range(this.body);
  71. this.components.push(this.range);
  72. this.body.range = this.range;
  73. // time axis
  74. this.timeAxis = new TimeAxis(this.body);
  75. this.components.push(this.timeAxis);
  76. //this.body.util.snap = this.timeAxis.snap.bind(this.timeAxis);
  77. // current time bar
  78. this.currentTime = new CurrentTime(this.body);
  79. this.components.push(this.currentTime);
  80. // item set
  81. this.linegraph = new LineGraph(this.body);
  82. this.components.push(this.linegraph);
  83. this.itemsData = null; // DataSet
  84. this.groupsData = null; // DataSet
  85. this.on('tap', function (event) {
  86. me.emit('click', me.getEventProperties(event))
  87. });
  88. this.on('doubletap', function (event) {
  89. me.emit('doubleClick', me.getEventProperties(event))
  90. });
  91. this.dom.root.oncontextmenu = function (event) {
  92. me.emit('contextmenu', me.getEventProperties(event))
  93. };
  94. // apply options
  95. if (options) {
  96. this.setOptions(options);
  97. }
  98. // IMPORTANT: THIS HAPPENS BEFORE SET ITEMS!
  99. if (groups) {
  100. this.setGroups(groups);
  101. }
  102. // create itemset
  103. if (items) {
  104. this.setItems(items);
  105. }
  106. this._redraw();
  107. }
  108. // Extend the functionality from Core
  109. Graph2d.prototype = new Core();
  110. Graph2d.prototype.setOptions = function (options) {
  111. // validate options
  112. let errorFound = Validator.validate(options, allOptions);
  113. if (errorFound === true) {
  114. console.log('%cErrors have been found in the supplied options object.', printStyle);
  115. }
  116. Core.prototype.setOptions.call(this, options);
  117. };
  118. /**
  119. * Set items
  120. * @param {vis.DataSet | Array | null} items
  121. */
  122. Graph2d.prototype.setItems = function(items) {
  123. var initialLoad = (this.itemsData == null);
  124. // convert to type DataSet when needed
  125. var newDataSet;
  126. if (!items) {
  127. newDataSet = null;
  128. }
  129. else if (items instanceof DataSet || items instanceof DataView) {
  130. newDataSet = items;
  131. }
  132. else {
  133. // turn an array into a dataset
  134. newDataSet = new DataSet(items, {
  135. type: {
  136. start: 'Date',
  137. end: 'Date'
  138. }
  139. });
  140. }
  141. // set items
  142. this.itemsData = newDataSet;
  143. this.linegraph && this.linegraph.setItems(newDataSet);
  144. if (initialLoad) {
  145. if (this.options.start != undefined || this.options.end != undefined) {
  146. var start = this.options.start != undefined ? this.options.start : null;
  147. var end = this.options.end != undefined ? this.options.end : null;
  148. this.setWindow(start, end, {animation: false});
  149. }
  150. else {
  151. this.fit({animation: false});
  152. }
  153. }
  154. };
  155. /**
  156. * Set groups
  157. * @param {vis.DataSet | Array} groups
  158. */
  159. Graph2d.prototype.setGroups = function(groups) {
  160. // convert to type DataSet when needed
  161. var newDataSet;
  162. if (!groups) {
  163. newDataSet = null;
  164. }
  165. else if (groups instanceof DataSet || groups instanceof DataView) {
  166. newDataSet = groups;
  167. }
  168. else {
  169. // turn an array into a dataset
  170. newDataSet = new DataSet(groups);
  171. }
  172. this.groupsData = newDataSet;
  173. this.linegraph.setGroups(newDataSet);
  174. };
  175. /**
  176. * Returns an object containing an SVG element with the icon of the group (size determined by iconWidth and iconHeight), the label of the group (content) and the yAxisOrientation of the group (left or right).
  177. * @param groupId
  178. * @param width
  179. * @param height
  180. */
  181. Graph2d.prototype.getLegend = function(groupId, width, height) {
  182. if (width === undefined) {width = 15;}
  183. if (height === undefined) {height = 15;}
  184. if (this.linegraph.groups[groupId] !== undefined) {
  185. return this.linegraph.groups[groupId].getLegend(width,height);
  186. }
  187. else {
  188. return "cannot find group:'" + groupId + "'";
  189. }
  190. };
  191. /**
  192. * This checks if the visible option of the supplied group (by ID) is true or false.
  193. * @param groupId
  194. * @returns {*}
  195. */
  196. Graph2d.prototype.isGroupVisible = function(groupId) {
  197. if (this.linegraph.groups[groupId] !== undefined) {
  198. return (this.linegraph.groups[groupId].visible && (this.linegraph.options.groups.visibility[groupId] === undefined || this.linegraph.options.groups.visibility[groupId] == true));
  199. }
  200. else {
  201. return false;
  202. }
  203. };
  204. /**
  205. * Get the data range of the item set.
  206. * @returns {{min: Date, max: Date}} range A range with a start and end Date.
  207. * When no minimum is found, min==null
  208. * When no maximum is found, max==null
  209. */
  210. Graph2d.prototype.getDataRange = function() {
  211. var min = null;
  212. var max = null;
  213. // calculate min from start filed
  214. for (var groupId in this.linegraph.groups) {
  215. if (this.linegraph.groups.hasOwnProperty(groupId)) {
  216. if (this.linegraph.groups[groupId].visible == true) {
  217. for (var i = 0; i < this.linegraph.groups[groupId].itemsData.length; i++) {
  218. var item = this.linegraph.groups[groupId].itemsData[i];
  219. var value = util.convert(item.x, 'Date').valueOf();
  220. min = min == null ? value : min > value ? value : min;
  221. max = max == null ? value : max < value ? value : max;
  222. }
  223. }
  224. }
  225. }
  226. return {
  227. min: (min != null) ? new Date(min) : null,
  228. max: (max != null) ? new Date(max) : null
  229. };
  230. };
  231. /**
  232. * Generate Timeline related information from an event
  233. * @param {Event} event
  234. * @return {Object} An object with related information, like on which area
  235. * The event happened, whether clicked on an item, etc.
  236. */
  237. Graph2d.prototype.getEventProperties = function (event) {
  238. var clientX = event.center ? event.center.x : event.clientX;
  239. var clientY = event.center ? event.center.y : event.clientY;
  240. var x = clientX - util.getAbsoluteLeft(this.dom.centerContainer);
  241. var y = clientY - util.getAbsoluteTop(this.dom.centerContainer);
  242. var time = this._toTime(x);
  243. var customTime = CustomTime.customTimeFromTarget(event);
  244. var element = util.getTarget(event);
  245. var what = null;
  246. if (util.hasParent(element, this.timeAxis.dom.foreground)) {what = 'axis';}
  247. else if (this.timeAxis2 && util.hasParent(element, this.timeAxis2.dom.foreground)) {what = 'axis';}
  248. else if (util.hasParent(element, this.linegraph.yAxisLeft.dom.frame)) {what = 'data-axis';}
  249. else if (util.hasParent(element, this.linegraph.yAxisRight.dom.frame)) {what = 'data-axis';}
  250. else if (util.hasParent(element, this.linegraph.legendLeft.dom.frame)) {what = 'legend';}
  251. else if (util.hasParent(element, this.linegraph.legendRight.dom.frame)) {what = 'legend';}
  252. else if (customTime != null) {what = 'custom-time';}
  253. else if (util.hasParent(element, this.currentTime.bar)) {what = 'current-time';}
  254. else if (util.hasParent(element, this.dom.center)) {what = 'background';}
  255. var value = [];
  256. var yAxisLeft = this.linegraph.yAxisLeft;
  257. var yAxisRight = this.linegraph.yAxisRight;
  258. if (!yAxisLeft.hidden) {
  259. value.push(yAxisLeft.screenToValue(y));
  260. }
  261. if (!yAxisRight.hidden) {
  262. value.push(yAxisRight.screenToValue(y));
  263. }
  264. return {
  265. event: event,
  266. what: what,
  267. pageX: event.srcEvent ? event.srcEvent.pageX : event.pageX,
  268. pageY: event.srcEvent ? event.srcEvent.pageY : event.pageY,
  269. x: x,
  270. y: y,
  271. time: time,
  272. value: value
  273. }
  274. };
  275. /**
  276. * Load a configurator
  277. * @return {Object}
  278. * @private
  279. */
  280. Graph2d.prototype._createConfigurator = function () {
  281. return new Configurator(this, this.dom.container, configureOptions);
  282. };
  283. module.exports = Graph2d;