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.

312 lines
9.0 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
  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 ConfigurationSystem = require('../shared/ConfigurationSystem');
  13. var Validator = require('../shared/Validator').default;
  14. var printStyle = require('../shared/Validator').printStyle;
  15. var allOptions = require('./graph2dOptions').allOptions;
  16. var configureOptions = require('./graph2dOptions').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. let errorFound = Validator.validate(options, allOptions);
  95. if (errorFound === true) {
  96. console.log('%cErrors have been found in the supplied options object.', printStyle);
  97. }
  98. this.setOptions(options);
  99. }
  100. // IMPORTANT: THIS HAPPENS BEFORE SET ITEMS!
  101. if (groups) {
  102. this.setGroups(groups);
  103. }
  104. // create itemset
  105. if (items) {
  106. this.setItems(items);
  107. }
  108. else {
  109. this._redraw();
  110. }
  111. }
  112. // Extend the functionality from Core
  113. Graph2d.prototype = new Core();
  114. /**
  115. * Set items
  116. * @param {vis.DataSet | Array | null} items
  117. */
  118. Graph2d.prototype.setItems = function(items) {
  119. var initialLoad = (this.itemsData == null);
  120. // convert to type DataSet when needed
  121. var newDataSet;
  122. if (!items) {
  123. newDataSet = null;
  124. }
  125. else if (items instanceof DataSet || items instanceof DataView) {
  126. newDataSet = items;
  127. }
  128. else {
  129. // turn an array into a dataset
  130. newDataSet = new DataSet(items, {
  131. type: {
  132. start: 'Date',
  133. end: 'Date'
  134. }
  135. });
  136. }
  137. // set items
  138. this.itemsData = newDataSet;
  139. this.linegraph && this.linegraph.setItems(newDataSet);
  140. if (initialLoad) {
  141. if (this.options.start != undefined || this.options.end != undefined) {
  142. var start = this.options.start != undefined ? this.options.start : null;
  143. var end = this.options.end != undefined ? this.options.end : null;
  144. this.setWindow(start, end, {animation: false});
  145. }
  146. else {
  147. this.fit({animation: false});
  148. }
  149. }
  150. };
  151. /**
  152. * Set groups
  153. * @param {vis.DataSet | Array} groups
  154. */
  155. Graph2d.prototype.setGroups = function(groups) {
  156. // convert to type DataSet when needed
  157. var newDataSet;
  158. if (!groups) {
  159. newDataSet = null;
  160. }
  161. else if (groups instanceof DataSet || groups instanceof DataView) {
  162. newDataSet = groups;
  163. }
  164. else {
  165. // turn an array into a dataset
  166. newDataSet = new DataSet(groups);
  167. }
  168. this.groupsData = newDataSet;
  169. this.linegraph.setGroups(newDataSet);
  170. };
  171. /**
  172. * 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).
  173. * @param groupId
  174. * @param width
  175. * @param height
  176. */
  177. Graph2d.prototype.getLegend = function(groupId, width, height) {
  178. if (width === undefined) {width = 15;}
  179. if (height === undefined) {height = 15;}
  180. if (this.linegraph.groups[groupId] !== undefined) {
  181. return this.linegraph.groups[groupId].getLegend(width,height);
  182. }
  183. else {
  184. return "cannot find group:" + groupId;
  185. }
  186. };
  187. /**
  188. * This checks if the visible option of the supplied group (by ID) is true or false.
  189. * @param groupId
  190. * @returns {*}
  191. */
  192. Graph2d.prototype.isGroupVisible = function(groupId) {
  193. if (this.linegraph.groups[groupId] !== undefined) {
  194. return (this.linegraph.groups[groupId].visible && (this.linegraph.options.groups.visibility[groupId] === undefined || this.linegraph.options.groups.visibility[groupId] == true));
  195. }
  196. else {
  197. return false;
  198. }
  199. };
  200. /**
  201. * Get the data range of the item set.
  202. * @returns {{min: Date, max: Date}} range A range with a start and end Date.
  203. * When no minimum is found, min==null
  204. * When no maximum is found, max==null
  205. */
  206. Graph2d.prototype.getItemRange = function() {
  207. var min = null;
  208. var max = null;
  209. // calculate min from start filed
  210. for (var groupId in this.linegraph.groups) {
  211. if (this.linegraph.groups.hasOwnProperty(groupId)) {
  212. if (this.linegraph.groups[groupId].visible == true) {
  213. for (var i = 0; i < this.linegraph.groups[groupId].itemsData.length; i++) {
  214. var item = this.linegraph.groups[groupId].itemsData[i];
  215. var value = util.convert(item.x, 'Date').valueOf();
  216. min = min == null ? value : min > value ? value : min;
  217. max = max == null ? value : max < value ? value : max;
  218. }
  219. }
  220. }
  221. }
  222. return {
  223. min: (min != null) ? new Date(min) : null,
  224. max: (max != null) ? new Date(max) : null
  225. };
  226. };
  227. /**
  228. * Generate Timeline related information from an event
  229. * @param {Event} event
  230. * @return {Object} An object with related information, like on which area
  231. * The event happened, whether clicked on an item, etc.
  232. */
  233. Graph2d.prototype.getEventProperties = function (event) {
  234. var pageX = event.center ? event.center.x : event.pageX;
  235. var pageY = event.center ? event.center.y : event.pageY;
  236. var x = pageX - util.getAbsoluteLeft(this.dom.centerContainer);
  237. var y = pageY - util.getAbsoluteTop(this.dom.centerContainer);
  238. var time = this._toTime(x);
  239. var customTime = CustomTime.customTimeFromTarget(event);
  240. var element = util.getTarget(event);
  241. var what = null;
  242. if (util.hasParent(element, this.timeAxis.dom.foreground)) {what = 'axis';}
  243. else if (this.timeAxis2 && util.hasParent(element, this.timeAxis2.dom.foreground)) {what = 'axis';}
  244. else if (util.hasParent(element, this.linegraph.yAxisLeft.dom.frame)) {what = 'data-axis';}
  245. else if (util.hasParent(element, this.linegraph.yAxisRight.dom.frame)) {what = 'data-axis';}
  246. else if (util.hasParent(element, this.linegraph.legendLeft.dom.frame)) {what = 'legend';}
  247. else if (util.hasParent(element, this.linegraph.legendRight.dom.frame)) {what = 'legend';}
  248. else if (customTime != null) {what = 'custom-time';}
  249. else if (util.hasParent(element, this.currentTime.bar)) {what = 'current-time';}
  250. else if (util.hasParent(element, this.dom.center)) {what = 'background';}
  251. var value = [];
  252. var yAxisLeft = this.linegraph.yAxisLeft;
  253. var yAxisRight = this.linegraph.yAxisRight;
  254. if (!yAxisLeft.hidden) {
  255. value.push(yAxisLeft.screenToValue(y));
  256. }
  257. if (!yAxisRight.hidden) {
  258. value.push(yAxisRight.screenToValue(y));
  259. }
  260. return {
  261. event: event,
  262. what: what,
  263. pageX: pageX,
  264. pageY: pageY,
  265. x: x,
  266. y: y,
  267. time: time,
  268. value: value
  269. }
  270. };
  271. module.exports = Graph2d;