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.

293 lines
8.2 KiB

  1. var DataSet = require('../DataSet');
  2. var DataView = require('../DataView');
  3. var Range = require('./Range');
  4. /**
  5. * Creates a container for all data of one specific 3D-graph.
  6. *
  7. * On construction, the container is totally empty; the data
  8. * needs to be initialized with method initializeData().
  9. * Failure to do so will result in the following exception begin thrown
  10. * on instantiation of Graph3D:
  11. *
  12. * Error: Array, DataSet, or DataView expected
  13. *
  14. * @constructor
  15. */
  16. function DataGroup() {
  17. this.dataTable = null; // The original data table
  18. }
  19. /**
  20. * Initializes the instance from the passed data.
  21. *
  22. * Calculates minimum and maximum values and column index values.
  23. *
  24. * The graph3d instance is used internally to access the settings for
  25. * the given instance.
  26. * TODO: Pass settings only instead.
  27. *
  28. * @param {Graph3D} graph3d Reference to the calling Graph3D instance.
  29. * @param {Array | DataSet | DataView} rawData The data containing the items for
  30. * the Graph.
  31. * @param {Number} style Style Number
  32. */
  33. DataGroup.prototype.initializeData = function(graph3d, rawData, style) {
  34. if (rawData === undefined) return;
  35. if (Array.isArray(rawData)) {
  36. rawData = new DataSet(rawData);
  37. }
  38. var data;
  39. if (rawData instanceof DataSet || rawData instanceof DataView) {
  40. data = rawData.get();
  41. }
  42. else {
  43. throw new Error('Array, DataSet, or DataView expected');
  44. }
  45. if (data.length == 0) return;
  46. // unsubscribe from the dataTable
  47. if (this.dataSet) {
  48. this.dataSet.off('*', this._onChange);
  49. }
  50. this.dataSet = rawData;
  51. this.dataTable = data;
  52. // subscribe to changes in the dataset
  53. var me = this;
  54. this._onChange = function () {
  55. graph3d.setData(me.dataSet);
  56. };
  57. this.dataSet.on('*', this._onChange);
  58. // determine the location of x,y,z,value,filter columns
  59. this.colX = 'x';
  60. this.colY = 'y';
  61. this.colZ = 'z';
  62. var withBars = graph3d.hasBars(style);
  63. // determine barWidth from data
  64. if (withBars) {
  65. if (graph3d.defaultXBarWidth !== undefined) {
  66. this.xBarWidth = graph3d.defaultXBarWidth;
  67. }
  68. else {
  69. this.xBarWidth = this.getSmallestDifference(data, this.colX) || 1;
  70. }
  71. if (graph3d.defaultYBarWidth !== undefined) {
  72. this.yBarWidth = graph3d.defaultYBarWidth;
  73. }
  74. else {
  75. this.yBarWidth = this.getSmallestDifference(data, this.colY) || 1;
  76. }
  77. }
  78. // calculate minima and maxima
  79. this._initializeRange(data, this.colX, graph3d, withBars);
  80. this._initializeRange(data, this.colY, graph3d, withBars);
  81. this._initializeRange(data, this.colZ, graph3d, false);
  82. if (data[0].hasOwnProperty('style')) {
  83. this.colValue = 'style';
  84. var valueRange = this.getColumnRange(data, this.colValue);
  85. this._setRangeDefaults(valueRange, graph3d.defaultValueMin, graph3d.defaultValueMax);
  86. this.valueRange = valueRange;
  87. }
  88. };
  89. /**
  90. * Collect the range settings for the given data column.
  91. *
  92. * This internal method is intended to make the range
  93. * initalization more generic.
  94. *
  95. * TODO: if/when combined settings per axis defined, get rid of this.
  96. *
  97. * @private
  98. *
  99. * @param {'x'|'y'|'z'} column The data column to process
  100. * @param {Graph3D} graph3d Reference to the calling Graph3D instance;
  101. * required for access to settings
  102. */
  103. DataGroup.prototype._collectRangeSettings = function(column, graph3d) {
  104. var index = ['x', 'y', 'z'].indexOf(column);
  105. if (index == -1) {
  106. throw new Error('Column \'' + column + '\' invalid');
  107. }
  108. var upper = column.toUpperCase();
  109. return {
  110. barWidth : this[column + 'BarWidth'],
  111. min : graph3d['default' + upper + 'Min'],
  112. max : graph3d['default' + upper + 'Max'],
  113. step : graph3d['default' + upper + 'Step'],
  114. range_label: column + 'Range', // Name of instance field to write to
  115. step_label : column + 'Step' // Name of instance field to write to
  116. };
  117. }
  118. /**
  119. * Initializes the settings per given column.
  120. *
  121. * TODO: if/when combined settings per axis defined, rewrite this.
  122. *
  123. * @private
  124. *
  125. * @param {DataSet | DataView} data The data containing the items for the Graph
  126. * @param {'x'|'y'|'z'} column The data column to process
  127. * @param {Graph3D} graph3d Reference to the calling Graph3D instance;
  128. * required for access to settings
  129. * @param {Boolean} withBars True if initializing for bar graph
  130. */
  131. DataGroup.prototype._initializeRange = function(data, column, graph3d, withBars) {
  132. var NUMSTEPS = 5;
  133. var settings = this._collectRangeSettings(column, graph3d);
  134. var range = this.getColumnRange(data, column);
  135. if (withBars && column != 'z') { // Safeguard for 'z'; it doesn't have a bar width
  136. range.expand(settings.barWidth / 2);
  137. }
  138. this._setRangeDefaults(range, settings.min, settings.max);
  139. this[settings.range_label] = range;
  140. this[settings.step_label ] = (settings.step !== undefined) ? settings.step : range.range()/NUMSTEPS;
  141. }
  142. /**
  143. * Creates a list with all the different values in the data for the given column.
  144. *
  145. * If no data passed, use the internal data of this instance.
  146. *
  147. * @param {'x'|'y'|'z'} column The data column to process
  148. * @param {DataSet|DataView|undefined} data The data containing the items for the Graph
  149. *
  150. * @returns {Array} All distinct values in the given column data, sorted ascending.
  151. */
  152. DataGroup.prototype.getDistinctValues = function(column, data) {
  153. if (data === undefined) {
  154. data = this.dataTable;
  155. }
  156. var values = [];
  157. for (var i = 0; i < data.length; i++) {
  158. var value = data[i][column] || 0;
  159. if (values.indexOf(value) === -1) {
  160. values.push(value);
  161. }
  162. }
  163. return values.sort(function(a,b) { return a - b; });
  164. };
  165. /**
  166. * Determine the smallest difference between the values for given
  167. * column in the passed data set.
  168. *
  169. * @param {DataSet|DataView|undefined} data The data containing the items for the Graph
  170. * @param {'x'|'y'|'z'} column The data column to process
  171. *
  172. * @returns {Number|null} Smallest difference value or
  173. * null, if it can't be determined.
  174. */
  175. DataGroup.prototype.getSmallestDifference = function(data, column) {
  176. var values = this.getDistinctValues(data, column);
  177. // Get all the distinct diffs
  178. // Array values is assumed to be sorted here
  179. var smallest_diff = null;
  180. for (var i = 1; i < values.length; i++) {
  181. var diff = values[i] - values[i - 1];
  182. if (smallest_diff == null || smallest_diff > diff ) {
  183. smallest_diff = diff;
  184. }
  185. }
  186. return smallest_diff;
  187. }
  188. /**
  189. * Get the absolute min/max values for the passed data column.
  190. *
  191. * @param {DataSet|DataView|undefined} data The data containing the items for the Graph
  192. * @param {'x'|'y'|'z'} column The data column to process
  193. *
  194. * @returns {Range} A Range instance with min/max members properly set.
  195. */
  196. DataGroup.prototype.getColumnRange = function(data, column) {
  197. var range = new Range();
  198. // Adjust the range so that it covers all values in the passed data elements.
  199. for (var i = 0; i < data.length; i++) {
  200. var item = data[i][column];
  201. range.adjust(item);
  202. }
  203. return range;
  204. };
  205. /**
  206. * Determines the number of rows in the current data.
  207. *
  208. * @returns {Number}
  209. */
  210. DataGroup.prototype.getNumberOfRows = function() {
  211. return this.dataTable.length;
  212. }
  213. /**
  214. * Set default values for range
  215. *
  216. * The default values override the range values, if defined.
  217. *
  218. * Because it's possible that only defaultMin or defaultMax is set, it's better
  219. * to pass in a range already set with the min/max set from the data. Otherwise,
  220. * it's quite hard to process the min/max properly.
  221. */
  222. DataGroup.prototype._setRangeDefaults = function (range, defaultMin, defaultMax) {
  223. if (defaultMin !== undefined) {
  224. range.min = defaultMin;
  225. }
  226. if (defaultMax !== undefined) {
  227. range.max = defaultMax;
  228. }
  229. // This is the original way that the default min/max values were adjusted.
  230. // TODO: Perhaps it's better if an error is thrown if the values do not agree.
  231. // But this will change the behaviour.
  232. if (range.max <= range.min) range.max = range.min + 1;
  233. };
  234. DataGroup.prototype.getDataTable = function() {
  235. return this.dataTable;
  236. };
  237. DataGroup.prototype.getDataSet = function() {
  238. return this.dataSet;
  239. };
  240. module.exports = DataGroup;