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.

136 lines
4.9 KiB

  1. var util = require('../../util');
  2. var DOMutil = require('../../DOMutil');
  3. /**
  4. * @constructor Group
  5. * @param {Number | String} groupId
  6. * @param {Object} data
  7. * @param {ItemSet} itemSet
  8. */
  9. function GraphGroup (group, groupId, options, groupsUsingDefaultStyles) {
  10. this.id = groupId;
  11. var fields = ['sampling','style','sort','yAxisOrientation','barChart','drawPoints','shaded','catmullRom']
  12. this.options = util.selectiveBridgeObject(fields,options);
  13. this.usingDefaultStyle = group.className === undefined;
  14. this.groupsUsingDefaultStyles = groupsUsingDefaultStyles;
  15. this.zeroPosition = 0;
  16. this.update(group);
  17. if (this.usingDefaultStyle == true) {
  18. this.groupsUsingDefaultStyles[0] += 1;
  19. }
  20. this.itemsData = [];
  21. this.visible = group.visible === undefined ? true : group.visible;
  22. }
  23. GraphGroup.prototype.setItems = function(items) {
  24. if (items != null) {
  25. this.itemsData = items;
  26. if (this.options.sort == true) {
  27. this.itemsData.sort(function (a,b) {return a.x - b.x;})
  28. }
  29. }
  30. else {
  31. this.itemsData = [];
  32. }
  33. };
  34. GraphGroup.prototype.setZeroPosition = function(pos) {
  35. this.zeroPosition = pos;
  36. };
  37. GraphGroup.prototype.setOptions = function(options) {
  38. if (options !== undefined) {
  39. var fields = ['sampling','style','sort','yAxisOrientation','barChart'];
  40. util.selectiveDeepExtend(fields, this.options, options);
  41. util.mergeOptions(this.options, options,'catmullRom');
  42. util.mergeOptions(this.options, options,'drawPoints');
  43. util.mergeOptions(this.options, options,'shaded');
  44. if (options.catmullRom) {
  45. if (typeof options.catmullRom == 'object') {
  46. if (options.catmullRom.parametrization) {
  47. if (options.catmullRom.parametrization == 'uniform') {
  48. this.options.catmullRom.alpha = 0;
  49. }
  50. else if (options.catmullRom.parametrization == 'chordal') {
  51. this.options.catmullRom.alpha = 1.0;
  52. }
  53. else {
  54. this.options.catmullRom.parametrization = 'centripetal';
  55. this.options.catmullRom.alpha = 0.5;
  56. }
  57. }
  58. }
  59. }
  60. }
  61. };
  62. GraphGroup.prototype.update = function(group) {
  63. this.group = group;
  64. this.content = group.content || 'graph';
  65. this.className = group.className || this.className || "graphGroup" + this.groupsUsingDefaultStyles[0] % 10;
  66. this.visible = group.visible === undefined ? true : group.visible;
  67. this.style = group.style;
  68. this.setOptions(group.options);
  69. };
  70. GraphGroup.prototype.drawIcon = function(x, y, JSONcontainer, SVGcontainer, iconWidth, iconHeight) {
  71. var fillHeight = iconHeight * 0.5;
  72. var path, fillPath;
  73. var outline = DOMutil.getSVGElement("rect", JSONcontainer, SVGcontainer);
  74. outline.setAttributeNS(null, "x", x);
  75. outline.setAttributeNS(null, "y", y - fillHeight);
  76. outline.setAttributeNS(null, "width", iconWidth);
  77. outline.setAttributeNS(null, "height", 2*fillHeight);
  78. outline.setAttributeNS(null, "class", "outline");
  79. if (this.options.style == 'line') {
  80. path = DOMutil.getSVGElement("path", JSONcontainer, SVGcontainer);
  81. path.setAttributeNS(null, "class", this.className);
  82. path.setAttributeNS(null, "d", "M" + x + ","+y+" L" + (x + iconWidth) + ","+y+"");
  83. if (this.options.shaded.enabled == true) {
  84. fillPath = DOMutil.getSVGElement("path", JSONcontainer, SVGcontainer);
  85. if (this.options.shaded.orientation == 'top') {
  86. fillPath.setAttributeNS(null, "d", "M"+x+", " + (y - fillHeight) +
  87. "L"+x+","+y+" L"+ (x + iconWidth) + ","+y+" L"+ (x + iconWidth) + "," + (y - fillHeight));
  88. }
  89. else {
  90. fillPath.setAttributeNS(null, "d", "M"+x+","+y+" " +
  91. "L"+x+"," + (y + fillHeight) + " " +
  92. "L"+ (x + iconWidth) + "," + (y + fillHeight) +
  93. "L"+ (x + iconWidth) + ","+y);
  94. }
  95. fillPath.setAttributeNS(null, "class", this.className + " iconFill");
  96. }
  97. if (this.options.drawPoints.enabled == true) {
  98. DOMutil.drawPoint(x + 0.5 * iconWidth,y, this, JSONcontainer, SVGcontainer);
  99. }
  100. }
  101. else {
  102. var barWidth = Math.round(0.3 * iconWidth);
  103. var bar1Height = Math.round(0.4 * iconHeight);
  104. var bar2Height = Math.round(0.75 * iconHeight);
  105. var offset = Math.round((iconWidth - (2 * barWidth))/3);
  106. DOMutil.drawBar(x + 0.5*barWidth + offset , y + fillHeight - bar1Height - 1, barWidth, bar1Height, this.className + ' bar', JSONcontainer, SVGcontainer);
  107. DOMutil.drawBar(x + 1.5*barWidth + offset + 2, y + fillHeight - bar2Height - 1, barWidth, bar2Height, this.className + ' bar', JSONcontainer, SVGcontainer);
  108. }
  109. };
  110. /**
  111. *
  112. * @param iconWidth
  113. * @param iconHeight
  114. * @returns {{icon: HTMLElement, label: (group.content|*|string), orientation: (.options.yAxisOrientation|*)}}
  115. */
  116. GraphGroup.prototype.getLegend = function(iconWidth, iconHeight) {
  117. var svg = document.createElementNS('http://www.w3.org/2000/svg',"svg");
  118. this.drawIcon(0,0.5*iconHeight,[],svg,iconWidth,iconHeight);
  119. return {icon: svg, label: this.content, orientation:this.options.yAxisOrientation};
  120. }
  121. module.exports = GraphGroup;