@gravity-ui/chartkit 5.16.0 → 5.17.0

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.
@@ -18,7 +18,8 @@ export function prepareTreemap(args) {
18
18
  style: Object.assign({}, DEFAULT_DATALABELS_STYLE, (_a = s.dataLabels) === null || _a === void 0 ? void 0 : _a.style),
19
19
  padding: get(s, 'dataLabels.padding', DEFAULT_DATALABELS_PADDING),
20
20
  allowOverlap: get(s, 'dataLabels.allowOverlap', false),
21
- html: get(series, 'dataLabels.html', false),
21
+ html: get(s, 'dataLabels.html', false),
22
+ align: get(s, 'dataLabels.align', 'left'),
22
23
  },
23
24
  id,
24
25
  type: s.type,
@@ -231,6 +231,7 @@ export type PreparedTreemapSeries = {
231
231
  padding: number;
232
232
  allowOverlap: boolean;
233
233
  html: boolean;
234
+ align: Required<Required<TreemapSeries>['dataLabels']>['align'];
234
235
  };
235
236
  layoutAlgorithm: `${LayoutAlgorithm}`;
236
237
  } & BasePreparedSeries & Omit<TreemapSeries, keyof BasePreparedSeries>;
@@ -1,24 +1,62 @@
1
1
  import { stratify, treemap, treemapBinary, treemapDice, treemapSlice, treemapSliceDice, treemapSquarify, } from 'd3';
2
2
  import { LayoutAlgorithm } from '../../../../../../constants';
3
+ import { getLabelsSize } from '../../../utils';
3
4
  const DEFAULT_PADDING = 1;
4
- function getLabelData(data) {
5
- return data.map((d) => {
6
- const text = d.data.name;
7
- return {
8
- text,
9
- x: d.x0,
10
- y: d.y0,
11
- width: d.x1 - d.x0,
12
- nodeData: d.data,
13
- };
14
- });
5
+ function getLabels(args) {
6
+ const { data, html, padding, align } = args;
7
+ return data.reduce((acc, d) => {
8
+ const texts = Array.isArray(d.data.name) ? d.data.name : [d.data.name];
9
+ texts.forEach((text, index) => {
10
+ var _a;
11
+ const { maxHeight: lineHeight, maxWidth: labelWidth } = (_a = getLabelsSize({ labels: [text], html })) !== null && _a !== void 0 ? _a : {};
12
+ const left = d.x0 + padding;
13
+ const right = d.x1 - padding;
14
+ const width = Math.max(0, right - left);
15
+ let x = left;
16
+ const y = index * lineHeight + d.y0 + padding;
17
+ switch (align) {
18
+ case 'left': {
19
+ x = left;
20
+ break;
21
+ }
22
+ case 'center': {
23
+ x = Math.max(left, left + (width - labelWidth) / 2);
24
+ break;
25
+ }
26
+ case 'right': {
27
+ x = Math.max(left, right - labelWidth);
28
+ break;
29
+ }
30
+ }
31
+ const item = html
32
+ ? {
33
+ content: text,
34
+ x,
35
+ y,
36
+ }
37
+ : {
38
+ text,
39
+ x,
40
+ y,
41
+ width,
42
+ nodeData: d.data,
43
+ };
44
+ acc.push(item);
45
+ });
46
+ return acc;
47
+ }, []);
15
48
  }
16
49
  export function prepareTreemapData(args) {
17
50
  var _a;
18
51
  const { series, width, height } = args;
19
52
  const dataWithRootNode = getSeriesDataWithRootNode(series);
20
53
  const hierarchy = stratify()
21
- .id((d) => d.id || d.name)
54
+ .id((d) => {
55
+ if (d.id) {
56
+ return d.id;
57
+ }
58
+ return Array.isArray(d.name) ? d.name.join() : d.name;
59
+ })
22
60
  .parentId((d) => d.parentId)(dataWithRootNode)
23
61
  .sum((d) => d.value || 0);
24
62
  const treemapInstance = treemap();
@@ -50,8 +88,19 @@ export function prepareTreemapData(args) {
50
88
  return (_b = levelOptions === null || levelOptions === void 0 ? void 0 : levelOptions.padding) !== null && _b !== void 0 ? _b : DEFAULT_PADDING;
51
89
  })(hierarchy);
52
90
  const leaves = root.leaves();
53
- const labelData = ((_a = series.dataLabels) === null || _a === void 0 ? void 0 : _a.enabled) ? getLabelData(leaves) : [];
54
- return { labelData, leaves, series, htmlElements: [] };
91
+ let labelData = [];
92
+ const htmlElements = [];
93
+ if ((_a = series.dataLabels) === null || _a === void 0 ? void 0 : _a.enabled) {
94
+ const { html, padding, align } = series.dataLabels;
95
+ const labels = getLabels({ html, padding, align, data: leaves });
96
+ if (html) {
97
+ htmlElements.push(...labels);
98
+ }
99
+ else {
100
+ labelData = labels;
101
+ }
102
+ }
103
+ return { labelData, leaves, series, htmlElements };
55
104
  }
56
105
  function getSeriesDataWithRootNode(series) {
57
106
  return series.data.reduce((acc, d) => {
@@ -126,7 +126,10 @@ const validateTreemapSeries = ({ series }) => {
126
126
  }
127
127
  });
128
128
  series.data.forEach((d) => {
129
- const idOrName = d.id || d.name;
129
+ let idOrName = d.id;
130
+ if (!idOrName) {
131
+ idOrName = Array.isArray(d.name) ? d.name.join() : d.name;
132
+ }
130
133
  if (parentIds[idOrName] && typeof d.value === 'number') {
131
134
  throw new ChartKitError({
132
135
  code: CHARTKIT_ERROR_CODE.INVALID_DATA,
@@ -3,8 +3,6 @@ export type BaseSeries = {
3
3
  visible?: boolean;
4
4
  /**
5
5
  * Options for the series data labels, appearing next to each data point.
6
- *
7
- * Note: now this option is supported only for `pie` charts.
8
6
  * */
9
7
  dataLabels?: {
10
8
  /**
@@ -3,7 +3,7 @@ import type { BaseSeries, BaseSeriesData } from './base';
3
3
  import { ChartKitWidgetLegend, RectLegendSymbolOptions } from './legend';
4
4
  export type TreemapSeriesData<T = any> = BaseSeriesData<T> & {
5
5
  /** The name of the node (used in legend, tooltip etc). */
6
- name: string;
6
+ name: string | string[];
7
7
  /** The value of the node. All nodes should have this property except nodes that have children. */
8
8
  value?: number;
9
9
  /** An id for the node. Used to group children. */
@@ -35,4 +35,11 @@ export type TreemapSeries<T = any> = BaseSeries & {
35
35
  color?: string;
36
36
  }[];
37
37
  layoutAlgorithm?: `${LayoutAlgorithm}`;
38
+ /**
39
+ * Options for the series data labels, appearing next to each data point.
40
+ * */
41
+ dataLabels?: BaseSeries['dataLabels'] & {
42
+ /** Horizontal alignment of the data label inside the tile. */
43
+ align?: 'left' | 'center' | 'right';
44
+ };
38
45
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/chartkit",
3
- "version": "5.16.0",
3
+ "version": "5.17.0",
4
4
  "description": "React component used to render charts based on any sources you need",
5
5
  "license": "MIT",
6
6
  "repository": "git@github.com:gravity-ui/ChartKit.git",