adaptor.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { __assign } from "tslib";
  2. import { get, isString } from '@antv/util';
  3. import { animation, annotation, interaction, scale, theme } from '../../adaptor/common';
  4. import { interval } from '../../adaptor/geometries';
  5. import { AXIS_META_CONFIG_KEYS } from '../../constant';
  6. import { deepAssign, flow, pick, renderGaugeStatistic } from '../../utils';
  7. import { DEFAULT_COLOR, INDICATEOR_VIEW_ID, PERCENT, RANGE_TYPE, RANGE_VALUE, RANGE_VIEW_ID } from './constants';
  8. import { getIndicatorData, getRangeData } from './utils';
  9. /**
  10. * geometry 处理
  11. * @param params
  12. */
  13. function geometry(params) {
  14. var chart = params.chart, options = params.options;
  15. var percent = options.percent, range = options.range, radius = options.radius, innerRadius = options.innerRadius, startAngle = options.startAngle, endAngle = options.endAngle, axis = options.axis, indicator = options.indicator, gaugeStyle = options.gaugeStyle, type = options.type, meter = options.meter;
  16. var color = range.color, rangeWidth = range.width;
  17. // 指标 & 指针
  18. // 如果开启在应用
  19. if (indicator) {
  20. var indicatorData = getIndicatorData(percent);
  21. var v1 = chart.createView({ id: INDICATEOR_VIEW_ID });
  22. v1.data(indicatorData);
  23. v1.point()
  24. .position("".concat(PERCENT, "*1"))
  25. .shape(indicator.shape || 'gauge-indicator')
  26. // 传入指针的样式到自定义 shape 中
  27. .customInfo({
  28. defaultColor: chart.getTheme().defaultColor,
  29. indicator: indicator,
  30. });
  31. v1.coordinate('polar', {
  32. startAngle: startAngle,
  33. endAngle: endAngle,
  34. radius: innerRadius * radius, // 外部的 innerRadius * radius = 这里的 radius
  35. });
  36. v1.axis(PERCENT, axis);
  37. // 一部分应用到 scale 中
  38. v1.scale(PERCENT, pick(axis, AXIS_META_CONFIG_KEYS));
  39. }
  40. // 辅助 range
  41. // [{ range: 1, type: '0', percent: 原始进度百分比 }]
  42. var rangeData = getRangeData(percent, options.range);
  43. var v2 = chart.createView({ id: RANGE_VIEW_ID });
  44. v2.data(rangeData);
  45. var rangeColor = isString(color) ? [color, DEFAULT_COLOR] : color;
  46. var ext = interval({
  47. chart: v2,
  48. options: {
  49. xField: '1',
  50. yField: RANGE_VALUE,
  51. seriesField: RANGE_TYPE,
  52. rawFields: [PERCENT],
  53. isStack: true,
  54. interval: {
  55. color: rangeColor,
  56. style: gaugeStyle,
  57. shape: type === 'meter' ? 'meter-gauge' : null,
  58. },
  59. args: {
  60. zIndexReversed: true,
  61. sortZIndex: true,
  62. },
  63. minColumnWidth: rangeWidth,
  64. maxColumnWidth: rangeWidth,
  65. },
  66. }).ext;
  67. var geometry = ext.geometry;
  68. // 传入到自定义 shape 中
  69. geometry.customInfo({ meter: meter });
  70. v2.coordinate('polar', {
  71. innerRadius: innerRadius,
  72. radius: radius,
  73. startAngle: startAngle,
  74. endAngle: endAngle,
  75. }).transpose();
  76. return params;
  77. }
  78. /**
  79. * meta 配置
  80. * @param params
  81. */
  82. function meta(params) {
  83. var _a;
  84. return flow(scale((_a = {
  85. range: {
  86. min: 0,
  87. max: 1,
  88. maxLimit: 1,
  89. minLimit: 0,
  90. }
  91. },
  92. _a[PERCENT] = {},
  93. _a)))(params);
  94. }
  95. /**
  96. * 统计指标文档
  97. * @param params
  98. */
  99. function statistic(params, updated) {
  100. var chart = params.chart, options = params.options;
  101. var statistic = options.statistic, percent = options.percent;
  102. // 先清空标注,再重新渲染
  103. chart.getController('annotation').clear(true);
  104. if (statistic) {
  105. var contentOption = statistic.content;
  106. var transformContent = void 0;
  107. // 当设置 content 的时候,设置默认样式
  108. if (contentOption) {
  109. transformContent = deepAssign({}, {
  110. content: "".concat((percent * 100).toFixed(2), "%"),
  111. style: {
  112. opacity: 0.75,
  113. fontSize: '30px',
  114. lineHeight: 1,
  115. textAlign: 'center',
  116. color: 'rgba(44,53,66,0.85)',
  117. },
  118. }, contentOption);
  119. }
  120. renderGaugeStatistic(chart, { statistic: __assign(__assign({}, statistic), { content: transformContent }) }, { percent: percent });
  121. }
  122. if (updated) {
  123. chart.render(true);
  124. }
  125. return params;
  126. }
  127. /**
  128. * tooltip 配置
  129. */
  130. function tooltip(params) {
  131. var chart = params.chart, options = params.options;
  132. var tooltip = options.tooltip;
  133. if (tooltip) {
  134. chart.tooltip(deepAssign({
  135. showTitle: false,
  136. showMarkers: false,
  137. containerTpl: '<div class="g2-tooltip"><div class="g2-tooltip-list"></div></div>',
  138. domStyles: {
  139. 'g2-tooltip': {
  140. padding: '4px 8px',
  141. fontSize: '10px',
  142. },
  143. },
  144. customContent: function (x, data) {
  145. var percent = get(data, [0, 'data', PERCENT], 0);
  146. return "".concat((percent * 100).toFixed(2), "%");
  147. },
  148. }, tooltip));
  149. }
  150. else {
  151. // 默认,不展示 tooltip
  152. chart.tooltip(false);
  153. }
  154. return params;
  155. }
  156. /**
  157. * other 配置
  158. * @param params
  159. */
  160. function other(params) {
  161. var chart = params.chart;
  162. chart.legend(false);
  163. return params;
  164. }
  165. /**
  166. * 对外暴露的 adaptor
  167. */
  168. export { statistic };
  169. /**
  170. * 图适配器
  171. * @param chart
  172. * @param options
  173. */
  174. export function adaptor(params) {
  175. // flow 的方式处理所有的配置到 G2 API
  176. return flow(theme,
  177. // animation 配置必须在 createView 之前,不然无法让子 View 生效
  178. animation, geometry, meta, tooltip, statistic, interaction, annotation(), other
  179. // ... 其他的 adaptor flow
  180. )(params);
  181. }
  182. //# sourceMappingURL=adaptor.js.map