adaptor.js 6.6 KB

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