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.

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