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.

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