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.

243 lines
6.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
  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 (!(groups instanceof Array || groups instanceof vis.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. util: {
  52. snap: null, // will be specified after TimeAxis is created
  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. // apply options
  80. if (options) {
  81. this.setOptions(options);
  82. }
  83. // IMPORTANT: THIS HAPPENS BEFORE SET ITEMS!
  84. if (groups) {
  85. this.setGroups(groups);
  86. }
  87. // create itemset
  88. if (items) {
  89. this.setItems(items);
  90. }
  91. else {
  92. this.redraw();
  93. }
  94. }
  95. // Extend the functionality from Core
  96. Graph2d.prototype = new Core();
  97. /**
  98. * Set items
  99. * @param {vis.DataSet | Array | google.visualization.DataTable | null} items
  100. */
  101. Graph2d.prototype.setItems = function(items) {
  102. var initialLoad = (this.itemsData == null);
  103. // convert to type DataSet when needed
  104. var newDataSet;
  105. if (!items) {
  106. newDataSet = null;
  107. }
  108. else if (items instanceof DataSet || items instanceof DataView) {
  109. newDataSet = items;
  110. }
  111. else {
  112. // turn an array into a dataset
  113. newDataSet = new DataSet(items, {
  114. type: {
  115. start: 'Date',
  116. end: 'Date'
  117. }
  118. });
  119. }
  120. // set items
  121. this.itemsData = newDataSet;
  122. this.linegraph && this.linegraph.setItems(newDataSet);
  123. if (initialLoad) {
  124. if (this.options.start != undefined || this.options.end != undefined) {
  125. var start = this.options.start != undefined ? this.options.start : null;
  126. var end = this.options.end != undefined ? this.options.end : null;
  127. this.setWindow(start, end, {animate: false});
  128. }
  129. else {
  130. this.fit({animate: false});
  131. }
  132. }
  133. };
  134. /**
  135. * Set groups
  136. * @param {vis.DataSet | Array | google.visualization.DataTable} groups
  137. */
  138. Graph2d.prototype.setGroups = function(groups) {
  139. // convert to type DataSet when needed
  140. var newDataSet;
  141. if (!groups) {
  142. newDataSet = null;
  143. }
  144. else if (groups instanceof DataSet || groups instanceof DataView) {
  145. newDataSet = groups;
  146. }
  147. else {
  148. // turn an array into a dataset
  149. newDataSet = new DataSet(groups);
  150. }
  151. this.groupsData = newDataSet;
  152. this.linegraph.setGroups(newDataSet);
  153. };
  154. /**
  155. * 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).
  156. * @param groupId
  157. * @param width
  158. * @param height
  159. */
  160. Graph2d.prototype.getLegend = function(groupId, width, height) {
  161. if (width === undefined) {width = 15;}
  162. if (height === undefined) {height = 15;}
  163. if (this.linegraph.groups[groupId] !== undefined) {
  164. return this.linegraph.groups[groupId].getLegend(width,height);
  165. }
  166. else {
  167. return "cannot find group:" + groupId;
  168. }
  169. }
  170. /**
  171. * This checks if the visible option of the supplied group (by ID) is true or false.
  172. * @param groupId
  173. * @returns {*}
  174. */
  175. Graph2d.prototype.isGroupVisible = function(groupId) {
  176. if (this.linegraph.groups[groupId] !== undefined) {
  177. return (this.linegraph.groups[groupId].visible && (this.linegraph.options.groups.visibility[groupId] === undefined || this.linegraph.options.groups.visibility[groupId] == true));
  178. }
  179. else {
  180. return false;
  181. }
  182. }
  183. /**
  184. * Get the data range of the item set.
  185. * @returns {{min: Date, max: Date}} range A range with a start and end Date.
  186. * When no minimum is found, min==null
  187. * When no maximum is found, max==null
  188. */
  189. Graph2d.prototype.getItemRange = function() {
  190. var min = null;
  191. var max = null;
  192. // calculate min from start filed
  193. for (var groupId in this.linegraph.groups) {
  194. if (this.linegraph.groups.hasOwnProperty(groupId)) {
  195. if (this.linegraph.groups[groupId].visible == true) {
  196. for (var i = 0; i < this.linegraph.groups[groupId].itemsData.length; i++) {
  197. var item = this.linegraph.groups[groupId].itemsData[i];
  198. var value = util.convert(item.x, 'Date').valueOf();
  199. min = min == null ? value : min > value ? value : min;
  200. max = max == null ? value : max < value ? value : max;
  201. }
  202. }
  203. }
  204. }
  205. return {
  206. min: (min != null) ? new Date(min) : null,
  207. max: (max != null) ? new Date(max) : null
  208. };
  209. };
  210. module.exports = Graph2d;