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.

160 lines
5.4 KiB

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