adaptor.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { __assign } from "tslib";
  2. import { each, omit } from '@antv/util';
  3. import { theme } from '../../adaptor/common';
  4. import { AXIS_META_CONFIG_KEYS } from '../../constant';
  5. import { deepAssign, flow, pick } from '../../utils';
  6. import { execPlotAdaptor } from '../mix/utils';
  7. import { execViewAdaptor } from './utils';
  8. function facetAdaptor(params) {
  9. var chart = params.chart, options = params.options;
  10. var facetType = options.type, data = options.data, fields = options.fields, eachView = options.eachView;
  11. var restFacetCfg = omit(options, [
  12. 'type',
  13. 'data',
  14. 'fields',
  15. 'eachView',
  16. 'axes',
  17. 'meta',
  18. 'tooltip',
  19. 'coordinate',
  20. 'theme',
  21. 'legend',
  22. 'interactions',
  23. 'annotations',
  24. ]);
  25. // 1. data
  26. chart.data(data);
  27. // 2. facet
  28. chart.facet(facetType, __assign(__assign({}, restFacetCfg), { fields: fields, eachView: function (viewOfG2, facet) {
  29. var viewOptions = eachView(viewOfG2, facet);
  30. if (viewOptions.geometries) {
  31. execViewAdaptor(viewOfG2, viewOptions);
  32. }
  33. else {
  34. var plot = viewOptions;
  35. var plotOptions = plot.options;
  36. // @ts-ignore 仪表盘没 tooltip
  37. if (plotOptions.tooltip) {
  38. // 配置 tooltip 交互
  39. viewOfG2.interaction('tooltip');
  40. }
  41. execPlotAdaptor(plot.type, viewOfG2, plotOptions);
  42. }
  43. } }));
  44. return params;
  45. }
  46. function component(params) {
  47. var chart = params.chart, options = params.options;
  48. var axes = options.axes, meta = options.meta, tooltip = options.tooltip, coordinate = options.coordinate, theme = options.theme, legend = options.legend, interactions = options.interactions, annotations = options.annotations;
  49. // 3. meta 配置
  50. var scales = {};
  51. if (axes) {
  52. each(axes, function (axis, field) {
  53. scales[field] = pick(axis, AXIS_META_CONFIG_KEYS);
  54. });
  55. }
  56. scales = deepAssign({}, meta, scales);
  57. chart.scale(scales);
  58. // 4. coordinate 配置
  59. chart.coordinate(coordinate);
  60. // 5. axis 轴配置 (默认不展示)
  61. if (!axes) {
  62. chart.axis(false);
  63. }
  64. else {
  65. each(axes, function (axis, field) {
  66. chart.axis(field, axis);
  67. });
  68. }
  69. // 6. tooltip 配置
  70. if (tooltip) {
  71. chart.interaction('tooltip');
  72. chart.tooltip(tooltip);
  73. }
  74. else if (tooltip === false) {
  75. chart.removeInteraction('tooltip');
  76. }
  77. // 7. legend 配置(默认展示)
  78. chart.legend(legend);
  79. // theme 配置
  80. if (theme) {
  81. chart.theme(theme);
  82. }
  83. // 8. interactions
  84. each(interactions, function (interaction) {
  85. if (interaction.enable === false) {
  86. chart.removeInteraction(interaction.type);
  87. }
  88. else {
  89. chart.interaction(interaction.type, interaction.cfg);
  90. }
  91. });
  92. // 9. annotations
  93. each(annotations, function (annotation) {
  94. chart.annotation()[annotation.type](__assign({}, annotation));
  95. });
  96. return params;
  97. }
  98. /**
  99. * 分面图适配器
  100. * @param chart
  101. * @param options
  102. */
  103. export function adaptor(params) {
  104. // flow 的方式处理所有的配置到 G2 API
  105. return flow(theme, facetAdaptor, component)(params);
  106. }
  107. //# sourceMappingURL=adaptor.js.map