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.

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