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.

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