adaptor.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.adaptor = exports.tooltip = void 0;
  4. var tslib_1 = require("tslib");
  5. var util_1 = require("@antv/util");
  6. var common_1 = require("../../adaptor/common");
  7. var geometries_1 = require("../../adaptor/geometries");
  8. var locale_1 = require("../../core/locale");
  9. var utils_1 = require("../../utils");
  10. var constant_1 = require("./constant");
  11. require("./shape");
  12. var utils_2 = require("./utils");
  13. /**
  14. * 处理默认配置项
  15. * @param params
  16. * @returns
  17. */
  18. function defaultOptions(params) {
  19. var _a = params.options, locale = _a.locale, total = _a.total;
  20. var localeTotalLabel = (0, locale_1.getLocale)(locale).get(['waterfall', 'total']);
  21. if (total && typeof total.label !== 'string' && localeTotalLabel) {
  22. // @ts-ignore
  23. params.options.total.label = localeTotalLabel;
  24. }
  25. return params;
  26. }
  27. /**
  28. * 字段
  29. * @param params
  30. */
  31. function geometry(params) {
  32. var chart = params.chart, options = params.options;
  33. var data = options.data, xField = options.xField, yField = options.yField, total = options.total, leaderLine = options.leaderLine, columnWidthRatio = options.columnWidthRatio, waterfallStyle = options.waterfallStyle, risingFill = options.risingFill, fallingFill = options.fallingFill, color = options.color, shape = options.shape, customInfo = options.customInfo;
  34. // 数据处理
  35. chart.data((0, utils_2.transformData)(data, xField, yField, total));
  36. // 瀑布图自带的 colorMapping
  37. var colorMapping = color ||
  38. function (datum) {
  39. if ((0, util_1.get)(datum, [constant_1.IS_TOTAL])) {
  40. return (0, util_1.get)(total, ['style', 'fill'], '');
  41. }
  42. return (0, util_1.get)(datum, [constant_1.Y_FIELD, 1]) - (0, util_1.get)(datum, [constant_1.Y_FIELD, 0]) > 0 ? risingFill : fallingFill;
  43. };
  44. var p = (0, utils_1.deepAssign)({}, params, {
  45. options: {
  46. xField: xField,
  47. yField: constant_1.Y_FIELD,
  48. seriesField: xField,
  49. rawFields: [yField, constant_1.DIFF_FIELD, constant_1.IS_TOTAL, constant_1.Y_FIELD],
  50. widthRatio: columnWidthRatio,
  51. interval: {
  52. style: waterfallStyle,
  53. // 支持外部自定义形状
  54. shape: shape || 'waterfall',
  55. color: colorMapping,
  56. },
  57. },
  58. });
  59. var ext = (0, geometries_1.interval)(p).ext;
  60. var geometry = ext.geometry;
  61. // 将 waterfall leaderLineCfg 传入到自定义 shape 中
  62. geometry.customInfo(tslib_1.__assign(tslib_1.__assign({}, customInfo), { leaderLine: leaderLine }));
  63. return params;
  64. }
  65. /**
  66. * meta 配置
  67. * @param params
  68. */
  69. function meta(params) {
  70. var _a, _b;
  71. var options = params.options;
  72. var xAxis = options.xAxis, yAxis = options.yAxis, xField = options.xField, yField = options.yField, meta = options.meta;
  73. var Y_FIELD_META = (0, utils_1.deepAssign)({}, { alias: yField }, (0, util_1.get)(meta, yField));
  74. return (0, utils_1.flow)((0, common_1.scale)((_a = {},
  75. _a[xField] = xAxis,
  76. _a[yField] = yAxis,
  77. _a[constant_1.Y_FIELD] = yAxis,
  78. _a), (0, utils_1.deepAssign)({}, meta, (_b = {}, _b[constant_1.Y_FIELD] = Y_FIELD_META, _b[constant_1.DIFF_FIELD] = Y_FIELD_META, _b[constant_1.ABSOLUTE_FIELD] = Y_FIELD_META, _b))))(params);
  79. }
  80. /**
  81. * axis 配置
  82. * @param params
  83. */
  84. function axis(params) {
  85. var chart = params.chart, options = params.options;
  86. var xAxis = options.xAxis, yAxis = options.yAxis, xField = options.xField, yField = options.yField;
  87. // 为 false 则是不显示轴
  88. if (xAxis === false) {
  89. chart.axis(xField, false);
  90. }
  91. else {
  92. chart.axis(xField, xAxis);
  93. }
  94. if (yAxis === false) {
  95. chart.axis(yField, false);
  96. chart.axis(constant_1.Y_FIELD, false);
  97. }
  98. else {
  99. chart.axis(yField, yAxis);
  100. chart.axis(constant_1.Y_FIELD, yAxis);
  101. }
  102. return params;
  103. }
  104. /**
  105. * legend 配置 todo 添加 hover 交互
  106. * @param params
  107. */
  108. function legend(params) {
  109. var chart = params.chart, options = params.options;
  110. var legend = options.legend, total = options.total, risingFill = options.risingFill, fallingFill = options.fallingFill, locale = options.locale;
  111. var i18n = (0, locale_1.getLocale)(locale);
  112. if (legend === false) {
  113. chart.legend(false);
  114. }
  115. else {
  116. var items = [
  117. {
  118. name: i18n.get(['general', 'increase']),
  119. value: 'increase',
  120. marker: { symbol: 'square', style: { r: 5, fill: risingFill } },
  121. },
  122. {
  123. name: i18n.get(['general', 'decrease']),
  124. value: 'decrease',
  125. marker: { symbol: 'square', style: { r: 5, fill: fallingFill } },
  126. },
  127. ];
  128. if (total) {
  129. items.push({
  130. name: total.label || '',
  131. value: 'total',
  132. marker: {
  133. symbol: 'square',
  134. style: (0, utils_1.deepAssign)({}, { r: 5 }, (0, util_1.get)(total, 'style')),
  135. },
  136. });
  137. }
  138. chart.legend((0, utils_1.deepAssign)({}, {
  139. custom: true,
  140. position: 'top',
  141. items: items,
  142. }, legend));
  143. chart.removeInteraction('legend-filter');
  144. }
  145. return params;
  146. }
  147. /**
  148. * 数据标签
  149. * @param params
  150. */
  151. function label(params) {
  152. var chart = params.chart, options = params.options;
  153. var label = options.label, labelMode = options.labelMode, xField = options.xField;
  154. var geometry = (0, utils_1.findGeometry)(chart, 'interval');
  155. if (!label) {
  156. geometry.label(false);
  157. }
  158. else {
  159. var callback = label.callback, cfg = tslib_1.__rest(label, ["callback"]);
  160. geometry.label({
  161. fields: labelMode === 'absolute' ? [constant_1.ABSOLUTE_FIELD, xField] : [constant_1.DIFF_FIELD, xField],
  162. callback: callback,
  163. cfg: (0, utils_1.transformLabel)(cfg),
  164. });
  165. }
  166. return params;
  167. }
  168. /**
  169. * tooltip 配置
  170. * @param params
  171. */
  172. function tooltip(params) {
  173. var chart = params.chart, options = params.options;
  174. var tooltip = options.tooltip, xField = options.xField, yField = options.yField;
  175. if (tooltip !== false) {
  176. chart.tooltip(tslib_1.__assign({ showCrosshairs: false, showMarkers: false, shared: true,
  177. // tooltip 默认展示 y 字段值
  178. fields: [yField] }, tooltip));
  179. // 瀑布图默认以 yField 作为 tooltip 内容
  180. var geometry_1 = chart.geometries[0];
  181. (tooltip === null || tooltip === void 0 ? void 0 : tooltip.formatter) ? geometry_1.tooltip("".concat(xField, "*").concat(yField), tooltip.formatter) : geometry_1.tooltip(yField);
  182. }
  183. else {
  184. chart.tooltip(false);
  185. }
  186. return params;
  187. }
  188. exports.tooltip = tooltip;
  189. /**
  190. * 瀑布图适配器
  191. * @param params
  192. */
  193. function adaptor(params) {
  194. return (0, utils_1.flow)(defaultOptions, common_1.theme, geometry, meta, axis, legend, tooltip, label, common_1.state, common_1.interaction, common_1.animation, (0, common_1.annotation)())(params);
  195. }
  196. exports.adaptor = adaptor;
  197. //# sourceMappingURL=adaptor.js.map