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.

157 lines
5.2 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
  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 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 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 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 iconWidth
  107. * @param iconHeight
  108. * @returns {{icon: HTMLElement, label: (group.content|*|string), orientation: (.options.yAxisOrientation|*)}}
  109. */
  110. GraphGroup.prototype.getLegend = function (iconWidth, iconHeight, framework, x, y) {
  111. if (framework == undefined || framework == null) {
  112. var svg = document.createElementNS('http://www.w3.org/2000/svg', "svg");
  113. framework = {svg: svg, svgElements:{}, options: this.options, groups: [this]}
  114. }
  115. if (x == undefined || x == null){
  116. x = 0;
  117. }
  118. if (y == undefined || y == null){
  119. y = 0.5 * iconHeight;
  120. }
  121. switch (this.options.style){
  122. case "line":
  123. Lines.drawIcon(this, x, y, iconWidth, iconHeight, framework);
  124. break;
  125. case "points": //explicit no break
  126. case "point":
  127. Points.drawIcon(this, x, y, iconWidth, iconHeight, framework);
  128. break;
  129. case "bar":
  130. Bars.drawIcon(this, x, y, iconWidth, iconHeight, framework);
  131. break;
  132. }
  133. return {icon: framework.svg, label: this.content, orientation: this.options.yAxisOrientation};
  134. };
  135. GraphGroup.prototype.getYRange = function (groupData) {
  136. var yMin = groupData[0].y;
  137. var yMax = groupData[0].y;
  138. for (var j = 0; j < groupData.length; j++) {
  139. yMin = yMin > groupData[j].y ? groupData[j].y : yMin;
  140. yMax = yMax < groupData[j].y ? groupData[j].y : yMax;
  141. }
  142. return {min: yMin, max: yMax, yAxisOrientation: this.options.yAxisOrientation};
  143. };
  144. module.exports = GraphGroup;