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.

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