@gravity-ui/charts 1.5.1 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/dist/cjs/components/ChartInner/index.js +3 -5
  2. package/dist/cjs/constants/defaults/data-labels.d.ts +2 -0
  3. package/dist/cjs/constants/defaults/data-labels.js +5 -0
  4. package/dist/cjs/constants/defaults/index.d.ts +1 -0
  5. package/dist/cjs/constants/defaults/index.js +1 -0
  6. package/dist/cjs/hooks/useSeries/constants.d.ts +1 -2
  7. package/dist/cjs/hooks/useSeries/constants.js +0 -5
  8. package/dist/cjs/hooks/useSeries/prepare-area.js +2 -1
  9. package/dist/cjs/hooks/useSeries/prepare-bar-x.js +2 -1
  10. package/dist/cjs/hooks/useSeries/prepare-bar-y.js +1 -1
  11. package/dist/cjs/hooks/useSeries/prepare-legend.js +1 -1
  12. package/dist/cjs/hooks/useSeries/prepare-line.js +2 -2
  13. package/dist/cjs/hooks/useSeries/prepare-pie.js +3 -2
  14. package/dist/cjs/hooks/useSeries/prepare-radar.js +2 -2
  15. package/dist/cjs/hooks/useSeries/prepare-sankey.js +1 -1
  16. package/dist/cjs/hooks/useSeries/prepare-treemap.js +2 -2
  17. package/dist/cjs/hooks/useSeries/prepare-waterfall.js +2 -2
  18. package/dist/cjs/hooks/useSeries/types.d.ts +1 -0
  19. package/dist/cjs/hooks/useShapes/HtmlLayer.js +2 -1
  20. package/dist/cjs/hooks/useShapes/pie/prepare-data.js +84 -30
  21. package/dist/cjs/hooks/useShapes/pie/utils.js +2 -1
  22. package/dist/cjs/hooks/useShapes/treemap/prepare-data.js +19 -11
  23. package/dist/cjs/types/chart/pie.d.ts +8 -0
  24. package/dist/cjs/types/chart-ui.d.ts +1 -1
  25. package/dist/cjs/utils/chart/text.d.ts +1 -1
  26. package/dist/cjs/utils/chart/text.js +13 -7
  27. package/dist/cjs/utils/chart-ui/pie-center-text.js +5 -1
  28. package/dist/esm/components/ChartInner/index.js +3 -5
  29. package/dist/esm/constants/defaults/data-labels.d.ts +2 -0
  30. package/dist/esm/constants/defaults/data-labels.js +5 -0
  31. package/dist/esm/constants/defaults/index.d.ts +1 -0
  32. package/dist/esm/constants/defaults/index.js +1 -0
  33. package/dist/esm/hooks/useSeries/constants.d.ts +1 -2
  34. package/dist/esm/hooks/useSeries/constants.js +0 -5
  35. package/dist/esm/hooks/useSeries/prepare-area.js +2 -1
  36. package/dist/esm/hooks/useSeries/prepare-bar-x.js +2 -1
  37. package/dist/esm/hooks/useSeries/prepare-bar-y.js +1 -1
  38. package/dist/esm/hooks/useSeries/prepare-legend.js +1 -1
  39. package/dist/esm/hooks/useSeries/prepare-line.js +2 -2
  40. package/dist/esm/hooks/useSeries/prepare-pie.js +3 -2
  41. package/dist/esm/hooks/useSeries/prepare-radar.js +2 -2
  42. package/dist/esm/hooks/useSeries/prepare-sankey.js +1 -1
  43. package/dist/esm/hooks/useSeries/prepare-treemap.js +2 -2
  44. package/dist/esm/hooks/useSeries/prepare-waterfall.js +2 -2
  45. package/dist/esm/hooks/useSeries/types.d.ts +1 -0
  46. package/dist/esm/hooks/useShapes/HtmlLayer.js +2 -1
  47. package/dist/esm/hooks/useShapes/pie/prepare-data.js +84 -30
  48. package/dist/esm/hooks/useShapes/pie/utils.js +2 -1
  49. package/dist/esm/hooks/useShapes/treemap/prepare-data.js +19 -11
  50. package/dist/esm/types/chart/pie.d.ts +8 -0
  51. package/dist/esm/types/chart-ui.d.ts +1 -1
  52. package/dist/esm/utils/chart/text.d.ts +1 -1
  53. package/dist/esm/utils/chart/text.js +13 -7
  54. package/dist/esm/utils/chart-ui/pie-center-text.js +5 -1
  55. package/package.json +1 -1
@@ -14,12 +14,17 @@ export function handleOverflowingText(tSpan, maxWidth) {
14
14
  revertRotation.setRotate(-angle, 0, 0);
15
15
  textNode === null || textNode === void 0 ? void 0 : textNode.transform.baseVal.appendItem(revertRotation);
16
16
  let text = tSpan.textContent || '';
17
- let textLength = ((_b = tSpan.getBoundingClientRect()) === null || _b === void 0 ? void 0 : _b.width) || 0;
17
+ // We believe that if the text goes beyond the boundaries of less than a pixel, it's not a big deal.
18
+ // Math.floor helps to solve the problem with the difference in rounding when comparing textLength with maxWidth.
19
+ let textLength = Math.floor(((_b = tSpan.getBoundingClientRect()) === null || _b === void 0 ? void 0 : _b.width) || 0);
18
20
  while (textLength > maxWidth && text.length > 1) {
19
21
  text = text.slice(0, -1);
20
22
  tSpan.textContent = text + '…';
21
23
  textLength = ((_c = tSpan.getBoundingClientRect()) === null || _c === void 0 ? void 0 : _c.width) || 0;
22
24
  }
25
+ if (textLength > maxWidth) {
26
+ tSpan.textContent = '';
27
+ }
23
28
  textNode === null || textNode === void 0 ? void 0 : textNode.transform.baseVal.removeItem((textNode === null || textNode === void 0 ? void 0 : textNode.transform.baseVal.length) - 1);
24
29
  }
25
30
  export function setEllipsisForOverflowText(selection, maxWidth) {
@@ -64,21 +69,22 @@ function renderLabels(selection, { labels, style = {}, attrs = {}, }) {
64
69
  return text;
65
70
  }
66
71
  export function getLabelsSize({ labels, style, rotation, html, }) {
67
- var _a, _b, _c, _d, _e;
72
+ var _a, _b, _c, _d, _e, _f, _g;
68
73
  if (!labels.filter(Boolean).length) {
69
74
  return { maxHeight: 0, maxWidth: 0 };
70
75
  }
71
76
  const container = select(document.body).append('div');
72
- // TODO: Why do we need this styles?
73
- // .attr('class', 'chartkit chartkit-theme_common');
74
77
  const result = { maxHeight: 0, maxWidth: 0 };
75
78
  let labelWrapper;
76
79
  if (html) {
77
80
  labelWrapper = container
78
81
  .append('div')
79
82
  .style('position', 'absolute')
83
+ .style('display', 'inline-block')
80
84
  .style('font-size', (_a = style === null || style === void 0 ? void 0 : style.fontSize) !== null && _a !== void 0 ? _a : '')
81
85
  .style('font-weight', (_b = style === null || style === void 0 ? void 0 : style.fontWeight) !== null && _b !== void 0 ? _b : '')
86
+ .style('max-width', (_c = style === null || style === void 0 ? void 0 : style.maxWidth) !== null && _c !== void 0 ? _c : '')
87
+ .style('max-height', (_d = style === null || style === void 0 ? void 0 : style.maxHeight) !== null && _d !== void 0 ? _d : '')
82
88
  .node();
83
89
  const { height, width } = labels.reduce((acc, l) => {
84
90
  var _a, _b;
@@ -102,9 +108,9 @@ export function getLabelsSize({ labels, style, rotation, html, }) {
102
108
  .attr('text-anchor', rotation > 0 ? 'start' : 'end')
103
109
  .style('transform', `rotate(${rotation}deg)`);
104
110
  }
105
- const rect = (_c = svg.select('g').node()) === null || _c === void 0 ? void 0 : _c.getBoundingClientRect();
106
- result.maxWidth = (_d = rect === null || rect === void 0 ? void 0 : rect.width) !== null && _d !== void 0 ? _d : 0;
107
- result.maxHeight = (_e = rect === null || rect === void 0 ? void 0 : rect.height) !== null && _e !== void 0 ? _e : 0;
111
+ const rect = (_e = svg.select('g').node()) === null || _e === void 0 ? void 0 : _e.getBoundingClientRect();
112
+ result.maxWidth = (_f = rect === null || rect === void 0 ? void 0 : rect.width) !== null && _f !== void 0 ? _f : 0;
113
+ result.maxHeight = (_g = rect === null || rect === void 0 ? void 0 : rect.height) !== null && _g !== void 0 ? _g : 0;
108
114
  }
109
115
  container.remove();
110
116
  return result;
@@ -11,7 +11,11 @@ export function pieCenterText(text, options) {
11
11
  return function (args) {
12
12
  let fontSize = MAX_FONT_SIZE;
13
13
  const textSize = getLabelsSize({ labels: [text], style: { fontSize: `${fontSize}px` } });
14
- fontSize = (fontSize * (args.series.innerRadius - padding) * 2) / textSize.maxWidth;
14
+ let availableSpace = args.series.innerRadius * 2;
15
+ if (padding < args.series.innerRadius) {
16
+ availableSpace -= padding * 2;
17
+ }
18
+ fontSize = (fontSize * availableSpace) / textSize.maxWidth;
15
19
  const container = create('svg:g');
16
20
  container
17
21
  .append('text')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/charts",
3
- "version": "1.5.1",
3
+ "version": "1.6.1",
4
4
  "description": "React component used to render charts",
5
5
  "license": "MIT",
6
6
  "main": "dist/cjs/index.js",