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.

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