base.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { __assign, __extends } from "tslib";
  2. import { each, isString, mix, isFunction } from '@antv/util';
  3. import GroupComponent from '../abstract/group-component';
  4. import Theme from '../util/theme';
  5. var GridBase = /** @class */ (function (_super) {
  6. __extends(GridBase, _super);
  7. function GridBase() {
  8. return _super !== null && _super.apply(this, arguments) || this;
  9. }
  10. GridBase.prototype.getDefaultCfg = function () {
  11. var cfg = _super.prototype.getDefaultCfg.call(this);
  12. return __assign(__assign({}, cfg), { name: 'grid', line: {}, alternateColor: null, capture: false, items: [], closed: false, defaultCfg: {
  13. line: {
  14. type: 'line',
  15. style: {
  16. lineWidth: 1,
  17. stroke: Theme.lineColor,
  18. },
  19. },
  20. } });
  21. };
  22. /**
  23. * 获取栅格线的类型
  24. * @return {string} 栅格线类型
  25. */
  26. GridBase.prototype.getLineType = function () {
  27. var line = this.get('line') || this.get('defaultCfg').line;
  28. return line.type;
  29. };
  30. GridBase.prototype.renderInner = function (group) {
  31. this.drawGrid(group);
  32. };
  33. GridBase.prototype.getAlternatePath = function (prePoints, points) {
  34. var regionPath = this.getGridPath(prePoints);
  35. var reversePoints = points.slice(0).reverse();
  36. var nextPath = this.getGridPath(reversePoints, true);
  37. var closed = this.get('closed');
  38. if (closed) {
  39. regionPath = regionPath.concat(nextPath);
  40. }
  41. else {
  42. nextPath[0][0] = 'L'; // 更新第一个节点
  43. regionPath = regionPath.concat(nextPath);
  44. regionPath.push(['Z']);
  45. }
  46. return regionPath;
  47. };
  48. // 获取路径的配置项
  49. GridBase.prototype.getPathStyle = function () {
  50. return this.get('line').style;
  51. };
  52. // 绘制栅格
  53. GridBase.prototype.drawGrid = function (group) {
  54. var _this = this;
  55. var line = this.get('line');
  56. var items = this.get('items');
  57. var alternateColor = this.get('alternateColor');
  58. var preItem = null;
  59. each(items, function (item, index) {
  60. var id = item.id || index;
  61. // 绘制栅格线
  62. if (line) {
  63. var style = _this.getPathStyle();
  64. style = isFunction(style) ? style(item, index, items) : style;
  65. var lineId = _this.getElementId("line-" + id);
  66. var gridPath = _this.getGridPath(item.points);
  67. _this.addShape(group, {
  68. type: 'path',
  69. name: 'grid-line',
  70. id: lineId,
  71. attrs: mix({
  72. path: gridPath,
  73. }, style),
  74. });
  75. }
  76. // 如果存在 alternateColor 则绘制矩形
  77. // 从第二个栅格线开始绘制
  78. if (alternateColor && index > 0) {
  79. var regionId = _this.getElementId("region-" + id);
  80. var isEven = index % 2 === 0;
  81. if (isString(alternateColor)) {
  82. // 如果颜色是单值,则是仅绘制偶数时的区域
  83. if (isEven) {
  84. _this.drawAlternateRegion(regionId, group, preItem.points, item.points, alternateColor);
  85. }
  86. }
  87. else {
  88. var color = isEven ? alternateColor[1] : alternateColor[0];
  89. _this.drawAlternateRegion(regionId, group, preItem.points, item.points, color);
  90. }
  91. }
  92. preItem = item;
  93. });
  94. };
  95. // 绘制栅格线间的间隔
  96. GridBase.prototype.drawAlternateRegion = function (id, group, prePoints, points, color) {
  97. var regionPath = this.getAlternatePath(prePoints, points);
  98. this.addShape(group, {
  99. type: 'path',
  100. id: id,
  101. name: 'grid-region',
  102. attrs: {
  103. path: regionPath,
  104. fill: color,
  105. },
  106. });
  107. };
  108. return GridBase;
  109. }(GroupComponent));
  110. export default GridBase;
  111. //# sourceMappingURL=base.js.map