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.

326 lines
9.4 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
  1. var Emitter = require('emitter-component');
  2. var Hammer = require('../module/hammer');
  3. var util = require('../util');
  4. var DataSet = require('../DataSet');
  5. var DataView = require('../DataView');
  6. var Range = require('./Range');
  7. var Core = require('./Core');
  8. var TimeAxis = require('./component/TimeAxis');
  9. var CurrentTime = require('./component/CurrentTime');
  10. var CustomTime = require('./component/CustomTime');
  11. var LineGraph = require('./component/LineGraph');
  12. var Configurator = require('../shared/Configurator');
  13. var Validator = require('../shared/Validator').default;
  14. var printStyle = require('../shared/Validator').printStyle;
  15. var allOptions = require('./optionsGraph2d').allOptions;
  16. var configureOptions = require('./optionsGraph2d').configureOptions;
  17. /**
  18. * Create a timeline visualization
  19. * @param {HTMLElement} container
  20. * @param {vis.DataSet | Array} [items]
  21. * @param {Object} [options] See Graph2d.setOptions for the available options.
  22. * @constructor
  23. * @extends Core
  24. */
  25. function Graph2d (container, items, groups, options) {
  26. // if the third element is options, the forth is groups (optionally);
  27. if (!(Array.isArray(groups) || groups instanceof DataSet) && groups instanceof Object) {
  28. var forthArgument = options;
  29. options = groups;
  30. groups = forthArgument;
  31. }
  32. var me = this;
  33. this.defaultOptions = {
  34. start: null,
  35. end: null,
  36. autoResize: true,
  37. orientation: {
  38. axis: 'bottom', // axis orientation: 'bottom', 'top', or 'both'
  39. item: 'bottom' // not relevant for Graph2d
  40. },
  41. width: null,
  42. height: null,
  43. maxHeight: null,
  44. minHeight: null
  45. };
  46. this.options = util.deepExtend({}, this.defaultOptions);
  47. // Create the DOM, props, and emitter
  48. this._create(container);
  49. // all components listed here will be repainted automatically
  50. this.components = [];
  51. this.body = {
  52. dom: this.dom,
  53. domProps: this.props,
  54. emitter: {
  55. on: this.on.bind(this),
  56. off: this.off.bind(this),
  57. emit: this.emit.bind(this)
  58. },
  59. hiddenDates: [],
  60. util: {
  61. toScreen: me._toScreen.bind(me),
  62. toGlobalScreen: me._toGlobalScreen.bind(me), // this refers to the root.width
  63. toTime: me._toTime.bind(me),
  64. toGlobalTime : me._toGlobalTime.bind(me)
  65. }
  66. };
  67. // range
  68. this.range = new Range(this.body);
  69. this.components.push(this.range);
  70. this.body.range = this.range;
  71. // time axis
  72. this.timeAxis = new TimeAxis(this.body);
  73. this.components.push(this.timeAxis);
  74. //this.body.util.snap = this.timeAxis.snap.bind(this.timeAxis);
  75. // current time bar
  76. this.currentTime = new CurrentTime(this.body);
  77. this.components.push(this.currentTime);
  78. // item set
  79. this.linegraph = new LineGraph(this.body);
  80. this.components.push(this.linegraph);
  81. this.itemsData = null; // DataSet
  82. this.groupsData = null; // DataSet
  83. this.on('tap', function (event) {
  84. me.emit('click', me.getEventProperties(event))
  85. });
  86. this.on('doubletap', function (event) {
  87. me.emit('doubleClick', me.getEventProperties(event))
  88. });
  89. this.dom.root.oncontextmenu = function (event) {
  90. me.emit('contextmenu', me.getEventProperties(event))
  91. };
  92. // apply options
  93. if (options) {
  94. this.setOptions(options);
  95. }
  96. // IMPORTANT: THIS HAPPENS BEFORE SET ITEMS!
  97. if (groups) {
  98. this.setGroups(groups);
  99. }
  100. // create itemset
  101. if (items) {
  102. this.setItems(items);
  103. }
  104. else {
  105. this._redraw();
  106. }
  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;