adaptor.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { __rest } from "tslib";
  2. import { get, isNumber } from '@antv/util';
  3. import { animation, annotation, interaction, pattern, scale, state, theme, tooltip } from '../../adaptor/common';
  4. import { geometry as geometryAdaptor } from '../../adaptor/geometries/base';
  5. import { deepAssign, findGeometry, flow, transformLabel } from '../../utils';
  6. import { getTooltipMapping } from '../../utils/tooltip';
  7. function geometry(params) {
  8. var chart = params.chart, options = params.options;
  9. var data = options.data, type = options.type, xField = options.xField, yField = options.yField, colorField = options.colorField, sizeField = options.sizeField, sizeRatio = options.sizeRatio, shape = options.shape, color = options.color, tooltip = options.tooltip, heatmapStyle = options.heatmapStyle, meta = options.meta;
  10. chart.data(data);
  11. var geometryType = 'polygon';
  12. if (type === 'density') {
  13. geometryType = 'heatmap';
  14. }
  15. var _a = getTooltipMapping(tooltip, [xField, yField, colorField]), fields = _a.fields, formatter = _a.formatter;
  16. /**
  17. * The ratio between the actual size and the max available size, must be in range `[0,1]`.
  18. *
  19. * If the `sizeRatio` attribute is undefined or it exceeds the range,
  20. * `checkedSizeRatio` would be set to 1 as default.
  21. */
  22. var checkedSizeRatio = 1;
  23. if (sizeRatio || sizeRatio === 0) {
  24. if (!shape && !sizeField) {
  25. console.warn('sizeRatio is not in effect: Must define shape or sizeField first');
  26. }
  27. else if (sizeRatio < 0 || sizeRatio > 1) {
  28. console.warn('sizeRatio is not in effect: It must be a number in [0,1]');
  29. }
  30. else {
  31. checkedSizeRatio = sizeRatio;
  32. }
  33. }
  34. geometryAdaptor(deepAssign({}, params, {
  35. options: {
  36. type: geometryType,
  37. colorField: colorField,
  38. tooltipFields: fields,
  39. shapeField: sizeField || '',
  40. label: undefined,
  41. mapping: {
  42. tooltip: formatter,
  43. shape: shape &&
  44. (sizeField
  45. ? function (dautm) {
  46. var field = data.map(function (row) { return row[sizeField]; });
  47. var _a = (meta === null || meta === void 0 ? void 0 : meta[sizeField]) || {}, min = _a.min, max = _a.max;
  48. min = isNumber(min) ? min : Math.min.apply(Math, field);
  49. max = isNumber(max) ? max : Math.max.apply(Math, field);
  50. return [shape, (get(dautm, sizeField) - min) / (max - min), checkedSizeRatio];
  51. }
  52. : function () { return [shape, 1, checkedSizeRatio]; }),
  53. color: color || (colorField && chart.getTheme().sequenceColors.join('-')),
  54. style: heatmapStyle,
  55. },
  56. },
  57. }));
  58. return params;
  59. }
  60. /**
  61. * meta 配置
  62. * @param params
  63. */
  64. function meta(params) {
  65. var _a;
  66. var options = params.options;
  67. var xAxis = options.xAxis, yAxis = options.yAxis, xField = options.xField, yField = options.yField;
  68. return flow(scale((_a = {},
  69. _a[xField] = xAxis,
  70. _a[yField] = yAxis,
  71. _a)))(params);
  72. }
  73. /**
  74. * axis 配置
  75. * @param params
  76. */
  77. function axis(params) {
  78. var chart = params.chart, options = params.options;
  79. var xAxis = options.xAxis, yAxis = options.yAxis, xField = options.xField, yField = options.yField;
  80. // 为 false 则是不显示轴
  81. if (xAxis === false) {
  82. chart.axis(xField, false);
  83. }
  84. else {
  85. chart.axis(xField, xAxis);
  86. }
  87. if (yAxis === false) {
  88. chart.axis(yField, false);
  89. }
  90. else {
  91. chart.axis(yField, yAxis);
  92. }
  93. return params;
  94. }
  95. /**
  96. * legend 配置
  97. * @param params
  98. */
  99. function legend(params) {
  100. var chart = params.chart, options = params.options;
  101. var legend = options.legend, colorField = options.colorField, sizeField = options.sizeField, sizeLegend = options.sizeLegend;
  102. /** legend 不为 false, 则展示图例, 优先展示 color 分类图例 */
  103. var showLegend = legend !== false;
  104. if (colorField) {
  105. chart.legend(colorField, showLegend ? legend : false);
  106. }
  107. // 旧版本: 有 sizeField 就有 sizeLegend. 这里默认继承下 legend 配置
  108. if (sizeField) {
  109. chart.legend(sizeField, sizeLegend === undefined ? legend : sizeLegend);
  110. }
  111. /** 默认没有 sizeField,则隐藏连续图例 */
  112. if (!showLegend && !sizeLegend) {
  113. chart.legend(false);
  114. }
  115. return params;
  116. }
  117. /**
  118. * fixme 后续确认下,数据标签的逻辑为啥和通用的不一致
  119. * 数据标签
  120. * @param params
  121. */
  122. function label(params) {
  123. var chart = params.chart, options = params.options;
  124. var label = options.label, colorField = options.colorField, type = options.type;
  125. var geometry = findGeometry(chart, type === 'density' ? 'heatmap' : 'polygon');
  126. if (!label) {
  127. geometry.label(false);
  128. }
  129. else if (colorField) {
  130. var callback = label.callback, cfg = __rest(label, ["callback"]);
  131. geometry.label({
  132. fields: [colorField],
  133. callback: callback,
  134. cfg: transformLabel(cfg),
  135. });
  136. }
  137. return params;
  138. }
  139. /**
  140. * 极坐标
  141. * @param params
  142. */
  143. function coordinate(params) {
  144. var _a, _b;
  145. var chart = params.chart, options = params.options;
  146. var coordinate = options.coordinate, reflect = options.reflect;
  147. var coordinateOption = deepAssign({ actions: [] }, coordinate !== null && coordinate !== void 0 ? coordinate : { type: 'rect' });
  148. if (reflect) {
  149. (_b = (_a = coordinateOption.actions) === null || _a === void 0 ? void 0 : _a.push) === null || _b === void 0 ? void 0 : _b.call(_a, ['reflect', reflect]);
  150. }
  151. chart.coordinate(coordinateOption);
  152. return params;
  153. }
  154. /**
  155. * 热力图适配器
  156. * @param chart
  157. * @param options
  158. */
  159. export function adaptor(params) {
  160. // flow 的方式处理所有的配置到 G2 API
  161. return flow(theme, pattern('heatmapStyle'), meta, coordinate, geometry, axis, legend, tooltip, label, annotation(), interaction, animation, state)(params);
  162. }
  163. //# sourceMappingURL=adaptor.js.map