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.

116 lines
4.2 KiB

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