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.

168 lines
5.4 KiB

  1. var util = require('../../util');
  2. var DOMutil = require('../../DOMutil');
  3. var Bars = require('./graph2d_types/bar');
  4. var Lines = require('./graph2d_types/line');
  5. var Points = require('./graph2d_types/points');
  6. /**
  7. * /**
  8. * @param {object} group | the object of the group from the dataset
  9. * @param {string} groupId | ID of the group
  10. * @param {object} options | the default options
  11. * @param {array} groupsUsingDefaultStyles | this array has one entree.
  12. * It is passed as an array so it is passed by reference.
  13. * It enumerates through the default styles
  14. * @constructor
  15. */
  16. function GraphGroup(group, groupId, options, groupsUsingDefaultStyles) {
  17. this.id = groupId;
  18. var fields = ['sampling', 'style', 'sort', 'yAxisOrientation', 'barChart', 'drawPoints', 'shaded', 'interpolation'];
  19. this.options = util.selectiveBridgeObject(fields, options);
  20. this.usingDefaultStyle = group.className === undefined;
  21. this.groupsUsingDefaultStyles = groupsUsingDefaultStyles;
  22. this.zeroPosition = 0;
  23. this.update(group);
  24. if (this.usingDefaultStyle == true) {
  25. this.groupsUsingDefaultStyles[0] += 1;
  26. }
  27. this.itemsData = [];
  28. this.visible = group.visible === undefined ? true : group.visible;
  29. }
  30. function insertionSort (a,compare) {
  31. for (var i = 0; i < a.length; i++) {
  32. var k = a[i];
  33. for (var j = i; j > 0 && compare(k,a[j - 1])<0; j--) {
  34. a[j] = a[j - 1];
  35. }
  36. a[j] = k;
  37. }
  38. return a;
  39. }
  40. /**
  41. * this loads a reference to all items in this group into this group.
  42. * @param {array} items
  43. */
  44. GraphGroup.prototype.setItems = function (items) {
  45. if (items != null) {
  46. this.itemsData = items;
  47. if (this.options.sort == true) {
  48. insertionSort(this.itemsData,function (a, b) {
  49. return a.x > b.x ? 1 : -1;
  50. });
  51. }
  52. }
  53. else {
  54. this.itemsData = [];
  55. }
  56. };
  57. GraphGroup.prototype.getItems = function () {
  58. return this.itemsData;
  59. }
  60. /**
  61. * this is used for barcharts and shading, this way, we only have to calculate it once.
  62. * @param pos
  63. */
  64. GraphGroup.prototype.setZeroPosition = function (pos) {
  65. this.zeroPosition = pos;
  66. };
  67. /**
  68. * set the options of the graph group over the default options.
  69. * @param options
  70. */
  71. GraphGroup.prototype.setOptions = function (options) {
  72. if (options !== undefined) {
  73. var fields = ['sampling', 'style', 'sort', 'yAxisOrientation', 'barChart', 'excludeFromLegend', 'excludeFromStacking'];
  74. util.selectiveDeepExtend(fields, this.options, options);
  75. // if the group's drawPoints is a function delegate the callback to the onRender property
  76. if (typeof options.drawPoints == 'function') {
  77. options.drawPoints = {
  78. onRender: options.drawPoints
  79. }
  80. }
  81. util.mergeOptions(this.options, options, 'interpolation');
  82. util.mergeOptions(this.options, options, 'drawPoints');
  83. util.mergeOptions(this.options, options, 'shaded');
  84. if (options.interpolation) {
  85. if (typeof options.interpolation == 'object') {
  86. if (options.interpolation.parametrization) {
  87. if (options.interpolation.parametrization == 'uniform') {
  88. this.options.interpolation.alpha = 0;
  89. }
  90. else if (options.interpolation.parametrization == 'chordal') {
  91. this.options.interpolation.alpha = 1.0;
  92. }
  93. else {
  94. this.options.interpolation.parametrization = 'centripetal';
  95. this.options.interpolation.alpha = 0.5;
  96. }
  97. }
  98. }
  99. }
  100. }
  101. };
  102. /**
  103. * this updates the current group class with the latest group dataset entree, used in _updateGroup in linegraph
  104. * @param group
  105. */
  106. GraphGroup.prototype.update = function (group) {
  107. this.group = group;
  108. this.content = group.content || 'graph';
  109. this.className = group.className || this.className || 'vis-graph-group' + this.groupsUsingDefaultStyles[0] % 10;
  110. this.visible = group.visible === undefined ? true : group.visible;
  111. this.style = group.style;
  112. this.setOptions(group.options);
  113. };
  114. /**
  115. * return the legend entree for this group.
  116. *
  117. * @param iconWidth
  118. * @param iconHeight
  119. * @returns {{icon: HTMLElement, label: (group.content|*|string), orientation: (.options.yAxisOrientation|*)}}
  120. */
  121. GraphGroup.prototype.getLegend = function (iconWidth, iconHeight, framework, x, y) {
  122. if (framework == undefined || framework == null) {
  123. var svg = document.createElementNS('http://www.w3.org/2000/svg', "svg");
  124. framework = {svg: svg, svgElements:{}, options: this.options, groups: [this]}
  125. }
  126. if (x == undefined || x == null){
  127. x = 0;
  128. }
  129. if (y == undefined || y == null){
  130. y = 0.5 * iconHeight;
  131. }
  132. switch (this.options.style){
  133. case "line":
  134. Lines.drawIcon(this, x, y, iconWidth, iconHeight, framework);
  135. break;
  136. case "points":
  137. Points.drawIcon(this, x, y, iconWidth, iconHeight, framework);
  138. break;
  139. case "bar":
  140. Bars.drawIcon(this, x, y, iconWidth, iconHeight, framework);
  141. break;
  142. }
  143. return {icon: framework.svg, label: this.content, orientation: this.options.yAxisOrientation};
  144. };
  145. GraphGroup.prototype.getYRange = function (groupData) {
  146. var yMin = groupData[0].y;
  147. var yMax = groupData[0].y;
  148. for (var j = 0; j < groupData.length; j++) {
  149. yMin = yMin > groupData[j].y ? groupData[j].y : yMin;
  150. yMax = yMax < groupData[j].y ? groupData[j].y : yMax;
  151. }
  152. return {min: yMin, max: yMax, yAxisOrientation: this.options.yAxisOrientation};
  153. };
  154. module.exports = GraphGroup;