adaptor.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { __assign } from "tslib";
  2. import { each } from '@antv/util';
  3. import { animation, annotation, interaction, theme, tooltip } from '../../adaptor/common';
  4. import { geometry as geometryAdaptor } from '../../adaptor/geometries/base';
  5. import { AXIS_META_CONFIG_KEYS } from '../../constant';
  6. import { PLOT_CONTAINER_OPTIONS } from '../../core/plot';
  7. import { deepAssign, flow, pick } from '../../utils';
  8. import { execPlotAdaptor } from './utils';
  9. /**
  10. * geometry 处理
  11. * @param params
  12. */
  13. function multiView(params) {
  14. var chart = params.chart, options = params.options;
  15. var views = options.views, legend = options.legend;
  16. each(views, function (v) {
  17. var region = v.region, data = v.data, meta = v.meta, axes = v.axes, coordinate = v.coordinate, interactions = v.interactions, annotations = v.annotations, tooltip = v.tooltip, geometries = v.geometries;
  18. // 1. 创建 view
  19. var viewOfG2 = chart.createView({
  20. region: region,
  21. });
  22. // 2. data
  23. viewOfG2.data(data);
  24. // 3. meta
  25. var scales = {};
  26. if (axes) {
  27. each(axes, function (axis, field) {
  28. scales[field] = pick(axis, AXIS_META_CONFIG_KEYS);
  29. });
  30. }
  31. scales = deepAssign({}, meta, scales);
  32. viewOfG2.scale(scales);
  33. // 4. x y axis
  34. if (!axes) {
  35. viewOfG2.axis(false);
  36. }
  37. else {
  38. each(axes, function (axis, field) {
  39. viewOfG2.axis(field, axis);
  40. });
  41. }
  42. // 5. coordinate
  43. viewOfG2.coordinate(coordinate);
  44. // 6. geometry
  45. each(geometries, function (geometry) {
  46. var ext = geometryAdaptor({
  47. chart: viewOfG2,
  48. options: geometry,
  49. }).ext;
  50. // adjust
  51. var adjust = geometry.adjust;
  52. if (adjust) {
  53. ext.geometry.adjust(adjust);
  54. }
  55. });
  56. // 7. interactions
  57. each(interactions, function (interaction) {
  58. if (interaction.enable === false) {
  59. viewOfG2.removeInteraction(interaction.type);
  60. }
  61. else {
  62. viewOfG2.interaction(interaction.type, interaction.cfg);
  63. }
  64. });
  65. // 8. annotations
  66. each(annotations, function (annotation) {
  67. viewOfG2.annotation()[annotation.type](__assign({}, annotation));
  68. });
  69. // 9. animation (先做动画)
  70. if (typeof v.animation === 'boolean') {
  71. viewOfG2.animate(false);
  72. }
  73. else {
  74. viewOfG2.animate(true);
  75. // 9.1 所有的 Geometry 都使用同一动画(各个图形如有区别,todo 自行覆盖)
  76. each(viewOfG2.geometries, function (g) {
  77. g.animate(v.animation);
  78. });
  79. }
  80. if (tooltip) {
  81. // 10. tooltip
  82. viewOfG2.interaction('tooltip');
  83. viewOfG2.tooltip(tooltip);
  84. }
  85. });
  86. // legend
  87. if (!legend) {
  88. chart.legend(false);
  89. }
  90. else {
  91. each(legend, function (l, field) {
  92. chart.legend(field, l);
  93. });
  94. }
  95. // tooltip
  96. chart.tooltip(options.tooltip);
  97. return params;
  98. }
  99. /**
  100. * 支持嵌套使用 g2plot 内置图表
  101. * @param params
  102. */
  103. function multiPlot(params) {
  104. var chart = params.chart, options = params.options;
  105. var plots = options.plots, _a = options.data, data = _a === void 0 ? [] : _a;
  106. each(plots, function (plot) {
  107. var type = plot.type, region = plot.region, _a = plot.options, options = _a === void 0 ? {} : _a, top = plot.top;
  108. var tooltip = options.tooltip;
  109. if (top) {
  110. execPlotAdaptor(type, chart, __assign(__assign({}, options), { data: data }));
  111. return;
  112. }
  113. var viewOfG2 = chart.createView(__assign({ region: region }, pick(options, PLOT_CONTAINER_OPTIONS)));
  114. if (tooltip) {
  115. // 配置 tooltip 交互
  116. viewOfG2.interaction('tooltip');
  117. }
  118. execPlotAdaptor(type, viewOfG2, __assign({ data: data }, options));
  119. });
  120. return params;
  121. }
  122. /**
  123. * 处理缩略轴的 adaptor (mix)
  124. * @param params
  125. */
  126. export function slider(params) {
  127. var chart = params.chart, options = params.options;
  128. chart.option('slider', options.slider);
  129. return params;
  130. }
  131. /**
  132. * 图适配器
  133. * @param chart
  134. * @param options
  135. */
  136. export function adaptor(params) {
  137. return flow(animation, // 多 view 的图,动画配置放到最前面
  138. multiView, multiPlot, interaction, animation, theme, tooltip, slider, annotation()
  139. // ... 其他的 adaptor flow
  140. )(params);
  141. }
  142. //# sourceMappingURL=adaptor.js.map