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.

80 lines
2.5 KiB

  1. /**
  2. * @class Groups
  3. * This class can store groups and properties specific for groups.
  4. */
  5. function Groups() {
  6. this.clear();
  7. this.defaultIndex = 0;
  8. }
  9. /**
  10. * default constants for group colors
  11. */
  12. Groups.DEFAULT = [
  13. {border: "#2B7CE9", background: "#97C2FC", highlight: {border: "#2B7CE9", background: "#D2E5FF"}}, // blue
  14. {border: "#FFA500", background: "#FFFF00", highlight: {border: "#FFA500", background: "#FFFFA3"}}, // yellow
  15. {border: "#FA0A10", background: "#FB7E81", highlight: {border: "#FA0A10", background: "#FFAFB1"}}, // red
  16. {border: "#41A906", background: "#7BE141", highlight: {border: "#41A906", background: "#A1EC76"}}, // green
  17. {border: "#E129F0", background: "#EB7DF4", highlight: {border: "#E129F0", background: "#F0B3F5"}}, // magenta
  18. {border: "#7C29F0", background: "#AD85E4", highlight: {border: "#7C29F0", background: "#D3BDF0"}}, // purple
  19. {border: "#C37F00", background: "#FFA807", highlight: {border: "#C37F00", background: "#FFCA66"}}, // orange
  20. {border: "#4220FB", background: "#6E6EFD", highlight: {border: "#4220FB", background: "#9B9BFD"}}, // darkblue
  21. {border: "#FD5A77", background: "#FFC0CB", highlight: {border: "#FD5A77", background: "#FFD1D9"}}, // pink
  22. {border: "#4AD63A", background: "#C2FABC", highlight: {border: "#4AD63A", background: "#E6FFE3"}} // mint
  23. ];
  24. /**
  25. * Clear all groups
  26. */
  27. Groups.prototype.clear = function () {
  28. this.groups = {};
  29. this.groups.length = function()
  30. {
  31. var i = 0;
  32. for ( var p in this ) {
  33. if (this.hasOwnProperty(p)) {
  34. i++;
  35. }
  36. }
  37. return i;
  38. }
  39. };
  40. /**
  41. * get group properties of a groupname. If groupname is not found, a new group
  42. * is added.
  43. * @param {*} groupname Can be a number, string, Date, etc.
  44. * @return {Object} group The created group, containing all group properties
  45. */
  46. Groups.prototype.get = function (groupname) {
  47. var group = this.groups[groupname];
  48. if (group == undefined) {
  49. // create new group
  50. var index = this.defaultIndex % Groups.DEFAULT.length;
  51. this.defaultIndex++;
  52. group = {};
  53. group.color = Groups.DEFAULT[index];
  54. this.groups[groupname] = group;
  55. }
  56. return group;
  57. };
  58. /**
  59. * Add a custom group style
  60. * @param {String} groupname
  61. * @param {Object} style An object containing borderColor,
  62. * backgroundColor, etc.
  63. * @return {Object} group The created group object
  64. */
  65. Groups.prototype.add = function (groupname, style) {
  66. this.groups[groupname] = style;
  67. if (style.color) {
  68. style.color = util.parseColor(style.color);
  69. }
  70. return style;
  71. };