statistic.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { __assign } from "tslib";
  2. import { each, get, isFunction, isNumber, isString } from '@antv/util';
  3. import { kebabCase } from './kebab-case';
  4. import { pick } from './pick';
  5. /**
  6. * @desc 生成 html-statistic 的 style 字符串 (兼容 canvas 的 shapeStyle 到 css样式上)
  7. *
  8. * @param width
  9. * @param style
  10. */
  11. export function adapteStyle(style) {
  12. var styleObject = {
  13. overflow: 'hidden',
  14. 'white-space': 'nowrap',
  15. 'text-overflow': 'ellipsis',
  16. display: 'flex',
  17. justifyContent: 'center',
  18. alignItems: 'center',
  19. };
  20. var shapeStyleKeys = [
  21. 'stroke',
  22. 'lineWidth',
  23. 'shadowColor',
  24. 'strokeOpacity',
  25. 'shadowBlur',
  26. 'shadowOffsetX',
  27. 'shadowOffsetY',
  28. 'fill',
  29. ];
  30. // 兼容 shapeStyle 设置 · start
  31. if (get(style, 'fill')) {
  32. styleObject['color'] = style['fill'];
  33. }
  34. var _a = pick(style, shapeStyleKeys), shadowColor = _a.shadowColor, _b = _a.shadowBlur, shadowBlur = _b === void 0 ? 0 : _b, _c = _a.shadowOffsetX, shadowOffsetX = _c === void 0 ? 0 : _c, _d = _a.shadowOffsetY, shadowOffsetY = _d === void 0 ? 0 : _d;
  35. styleObject['text-shadow'] = "".concat([shadowColor, "".concat(shadowOffsetX, "px"), "".concat(shadowOffsetY, "px"), "".concat(shadowBlur, "px")].join(' '));
  36. var _e = pick(style, shapeStyleKeys), stroke = _e.stroke, _f = _e.lineWidth, lineWidth = _f === void 0 ? 0 : _f;
  37. styleObject['-webkit-text-stroke'] = "".concat(["".concat(lineWidth, "px"), stroke].join(' '));
  38. // 兼容 shapeStyle 设置 · end
  39. each(style, function (v, k) {
  40. // 兼容 shapeStyle 的 fontSize 没有单位
  41. if (['fontSize'].includes(k) && isNumber(v)) {
  42. styleObject[kebabCase(k)] = "".concat(v, "px");
  43. }
  44. else if (k && !shapeStyleKeys.includes(k)) {
  45. styleObject[kebabCase(k)] = "".concat(v);
  46. }
  47. });
  48. return styleObject;
  49. }
  50. /**
  51. * @desc 设置 html-statistic 容器的默认样式
  52. *
  53. * - 默认事件穿透
  54. */
  55. export function setStatisticContainerStyle(container, style) {
  56. container.style['pointer-events'] = 'none';
  57. each(style, function (v, k) {
  58. if (k && v) {
  59. container.style[k] = v;
  60. }
  61. });
  62. }
  63. /**
  64. * 渲染环图 html-annotation(默认 position 居中 [50%, 50%])
  65. * @param chart
  66. * @param options
  67. * @param meta 字段元信息
  68. * @param {optional} datum 当前的元数据
  69. */
  70. export var renderStatistic = function (chart, options, datum) {
  71. var statistic = options.statistic, plotType = options.plotType;
  72. var titleOpt = statistic.title, contentOpt = statistic.content;
  73. [titleOpt, contentOpt].forEach(function (option, idx) {
  74. if (!option) {
  75. return;
  76. }
  77. var transform = '';
  78. if (idx === 0) {
  79. transform = contentOpt ? 'translate(-50%, -100%)' : 'translate(-50%, -50%)';
  80. }
  81. else {
  82. transform = titleOpt ? 'translate(-50%, 0)' : 'translate(-50%, -50%)';
  83. }
  84. var style = isFunction(option.style) ? option.style(datum) : option.style;
  85. chart.annotation().html(__assign({ position: ['50%', '50%'], html: function (container, view) {
  86. var coordinate = view.getCoordinate();
  87. var containerW = 0;
  88. if (plotType === 'pie' || plotType === 'ring-progress') {
  89. containerW = coordinate.getRadius() * coordinate.innerRadius * 2;
  90. }
  91. else if (plotType === 'liquid') {
  92. var liquidShape = get(view.geometries, [0, 'elements', 0, 'shape']);
  93. if (liquidShape) {
  94. // 获取到水波图边框大小
  95. var path = liquidShape.find(function (t) { return t.get('name') === 'wrap'; });
  96. var width = path.getCanvasBBox().width;
  97. containerW = width;
  98. }
  99. }
  100. else if (!containerW) {
  101. // 保底方案
  102. containerW = coordinate.getWidth();
  103. }
  104. setStatisticContainerStyle(container, __assign({ width: "".concat(containerW, "px"), transform: transform }, adapteStyle(style)));
  105. var filteredData = view.getData();
  106. if (option.customHtml) {
  107. return option.customHtml(container, view, datum, filteredData);
  108. }
  109. var text = option.content;
  110. if (option.formatter) {
  111. text = option.formatter(datum, filteredData);
  112. }
  113. // todo G2 层修复可以返回空字符串 & G2 层修复允许返回非字符串的内容,比如数值 number
  114. return text ? (isString(text) ? text : "".concat(text)) : '<div></div>';
  115. },
  116. // @ts-ignore
  117. key: "".concat(idx === 0 ? 'top' : 'bottom', "-statistic") }, pick(option, ['offsetX', 'offsetY', 'rotate', 'style', 'formatter']) /** 透传配置 */));
  118. });
  119. };
  120. /**
  121. * 渲染 html-annotation for gauge (等不规则 plot), 默认 position 居中居底 [50%, 100%])
  122. * @param chart
  123. * @param options
  124. * @param meta 字段元信息
  125. * @param {optional} datum 当前的元数据
  126. */
  127. export var renderGaugeStatistic = function (chart, options, datum) {
  128. var statistic = options.statistic;
  129. var titleOpt = statistic.title, contentOpt = statistic.content;
  130. [titleOpt, contentOpt].forEach(function (option) {
  131. if (!option) {
  132. return;
  133. }
  134. var style = isFunction(option.style) ? option.style(datum) : option.style;
  135. chart.annotation().html(__assign({ position: ['50%', '100%'], html: function (container, view) {
  136. var coordinate = view.getCoordinate();
  137. // 弧形的坐标
  138. var polarCoord = view.views[0].getCoordinate();
  139. var polarCenter = polarCoord.getCenter();
  140. var polarRadius = polarCoord.getRadius();
  141. var polarMaxY = Math.max(Math.sin(polarCoord.startAngle), Math.sin(polarCoord.endAngle)) * polarRadius;
  142. var offsetY = polarCenter.y + polarMaxY - coordinate.y.start - parseFloat(get(style, 'fontSize', 0));
  143. var containerWidth = coordinate.getRadius() * coordinate.innerRadius * 2;
  144. setStatisticContainerStyle(container, __assign({ width: "".concat(containerWidth, "px"), transform: "translate(-50%, ".concat(offsetY, "px)") }, adapteStyle(style)));
  145. var filteredData = view.getData();
  146. if (option.customHtml) {
  147. return option.customHtml(container, view, datum, filteredData);
  148. }
  149. var text = option.content;
  150. if (option.formatter) {
  151. text = option.formatter(datum, filteredData);
  152. }
  153. // todo G2 层修复可以返回空字符串 & G2 层修复允许返回非字符串的内容,比如数值 number
  154. return text ? (isString(text) ? text : "".concat(text)) : '<div></div>';
  155. } }, pick(option, ['offsetX', 'offsetY', 'rotate', 'style', 'formatter']) /** 透传配置 */));
  156. });
  157. };
  158. //# sourceMappingURL=statistic.js.map