statistic.js 7.7 KB

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