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.

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