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.

111 lines
5.8 KiB

  1. var util = require('../util');
  2. /**
  3. * @class Groups
  4. * This class can store groups and properties specific for groups.
  5. */
  6. function Groups() {
  7. this.clear();
  8. this.defaultIndex = 0;
  9. this.groupsArray = [];
  10. this.groupIndex = 0;
  11. this.useDefaultGroups = true;
  12. }
  13. /**
  14. * default constants for group colors
  15. */
  16. Groups.DEFAULT = [
  17. {border: "#2B7CE9", background: "#97C2FC", highlight: {border: "#2B7CE9", background: "#D2E5FF"}, hover: {border: "#2B7CE9", background: "#D2E5FF"}}, // 0: blue
  18. {border: "#FFA500", background: "#FFFF00", highlight: {border: "#FFA500", background: "#FFFFA3"}, hover: {border: "#FFA500", background: "#FFFFA3"}}, // 1: yellow
  19. {border: "#FA0A10", background: "#FB7E81", highlight: {border: "#FA0A10", background: "#FFAFB1"}, hover: {border: "#FA0A10", background: "#FFAFB1"}}, // 2: red
  20. {border: "#41A906", background: "#7BE141", highlight: {border: "#41A906", background: "#A1EC76"}, hover: {border: "#41A906", background: "#A1EC76"}}, // 3: green
  21. {border: "#E129F0", background: "#EB7DF4", highlight: {border: "#E129F0", background: "#F0B3F5"}, hover: {border: "#E129F0", background: "#F0B3F5"}}, // 4: magenta
  22. {border: "#7C29F0", background: "#AD85E4", highlight: {border: "#7C29F0", background: "#D3BDF0"}, hover: {border: "#7C29F0", background: "#D3BDF0"}}, // 5: purple
  23. {border: "#C37F00", background: "#FFA807", highlight: {border: "#C37F00", background: "#FFCA66"}, hover: {border: "#C37F00", background: "#FFCA66"}}, // 6: orange
  24. {border: "#4220FB", background: "#6E6EFD", highlight: {border: "#4220FB", background: "#9B9BFD"}, hover: {border: "#4220FB", background: "#9B9BFD"}}, // 7: darkblue
  25. {border: "#FD5A77", background: "#FFC0CB", highlight: {border: "#FD5A77", background: "#FFD1D9"}, hover: {border: "#FD5A77", background: "#FFD1D9"}}, // 8: pink
  26. {border: "#4AD63A", background: "#C2FABC", highlight: {border: "#4AD63A", background: "#E6FFE3"}, hover: {border: "#4AD63A", background: "#E6FFE3"}}, // 9: mint
  27. {border: "#990000", background: "#EE0000", highlight: {border: "#BB0000", background: "#FF3333"}, hover: {border: "#BB0000", background: "#FF3333"}}, // 10:bright red
  28. {border: "#01AA01", background: "#22FF22", highlight: {border: "#33DD33", background: "#AAFFAA"}, hover: {border: "#22FF22", background: "#66FF66"}}, // 11:bright GREEN
  29. {border: "#97C2FC", background: "#2B7CE9", highlight: {border: "#D2E5FF", background: "#2B7CE9"}, hover: {border: "#D2E5FF", background: "#2B7CE9"}}, // 12: blue
  30. {border: "#FFFF00", background: "#FFA500", highlight: {border: "#FFFFA3", background: "#FFA500"}, hover: {border: "#FFFFA3", background: "#FFA500"}}, // 13: yellow
  31. //{border: "#FB7E81", background: "#FA0A10", highlight: {border: "#FFAFB1", background: "#FA0A10"}, hover: {border: "#FFAFB1", background: "#FA0A10"}}, // 14: red
  32. {border: "#7BE141", background: "#41A906", highlight: {border: "#A1EC76", background: "#41A906"}, hover: {border: "#A1EC76", background: "#41A906"}}, // 15: green
  33. {border: "#EB7DF4", background: "#E129F0", highlight: {border: "#F0B3F5", background: "#E129F0"}, hover: {border: "#F0B3F5", background: "#E129F0"}}, // 16: magenta
  34. {border: "#AD85E4", background: "#7C29F0", highlight: {border: "#D3BDF0", background: "#7C29F0"}, hover: {border: "#D3BDF0", background: "#7C29F0"}}, // 17: purple
  35. {border: "#FFA807", background: "#C37F00", highlight: {border: "#FFCA66", background: "#C37F00"}, hover: {border: "#FFCA66", background: "#C37F00"}}, // 18: orange
  36. {border: "#6E6EFD", background: "#4220FB", highlight: {border: "#9B9BFD", background: "#4220FB"}, hover: {border: "#9B9BFD", background: "#4220FB"}}, // 19: darkblue
  37. {border: "#FFC0CB", background: "#FD5A77", highlight: {border: "#FFD1D9", background: "#FD5A77"}, hover: {border: "#FFD1D9", background: "#FD5A77"}}, // 20: pink
  38. {border: "#C2FABC", background: "#4AD63A", highlight: {border: "#E6FFE3", background: "#4AD63A"}, hover: {border: "#E6FFE3", background: "#4AD63A"}}, // 21:mint
  39. {border: "#EE0000", background: "#990000", highlight: {border: "#FF3333", background: "#BB0000"}, hover: {border: "#FF3333", background: "#BB0000"}}, // 22:bright red
  40. {border: "#22FF22", background: "#01AA01", highlight: {border: "#AAFFAA", background: "#33DD33"}, hover: {border: "#66FF66", background: "#22FF22"}}, // 23:bright GREEN
  41. ];
  42. /**
  43. * Clear all groups
  44. */
  45. Groups.prototype.clear = function () {
  46. this.groups = {};
  47. this.groups.length = function()
  48. {
  49. var i = 0;
  50. for ( var p in this ) {
  51. if (this.hasOwnProperty(p)) {
  52. i++;
  53. }
  54. }
  55. return i;
  56. }
  57. };
  58. /**
  59. * get group properties of a groupname. If groupname is not found, a new group
  60. * is added.
  61. * @param {*} groupname Can be a number, string, Date, etc.
  62. * @return {Object} group The created group, containing all group properties
  63. */
  64. Groups.prototype.get = function (groupname) {
  65. var group = this.groups[groupname];
  66. if (group == undefined) {
  67. if (this.useDefaultGroups === false && this.groupsArray.length > 0) {
  68. // create new group
  69. var index = this.groupIndex % this.groupsArray.length;
  70. this.groupIndex++;
  71. group = {};
  72. group.color = this.groups[this.groupsArray[index]];
  73. this.groups[groupname] = group;
  74. }
  75. else {
  76. // create new group
  77. var index = this.defaultIndex % Groups.DEFAULT.length;
  78. this.defaultIndex++;
  79. group = {};
  80. group.color = Groups.DEFAULT[index];
  81. this.groups[groupname] = group;
  82. }
  83. }
  84. return group;
  85. };
  86. /**
  87. * Add a custom group style
  88. * @param {String} groupName
  89. * @param {Object} style An object containing borderColor,
  90. * backgroundColor, etc.
  91. * @return {Object} group The created group object
  92. */
  93. Groups.prototype.add = function (groupName, style) {
  94. this.groups[groupName] = style;
  95. this.groupsArray.push(groupName);
  96. return style;
  97. };
  98. module.exports = Groups;