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.

219 lines
7.6 KiB

  1. var util = require('../../util');
  2. var DOMutil = require('../../DOMutil');
  3. var Line = require('./graph2d_types/line');
  4. var Bar = require('./graph2d_types/bar');
  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. /**
  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. this.itemsData.sort(function (a, b) {
  39. return a.x - b.x;
  40. })
  41. }
  42. // typecast all items to numbers. Takes around 10ms for 500.000 items
  43. for (var i = 0; i < this.itemsData.length; i++) {
  44. this.itemsData[i].y = Number(this.itemsData[i].y);
  45. }
  46. }
  47. else {
  48. this.itemsData = [];
  49. }
  50. };
  51. /**
  52. * this is used for plotting barcharts, this way, we only have to calculate it once.
  53. * @param pos
  54. */
  55. GraphGroup.prototype.setZeroPosition = function (pos) {
  56. this.zeroPosition = pos;
  57. };
  58. /**
  59. * set the options of the graph group over the default options.
  60. * @param options
  61. */
  62. GraphGroup.prototype.setOptions = function (options) {
  63. if (options !== undefined) {
  64. var fields = ['sampling', 'style', 'sort', 'yAxisOrientation', 'barChart', 'excludeFromLegend'];
  65. util.selectiveDeepExtend(fields, this.options, options);
  66. // if the group's drawPoints is a function delegate the callback to the onRender property
  67. if (typeof options.drawPoints == 'function') {
  68. options.drawPoints = {
  69. onRender: options.drawPoints
  70. }
  71. }
  72. util.mergeOptions(this.options, options, 'interpolation');
  73. util.mergeOptions(this.options, options, 'drawPoints');
  74. util.mergeOptions(this.options, options, 'shaded');
  75. if (options.interpolation) {
  76. if (typeof options.interpolation == 'object') {
  77. if (options.interpolation.parametrization) {
  78. if (options.interpolation.parametrization == 'uniform') {
  79. this.options.interpolation.alpha = 0;
  80. }
  81. else if (options.interpolation.parametrization == 'chordal') {
  82. this.options.interpolation.alpha = 1.0;
  83. }
  84. else {
  85. this.options.interpolation.parametrization = 'centripetal';
  86. this.options.interpolation.alpha = 0.5;
  87. }
  88. }
  89. }
  90. }
  91. }
  92. if (this.options.style == 'line') {
  93. this.type = new Line(this.id, this.options);
  94. }
  95. else if (this.options.style == 'bar') {
  96. this.type = new Bar(this.id, this.options);
  97. }
  98. else if (this.options.style == 'points') {
  99. this.type = new Points(this.id, this.options);
  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. * draw the icon for the legend.
  116. *
  117. * @param x
  118. * @param y
  119. * @param JSONcontainer
  120. * @param SVGcontainer
  121. * @param iconWidth
  122. * @param iconHeight
  123. */
  124. GraphGroup.prototype.drawIcon = function (x, y, JSONcontainer, SVGcontainer, iconWidth, iconHeight) {
  125. var fillHeight = iconHeight * 0.5;
  126. var path, fillPath;
  127. var outline = DOMutil.getSVGElement("rect", JSONcontainer, SVGcontainer);
  128. outline.setAttributeNS(null, "x", x);
  129. outline.setAttributeNS(null, "y", y - fillHeight);
  130. outline.setAttributeNS(null, "width", iconWidth);
  131. outline.setAttributeNS(null, "height", 2 * fillHeight);
  132. outline.setAttributeNS(null, "class", "vis-outline");
  133. if (this.options.style == 'line') {
  134. path = DOMutil.getSVGElement("path", JSONcontainer, SVGcontainer);
  135. path.setAttributeNS(null, "class", this.className);
  136. if (this.style !== undefined) {
  137. path.setAttributeNS(null, "style", this.style);
  138. }
  139. path.setAttributeNS(null, "d", "M" + x + "," + y + " L" + (x + iconWidth) + "," + y + "");
  140. if (this.options.shaded.enabled == true) {
  141. fillPath = DOMutil.getSVGElement("path", JSONcontainer, SVGcontainer);
  142. if (this.options.shaded.orientation == 'top') {
  143. fillPath.setAttributeNS(null, "d", "M" + x + ", " + (y - fillHeight) +
  144. "L" + x + "," + y + " L" + (x + iconWidth) + "," + y + " L" + (x + iconWidth) + "," + (y - fillHeight));
  145. }
  146. else {
  147. fillPath.setAttributeNS(null, "d", "M" + x + "," + y + " " +
  148. "L" + x + "," + (y + fillHeight) + " " +
  149. "L" + (x + iconWidth) + "," + (y + fillHeight) +
  150. "L" + (x + iconWidth) + "," + y);
  151. }
  152. fillPath.setAttributeNS(null, "class", this.className + " vis-icon-fill");
  153. if (this.options.shaded.style !== undefined && this.options.shaded.style !== "") {
  154. fillPath.setAttributeNS(null, "style", this.options.shaded.style);
  155. }
  156. }
  157. if (this.options.drawPoints.enabled == true) {
  158. var groupTemplate = {
  159. style: this.options.drawPoints.style,
  160. styles: this.options.drawPoints.styles,
  161. size: this.options.drawPoints.size,
  162. className: this.className
  163. };
  164. DOMutil.drawPoint(x + 0.5 * iconWidth, y, groupTemplate, JSONcontainer, SVGcontainer);
  165. }
  166. }
  167. else {
  168. var barWidth = Math.round(0.3 * iconWidth);
  169. var bar1Height = Math.round(0.4 * iconHeight);
  170. var bar2Height = Math.round(0.75 * iconHeight);
  171. var offset = Math.round((iconWidth - (2 * barWidth)) / 3);
  172. DOMutil.drawBar(x + 0.5 * barWidth + offset, y + fillHeight - bar1Height - 1, barWidth, bar1Height, this.className + ' vis-bar', JSONcontainer, SVGcontainer, this.style);
  173. DOMutil.drawBar(x + 1.5 * barWidth + offset + 2, y + fillHeight - bar2Height - 1, barWidth, bar2Height, this.className + ' vis-bar', JSONcontainer, SVGcontainer, this.style);
  174. }
  175. };
  176. /**
  177. * return the legend entree for this group.
  178. *
  179. * @param iconWidth
  180. * @param iconHeight
  181. * @returns {{icon: HTMLElement, label: (group.content|*|string), orientation: (.options.yAxisOrientation|*)}}
  182. */
  183. GraphGroup.prototype.getLegend = function (iconWidth, iconHeight) {
  184. var svg = document.createElementNS('http://www.w3.org/2000/svg', "svg");
  185. this.drawIcon(0, 0.5 * iconHeight, [], svg, iconWidth, iconHeight);
  186. return {icon: svg, label: this.content, orientation: this.options.yAxisOrientation};
  187. };
  188. GraphGroup.prototype.getYRange = function (groupData) {
  189. return this.type.getYRange(groupData);
  190. };
  191. GraphGroup.prototype.getData = function (groupData) {
  192. return this.type.getData(groupData);
  193. };
  194. module.exports = GraphGroup;