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.

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