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.

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