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
5.6 KiB

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