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.

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