UNPKG

5.71 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var util_1 = require("@antv/util");
5var constant_1 = require("../constant");
6var facet_1 = require("../util/facet");
7var facet_2 = require("./facet");
8/**
9 * @ignore
10 * 矩阵分面
11 */
12var Rect = /** @class */ (function (_super) {
13 tslib_1.__extends(Rect, _super);
14 function Rect() {
15 return _super !== null && _super.apply(this, arguments) || this;
16 }
17 Rect.prototype.afterEachView = function (view, facet) {
18 this.processAxis(view, facet);
19 };
20 Rect.prototype.beforeEachView = function (view, facet) {
21 // do nothing
22 };
23 Rect.prototype.getDefaultCfg = function () {
24 return (0, util_1.deepMix)({}, _super.prototype.getDefaultCfg.call(this), {
25 type: 'rect',
26 columnTitle: tslib_1.__assign({}, _super.prototype.getDefaultTitleCfg.call(this)),
27 rowTitle: tslib_1.__assign({}, _super.prototype.getDefaultTitleCfg.call(this)),
28 });
29 };
30 Rect.prototype.render = function () {
31 _super.prototype.render.call(this);
32 if (this.cfg.showTitle) {
33 this.renderTitle();
34 }
35 };
36 /**
37 * 生成矩阵分面的分面数据
38 * @param data
39 */
40 Rect.prototype.generateFacets = function (data) {
41 var _this = this;
42 var _a = tslib_1.__read(this.cfg.fields, 2), columnField = _a[0], rowField = _a[1];
43 var rst = [];
44 var columnValuesLength = 1;
45 var rowValuesLength = 1;
46 var columnValues = [''];
47 var rowValues = [''];
48 if (columnField) {
49 columnValues = this.getFieldValues(data, columnField);
50 columnValuesLength = columnValues.length;
51 }
52 if (rowField) {
53 rowValues = this.getFieldValues(data, rowField);
54 rowValuesLength = rowValues.length;
55 }
56 // 获取每个维度对应的数据配置片段
57 columnValues.forEach(function (xVal, xIndex) {
58 rowValues.forEach(function (yVal, yIndex) {
59 var conditions = [
60 { field: columnField, value: xVal, values: columnValues },
61 { field: rowField, value: yVal, values: rowValues },
62 ];
63 var facetData = (0, util_1.filter)(data, _this.getFacetDataFilter(conditions));
64 var facet = {
65 type: _this.cfg.type,
66 data: facetData,
67 region: _this.getRegion(rowValuesLength, columnValuesLength, xIndex, yIndex),
68 columnValue: xVal,
69 rowValue: yVal,
70 columnField: columnField,
71 rowField: rowField,
72 columnIndex: xIndex,
73 rowIndex: yIndex,
74 columnValuesLength: columnValuesLength,
75 rowValuesLength: rowValuesLength,
76 };
77 rst.push(facet);
78 });
79 });
80 return rst;
81 };
82 Rect.prototype.renderTitle = function () {
83 var _this = this;
84 (0, util_1.each)(this.facets, function (facet, facetIndex) {
85 var columnIndex = facet.columnIndex, rowIndex = facet.rowIndex, columnValuesLength = facet.columnValuesLength, columnValue = facet.columnValue, rowValue = facet.rowValue, view = facet.view;
86 // top
87 if (rowIndex === 0) {
88 var formatter = (0, util_1.get)(_this.cfg.columnTitle, 'formatter');
89 var config = (0, util_1.deepMix)({
90 position: ['50%', '0%'],
91 content: formatter ? formatter(columnValue) : columnValue,
92 }, (0, facet_1.getFactTitleConfig)(constant_1.DIRECTION.TOP), _this.cfg.columnTitle);
93 view.annotation().text(config);
94 }
95 // right
96 if (columnIndex === columnValuesLength - 1) {
97 var formatter = (0, util_1.get)(_this.cfg.rowTitle, 'formatter');
98 var config = (0, util_1.deepMix)({
99 position: ['100%', '50%'],
100 content: formatter ? formatter(rowValue) : rowValue,
101 }, (0, facet_1.getFactTitleConfig)(constant_1.DIRECTION.RIGHT), _this.cfg.rowTitle);
102 view.annotation().text(config);
103 }
104 });
105 };
106 /**
107 * 设置 x 坐标轴的文本、title 是否显示
108 * @param x
109 * @param axes
110 * @param option
111 * @param facet
112 */
113 Rect.prototype.getXAxisOption = function (x, axes, option, facet) {
114 // 非最后一行
115 if (facet.rowIndex !== facet.rowValuesLength - 1) {
116 return tslib_1.__assign(tslib_1.__assign({}, option), { title: null, label: null });
117 }
118 else if (facet.columnIndex !== Math.floor((facet.columnValuesLength - 1) / 2)) {
119 // 不是中间列
120 return tslib_1.__assign(tslib_1.__assign({}, option), { title: null });
121 }
122 return option;
123 };
124 /**
125 * 设置 y 坐标轴的文本、title 是否显示
126 * @param y
127 * @param axes
128 * @param option
129 * @param facet
130 */
131 Rect.prototype.getYAxisOption = function (y, axes, option, facet) {
132 if (facet.columnIndex !== 0) {
133 return tslib_1.__assign(tslib_1.__assign({}, option), { title: null, label: null });
134 }
135 else if (facet.rowIndex !== Math.floor((facet.rowValuesLength - 1) / 2)) {
136 return tslib_1.__assign(tslib_1.__assign({}, option), { title: null });
137 }
138 return option;
139 };
140 return Rect;
141}(facet_2.Facet));
142exports.default = Rect;
143//# sourceMappingURL=rect.js.map
\No newline at end of file