adaptor.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { __assign } from "tslib";
  2. import { tooltip } from '../../adaptor/common';
  3. import { deepAssign, flow } from '../../utils';
  4. import { adaptor as columnAdaptor } from '../column/adaptor';
  5. export { meta } from '../column/adaptor';
  6. /**
  7. * 处理默认配置项
  8. * 1. switch xField、 yField
  9. * 2. switch xAxis、 yAxis and adjust axis.position configuration
  10. */
  11. function defaultOptions(params) {
  12. var options = params.options;
  13. var xField = options.xField, yField = options.yField, xAxis = options.xAxis, yAxis = options.yAxis;
  14. var position = {
  15. left: 'bottom',
  16. right: 'top',
  17. top: 'left',
  18. bottom: 'right',
  19. };
  20. var verticalAxis = yAxis !== false
  21. ? __assign({ position: position[(yAxis === null || yAxis === void 0 ? void 0 : yAxis.position) || 'left'] }, yAxis) : false;
  22. var horizontalAxis = xAxis !== false
  23. ? __assign({ position: position[(xAxis === null || xAxis === void 0 ? void 0 : xAxis.position) || 'bottom'] }, xAxis) : false;
  24. return __assign(__assign({}, params), { options: __assign(__assign({}, options), { xField: yField, yField: xField,
  25. // 条形图 xAxis,yAxis 不可以做 deepAssign
  26. xAxis: verticalAxis, yAxis: horizontalAxis }) });
  27. }
  28. /**
  29. * label 适配器
  30. * @param params
  31. */
  32. function label(params) {
  33. var options = params.options;
  34. var label = options.label;
  35. // label of bar charts default position is left, if plot has label
  36. if (label && !label.position) {
  37. label.position = 'left';
  38. // 配置默认的 label layout: 如果用户没有指定 layout 和 position, 则自动配置 layout
  39. if (!label.layout) {
  40. label.layout = [
  41. { type: 'interval-adjust-position' },
  42. { type: 'interval-hide-overlap' },
  43. { type: 'adjust-color' },
  44. { type: 'limit-in-plot', cfg: { action: 'hide' } },
  45. ];
  46. }
  47. }
  48. return deepAssign({}, params, { options: { label: label } });
  49. }
  50. /**
  51. * legend 适配器
  52. * @param params
  53. */
  54. function legend(params) {
  55. var options = params.options;
  56. // 默认 legend 位置
  57. var seriesField = options.seriesField, isStack = options.isStack;
  58. var legend = options.legend;
  59. if (seriesField) {
  60. if (legend !== false) {
  61. legend = __assign({ position: isStack ? 'top-left' : 'right-top' }, (legend || {}));
  62. }
  63. }
  64. else {
  65. legend = false;
  66. }
  67. return deepAssign({}, params, { options: { legend: legend } });
  68. }
  69. /**
  70. * coordinate 适配器
  71. * @param params
  72. */
  73. function coordinate(params) {
  74. // transpose column to bar 对角变换 & y 方向镜像变换
  75. var options = params.options;
  76. var coordinateOptions = [{ type: 'transpose' }, { type: 'reflectY' }].concat(options.coordinate || []);
  77. return deepAssign({}, params, { options: { coordinate: coordinateOptions } });
  78. }
  79. /**
  80. * 柱形图适配器
  81. * @param params
  82. */
  83. export function geometry(params) {
  84. var chart = params.chart, options = params.options;
  85. var barStyle = options.barStyle, barWidthRatio = options.barWidthRatio, minBarWidth = options.minBarWidth, maxBarWidth = options.maxBarWidth, barBackground = options.barBackground;
  86. return columnAdaptor({
  87. chart: chart,
  88. options: __assign(__assign({}, options), {
  89. // rename attrs as column
  90. columnStyle: barStyle, columnWidthRatio: barWidthRatio, minColumnWidth: minBarWidth, maxColumnWidth: maxBarWidth, columnBackground: barBackground }),
  91. }, true);
  92. }
  93. /**
  94. * @param chart
  95. * @param options
  96. */
  97. export function adaptor(params) {
  98. // flow 的方式处理所有的配置到 G2 API
  99. return flow(defaultOptions, label, legend, tooltip, coordinate, geometry)(params);
  100. }
  101. //# sourceMappingURL=adaptor.js.map