adaptor.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.adaptor = exports.animation = exports.theme = exports.limitInPlot = exports.interaction = 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 utils_1 = require("../../utils");
  9. var constant_1 = require("./constant");
  10. var utils_2 = require("./utils");
  11. /**
  12. * geometry 处理
  13. * @param params
  14. */
  15. function geometry(params) {
  16. var chart = params.chart, options = params.options;
  17. var data = options.data, xField = options.xField, yField = options.yField, color = options.color, barStyle = options.barStyle, widthRatio = options.widthRatio, legend = options.legend, layout = options.layout;
  18. // 处理数据
  19. var groupData = (0, utils_2.transformData)(xField, yField, constant_1.SERIES_FIELD_KEY, data, (0, utils_2.isHorizontal)(layout));
  20. // 在创建子 view 执行后不行,需要在前面处理 legend
  21. if (legend) {
  22. chart.legend(constant_1.SERIES_FIELD_KEY, legend);
  23. }
  24. else if (legend === false) {
  25. chart.legend(false);
  26. }
  27. // 创建 view
  28. var firstView;
  29. var secondView;
  30. var firstViewData = groupData[0], secondViewData = groupData[1];
  31. // 横向
  32. if ((0, utils_2.isHorizontal)(layout)) {
  33. firstView = chart.createView({
  34. region: {
  35. start: { x: 0, y: 0 },
  36. end: { x: 0.5, y: 1 },
  37. },
  38. id: constant_1.FIRST_AXES_VIEW,
  39. });
  40. firstView.coordinate().transpose().reflect('x');
  41. secondView = chart.createView({
  42. region: {
  43. start: { x: 0.5, y: 0 },
  44. end: { x: 1, y: 1 },
  45. },
  46. id: constant_1.SECOND_AXES_VIEW,
  47. });
  48. secondView.coordinate().transpose();
  49. // @说明: 测试发现,横向因为轴的反转,需要数据也反转,不然会图形渲染是反的(翻转操作进入到 transform 中处理)
  50. firstView.data(firstViewData);
  51. secondView.data(secondViewData);
  52. }
  53. else {
  54. // 纵向
  55. firstView = chart.createView({
  56. region: {
  57. start: { x: 0, y: 0 },
  58. end: { x: 1, y: 0.5 },
  59. },
  60. id: constant_1.FIRST_AXES_VIEW,
  61. });
  62. secondView = chart.createView({
  63. region: {
  64. start: { x: 0, y: 0.5 },
  65. end: { x: 1, y: 1 },
  66. },
  67. id: constant_1.SECOND_AXES_VIEW,
  68. });
  69. secondView.coordinate().reflect('y');
  70. firstView.data(firstViewData);
  71. secondView.data(secondViewData);
  72. }
  73. var left = (0, utils_1.deepAssign)({}, params, {
  74. chart: firstView,
  75. options: {
  76. widthRatio: widthRatio,
  77. xField: xField,
  78. yField: yField[0],
  79. seriesField: constant_1.SERIES_FIELD_KEY,
  80. interval: {
  81. color: color,
  82. style: barStyle,
  83. },
  84. },
  85. });
  86. (0, geometries_1.interval)(left);
  87. var right = (0, utils_1.deepAssign)({}, params, {
  88. chart: secondView,
  89. options: {
  90. xField: xField,
  91. yField: yField[1],
  92. seriesField: constant_1.SERIES_FIELD_KEY,
  93. widthRatio: widthRatio,
  94. interval: {
  95. color: color,
  96. style: barStyle,
  97. },
  98. },
  99. });
  100. (0, geometries_1.interval)(right);
  101. return params;
  102. }
  103. /**
  104. * meta 配置
  105. * - 对称条形图对数据进行了处理,通过 SERIES_FIELD_KEY 来对两条 yField 数据进行分类
  106. * @param params
  107. */
  108. function meta(params) {
  109. var _a, _b, _c;
  110. var options = params.options, chart = params.chart;
  111. var xAxis = options.xAxis, yAxis = options.yAxis, xField = options.xField, yField = options.yField;
  112. var firstView = (0, utils_1.findViewById)(chart, constant_1.FIRST_AXES_VIEW);
  113. var secondView = (0, utils_1.findViewById)(chart, constant_1.SECOND_AXES_VIEW);
  114. var aliasMap = {};
  115. (0, util_1.keys)((options === null || options === void 0 ? void 0 : options.meta) || {}).map(function (metaKey) {
  116. if ((0, util_1.get)(options === null || options === void 0 ? void 0 : options.meta, [metaKey, 'alias'])) {
  117. aliasMap[metaKey] = options.meta[metaKey].alias;
  118. }
  119. });
  120. chart.scale((_a = {},
  121. _a[constant_1.SERIES_FIELD_KEY] = {
  122. sync: true,
  123. formatter: function (v) {
  124. return (0, util_1.get)(aliasMap, v, v);
  125. },
  126. },
  127. _a));
  128. (0, common_1.scale)((_b = {},
  129. _b[xField] = xAxis,
  130. _b[yField[0]] = yAxis[yField[0]],
  131. _b))((0, utils_1.deepAssign)({}, params, { chart: firstView }));
  132. (0, common_1.scale)((_c = {},
  133. _c[xField] = xAxis,
  134. _c[yField[1]] = yAxis[yField[1]],
  135. _c))((0, utils_1.deepAssign)({}, params, { chart: secondView }));
  136. return params;
  137. }
  138. /**
  139. * axis 配置
  140. * @param params
  141. */
  142. function axis(params) {
  143. var chart = params.chart, options = params.options;
  144. var xAxis = options.xAxis, yAxis = options.yAxis, xField = options.xField, yField = options.yField, layout = options.layout;
  145. var firstView = (0, utils_1.findViewById)(chart, constant_1.FIRST_AXES_VIEW);
  146. var secondView = (0, utils_1.findViewById)(chart, constant_1.SECOND_AXES_VIEW);
  147. // 第二个 view axis 始终隐藏; 注意 bottom 的时候,只隐藏 label,其他共用配置
  148. // @ts-ignore
  149. if ((xAxis === null || xAxis === void 0 ? void 0 : xAxis.position) === 'bottom') {
  150. // fixme 直接设置 label: null 会导致 tickLine 无法显示
  151. secondView.axis(xField, tslib_1.__assign(tslib_1.__assign({}, xAxis), { label: { formatter: function () { return ''; } } }));
  152. }
  153. else {
  154. secondView.axis(xField, false);
  155. }
  156. // 为 false 则是不显示 firstView 轴
  157. if (xAxis === false) {
  158. firstView.axis(xField, false);
  159. }
  160. else {
  161. firstView.axis(xField, tslib_1.__assign({
  162. // 不同布局 firstView 的坐标轴显示位置
  163. position: (0, utils_2.isHorizontal)(layout) ? 'top' : 'bottom' }, xAxis));
  164. }
  165. if (yAxis === false) {
  166. firstView.axis(yField[0], false);
  167. secondView.axis(yField[1], false);
  168. }
  169. else {
  170. firstView.axis(yField[0], yAxis[yField[0]]);
  171. secondView.axis(yField[1], yAxis[yField[1]]);
  172. }
  173. /**
  174. * 这个注入,主要是在syncViewPadding时候拿到相对应的配置:布局和轴的位置
  175. * TODO 之后希望 g2 View 对象可以开放 setter 可以设置一些需要的东西
  176. */
  177. //@ts-ignore
  178. chart.__axisPosition = {
  179. position: firstView.getOptions().axes[xField].position,
  180. layout: layout,
  181. };
  182. return params;
  183. }
  184. /**
  185. * interaction 配置
  186. * @param params
  187. */
  188. function interaction(params) {
  189. var chart = params.chart;
  190. (0, common_1.interaction)((0, utils_1.deepAssign)({}, params, { chart: (0, utils_1.findViewById)(chart, constant_1.FIRST_AXES_VIEW) }));
  191. (0, common_1.interaction)((0, utils_1.deepAssign)({}, params, { chart: (0, utils_1.findViewById)(chart, constant_1.SECOND_AXES_VIEW) }));
  192. return params;
  193. }
  194. exports.interaction = interaction;
  195. /**
  196. * limitInPlot
  197. * @param params
  198. */
  199. function limitInPlot(params) {
  200. var chart = params.chart, options = params.options;
  201. var yField = options.yField, yAxis = options.yAxis;
  202. (0, common_1.limitInPlot)((0, utils_1.deepAssign)({}, params, {
  203. chart: (0, utils_1.findViewById)(chart, constant_1.FIRST_AXES_VIEW),
  204. options: {
  205. yAxis: yAxis[yField[0]],
  206. },
  207. }));
  208. (0, common_1.limitInPlot)((0, utils_1.deepAssign)({}, params, {
  209. chart: (0, utils_1.findViewById)(chart, constant_1.SECOND_AXES_VIEW),
  210. options: {
  211. yAxis: yAxis[yField[1]],
  212. },
  213. }));
  214. return params;
  215. }
  216. exports.limitInPlot = limitInPlot;
  217. /**
  218. * theme
  219. * @param params
  220. */
  221. function theme(params) {
  222. var chart = params.chart;
  223. (0, common_1.theme)((0, utils_1.deepAssign)({}, params, { chart: (0, utils_1.findViewById)(chart, constant_1.FIRST_AXES_VIEW) }));
  224. (0, common_1.theme)((0, utils_1.deepAssign)({}, params, { chart: (0, utils_1.findViewById)(chart, constant_1.SECOND_AXES_VIEW) }));
  225. (0, common_1.theme)(params);
  226. return params;
  227. }
  228. exports.theme = theme;
  229. /**
  230. * animation
  231. * @param params
  232. */
  233. function animation(params) {
  234. var chart = params.chart;
  235. (0, common_1.animation)((0, utils_1.deepAssign)({}, params, { chart: (0, utils_1.findViewById)(chart, constant_1.FIRST_AXES_VIEW) }));
  236. (0, common_1.animation)((0, utils_1.deepAssign)({}, params, { chart: (0, utils_1.findViewById)(chart, constant_1.SECOND_AXES_VIEW) }));
  237. return params;
  238. }
  239. exports.animation = animation;
  240. /**
  241. * label 配置 (1. 设置 offset 偏移量默认值 2. leftView 偏移量需要 *= -1)
  242. * @param params
  243. */
  244. function label(params) {
  245. var _this = this;
  246. var _a, _b;
  247. var chart = params.chart, options = params.options;
  248. var label = options.label, yField = options.yField, layout = options.layout;
  249. var firstView = (0, utils_1.findViewById)(chart, constant_1.FIRST_AXES_VIEW);
  250. var secondView = (0, utils_1.findViewById)(chart, constant_1.SECOND_AXES_VIEW);
  251. var leftGeometry = (0, utils_1.findGeometry)(firstView, 'interval');
  252. var rightGeometry = (0, utils_1.findGeometry)(secondView, 'interval');
  253. if (!label) {
  254. leftGeometry.label(false);
  255. rightGeometry.label(false);
  256. }
  257. else {
  258. var callback = label.callback, cfg_1 = tslib_1.__rest(label, ["callback"]);
  259. /** ---- 设置默认配置 ---- */
  260. // 默认居中
  261. if (!cfg_1.position) {
  262. cfg_1.position = 'middle';
  263. }
  264. if (cfg_1.offset === undefined) {
  265. cfg_1.offset = 2;
  266. }
  267. /** ---- leftView label 设置 ---- */
  268. var leftLabelCfg = tslib_1.__assign({}, cfg_1);
  269. if ((0, utils_2.isHorizontal)(layout)) {
  270. // 设置 textAlign 默认值
  271. var textAlign = ((_a = leftLabelCfg.style) === null || _a === void 0 ? void 0 : _a.textAlign) || (cfg_1.position === 'middle' ? 'center' : 'left');
  272. cfg_1.style = (0, utils_1.deepAssign)({}, cfg_1.style, { textAlign: textAlign });
  273. var textAlignMap = { left: 'right', right: 'left', center: 'center' };
  274. leftLabelCfg.style = (0, utils_1.deepAssign)({}, leftLabelCfg.style, { textAlign: textAlignMap[textAlign] });
  275. }
  276. else {
  277. var positionMap_1 = { top: 'bottom', bottom: 'top', middle: 'middle' };
  278. if (typeof cfg_1.position === 'string') {
  279. cfg_1.position = positionMap_1[cfg_1.position];
  280. }
  281. else if (typeof cfg_1.position === 'function') {
  282. cfg_1.position = function () {
  283. var args = [];
  284. for (var _i = 0; _i < arguments.length; _i++) {
  285. args[_i] = arguments[_i];
  286. }
  287. return positionMap_1[cfg_1.position.apply(_this, args)];
  288. };
  289. }
  290. // 设置 textBaseline 默认值
  291. var textBaseline = ((_b = leftLabelCfg.style) === null || _b === void 0 ? void 0 : _b.textBaseline) || 'bottom';
  292. leftLabelCfg.style = (0, utils_1.deepAssign)({}, leftLabelCfg.style, { textBaseline: textBaseline });
  293. var textBaselineMap = { top: 'bottom', bottom: 'top', middle: 'middle' };
  294. cfg_1.style = (0, utils_1.deepAssign)({}, cfg_1.style, { textBaseline: textBaselineMap[textBaseline] });
  295. }
  296. leftGeometry.label({
  297. fields: [yField[0]],
  298. callback: callback,
  299. cfg: (0, utils_1.transformLabel)(leftLabelCfg),
  300. });
  301. rightGeometry.label({
  302. fields: [yField[1]],
  303. callback: callback,
  304. cfg: (0, utils_1.transformLabel)(cfg_1),
  305. });
  306. }
  307. return params;
  308. }
  309. /**
  310. * 对称条形图适配器
  311. * @param chart
  312. * @param options
  313. */
  314. function adaptor(params) {
  315. // flow 的方式处理所有的配置到 G2 API
  316. return (0, utils_1.flow)(geometry, meta, axis, limitInPlot, theme, label, common_1.tooltip, interaction, animation)(params);
  317. }
  318. exports.adaptor = adaptor;
  319. //# sourceMappingURL=adaptor.js.map