base.js 4.3 KB

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