ribbon.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { __assign, __extends, __read } from "tslib";
  2. import { parseColor } from '@antv/g';
  3. import { isFunction } from '@antv/util';
  4. import { GUI } from '../../../core';
  5. import { classNames, select, subStyleProps } from '../../../util';
  6. import { ifHorizontal } from '../utils';
  7. import { getBlockColor } from './utils';
  8. var CLASS_NAMES = classNames({
  9. trackGroup: 'background-group',
  10. track: 'background',
  11. selectionGroup: 'ribbon-group',
  12. selection: 'ribbon',
  13. clipPath: 'clip-path',
  14. }, 'ribbon');
  15. function getShape(attr) {
  16. var orientation = attr.orientation, size = attr.size, length = attr.length;
  17. return ifHorizontal(orientation, [length, size], [size, length]);
  18. }
  19. function getTrackPath(attr) {
  20. var type = attr.type;
  21. var _a = __read(getShape(attr), 2), cw = _a[0], ch = _a[1];
  22. if (type === 'size') {
  23. return [['M', 0, ch], ['L', 0 + cw, 0], ['L', 0 + cw, ch], ['Z']];
  24. }
  25. return [['M', 0, ch], ['L', 0, 0], ['L', 0 + cw, 0], ['L', 0 + cw, ch], ['Z']];
  26. }
  27. function getSelectionPath(attr) {
  28. return getTrackPath(attr);
  29. }
  30. function getColor(attr) {
  31. var orientation = attr.orientation, color = attr.color, block = attr.block, partition = attr.partition;
  32. var colors;
  33. if (isFunction(color)) {
  34. var len = 20;
  35. colors = new Array(len).fill(0).map(function (_, index, arr) { return color(index / (arr.length - 1)); });
  36. }
  37. else
  38. colors = color;
  39. var count = colors.length;
  40. var genericColor = colors.map(function (c) { return parseColor(c).toString(); });
  41. if (!count)
  42. return '';
  43. if (count === 1)
  44. return genericColor[0];
  45. if (block)
  46. return getBlockColor(partition, genericColor, orientation);
  47. return genericColor.reduce(function (r, c, idx) { return (r += " ".concat(idx / (count - 1), ":").concat(c)); }, "l(".concat(ifHorizontal(orientation, '0', '270'), ")"));
  48. }
  49. function getClipPath(attr) {
  50. var orientation = attr.orientation, range = attr.range;
  51. if (!range)
  52. return [];
  53. var _a = __read(getShape(attr), 2), width = _a[0], height = _a[1];
  54. var _b = __read(range, 2), st = _b[0], et = _b[1];
  55. var x = ifHorizontal(orientation, st * width, 0);
  56. var y = ifHorizontal(orientation, 0, st * height);
  57. var w = ifHorizontal(orientation, et * width, width);
  58. var h = ifHorizontal(orientation, height, et * height);
  59. return [['M', x, y], ['L', x, h], ['L', w, h], ['L', w, y], ['Z']];
  60. }
  61. function renderTrack(container, attr) {
  62. var style = subStyleProps(attr, 'track');
  63. container.maybeAppendByClassName(CLASS_NAMES.track, 'path').styles(__assign({ path: getTrackPath(attr) }, style));
  64. }
  65. function renderSelection(container, attr) {
  66. var style = subStyleProps(attr, 'selection');
  67. var fill = getColor(attr);
  68. var ribbon = container
  69. .maybeAppendByClassName(CLASS_NAMES.selection, 'path')
  70. .styles(__assign({ path: getSelectionPath(attr), fill: fill }, style));
  71. var clipPath = ribbon
  72. .maybeAppendByClassName(CLASS_NAMES.clipPath, 'path')
  73. .styles({ path: getClipPath(attr) })
  74. .node();
  75. ribbon.style('clip-path', clipPath);
  76. }
  77. var Ribbon = /** @class */ (function (_super) {
  78. __extends(Ribbon, _super);
  79. function Ribbon(options) {
  80. return _super.call(this, options, {
  81. type: 'color',
  82. orientation: 'horizontal',
  83. size: 30,
  84. range: [0, 1],
  85. length: 200,
  86. block: false,
  87. partition: [],
  88. color: ['#fff', '#000'],
  89. trackFill: '#e5e5e5',
  90. }) || this;
  91. }
  92. Ribbon.prototype.render = function (attribute, container) {
  93. var trackGroup = select(container).maybeAppendByClassName(CLASS_NAMES.trackGroup, 'g');
  94. renderTrack(trackGroup, attribute);
  95. /**
  96. * - ribbon group
  97. * |- ribbon
  98. * - clip path
  99. */
  100. var ribbonGroup = select(container).maybeAppendByClassName(CLASS_NAMES.selectionGroup, 'g');
  101. renderSelection(ribbonGroup, attribute);
  102. };
  103. return Ribbon;
  104. }(GUI));
  105. export { Ribbon };
  106. //# sourceMappingURL=ribbon.js.map