Skip to content

Commit 81f105c

Browse files
committed
feat: use tick label max length with relative size
1 parent 029ea56 commit 81f105c

1 file changed

Lines changed: 13 additions & 50 deletions

File tree

  • src/platform/plugins/shared/chart_expressions/expression_xy/public/components

src/platform/plugins/shared/chart_expressions/expression_xy/public/components/xy_chart.tsx

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
99

10-
import React, { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
10+
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
1111
import { css } from '@emotion/react';
1212
import type {
1313
ElementClickListener,
@@ -19,7 +19,7 @@ import type {
1919
XYChartElementEvent,
2020
XYChartSeriesIdentifier,
2121
SettingsProps,
22-
Truncate,
22+
AxisStyle,
2323
} from '@elastic/charts';
2424
import {
2525
Chart,
@@ -309,28 +309,6 @@ export function XYChart({
309309
);
310310

311311
const dataLayers: CommonXYDataLayerConfig[] = filteredLayers.filter(isDataLayer);
312-
313-
const chartContainerRef = useRef<HTMLDivElement>(null);
314-
const [chartContainerWidth, setChartContainerWidth] = useState(0);
315-
316-
useLayoutEffect(() => {
317-
const element = chartContainerRef.current;
318-
if (!element) {
319-
return undefined;
320-
}
321-
const observer = new ResizeObserver((entries) => {
322-
const width = entries[0]?.contentRect.width;
323-
if (typeof width === 'number') {
324-
setChartContainerWidth(width);
325-
}
326-
});
327-
observer.observe(element);
328-
setChartContainerWidth(element.getBoundingClientRect().width);
329-
return () => {
330-
observer.disconnect();
331-
};
332-
}, [dataLayers.length]);
333-
334312
const isTimeViz = isTimeChart(dataLayers);
335313

336314
useEffect(() => {
@@ -694,7 +672,7 @@ export function XYChart({
694672
strokeWidth: 1,
695673
};
696674

697-
const getYAxesStyle = (axis: AxisConfiguration) => {
675+
const getYAxesStyle = (axis: AxisConfiguration): RecursivePartial<AxisStyle> => {
698676
const tickVisible = axis.showLabels;
699677
const position = getOriginalAxisPosition(axis.position, shouldRotate);
700678
const style = {
@@ -708,7 +686,7 @@ export function XYChart({
708686
inner: linesPaddings[position],
709687
}
710688
: undefined,
711-
truncation: axis.truncate ? { width: axis.truncate, position: 'end' as const } : undefined,
689+
maxLength: axis.truncate,
712690
},
713691
axisTitle: {
714692
visible: axis.showTitle,
@@ -724,34 +702,16 @@ export function XYChart({
724702
return style;
725703
};
726704

727-
const getXAxisStyle = () => {
728-
const DEFAULT_HORIZONTAL_BAR_X_AXIS_TICK_TRUNCATE_MIN_PX = 200;
729-
const DEFAULT_HORIZONTAL_BAR_X_AXIS_TICK_TRUNCATE_WIDTH_RELATIVE = 0.3;
730-
731-
let truncateStyle: Truncate | undefined =
732-
xAxisConfig?.truncate !== undefined
733-
? { width: xAxisConfig.truncate, position: 'end' }
734-
: undefined;
735-
736-
if (truncateStyle === undefined && isHorizontalChart(dataLayers) && hasBars) {
737-
const chartWidth =
738-
Number.isFinite(chartContainerWidth) && chartContainerWidth > 0 ? chartContainerWidth : 0;
739-
740-
const value = Math.floor(
741-
Math.max(
742-
DEFAULT_HORIZONTAL_BAR_X_AXIS_TICK_TRUNCATE_MIN_PX,
743-
DEFAULT_HORIZONTAL_BAR_X_AXIS_TICK_TRUNCATE_WIDTH_RELATIVE * chartWidth
744-
)
745-
);
746-
truncateStyle = { width: value, position: 'middle' as const };
747-
}
705+
const getXAxisStyle = (): RecursivePartial<AxisStyle> => {
706+
const isHorizontalBarChart = isHorizontalChart(dataLayers) && hasBars;
707+
const MAX_LENGTH_FOR_HORIZONTAL_BAR_CHART = '40%';
748708

749709
if (isHorizontalTimeAxis) {
750710
return {
751711
tickLabel: {
752712
visible: Boolean(xAxisConfig?.showLabels),
753713
fill: xAxisConfig?.labelColor,
754-
truncation: truncateStyle,
714+
maxLength: xAxisConfig?.truncate,
755715
},
756716
tickLine: {
757717
visible: Boolean(xAxisConfig?.showLabels),
@@ -768,7 +728,10 @@ export function XYChart({
768728
rotation: xAxisConfig?.labelsOrientation,
769729
padding: linesPaddings.bottom != null ? { inner: linesPaddings.bottom } : undefined,
770730
fill: xAxisConfig?.labelColor,
771-
truncation: truncateStyle,
731+
maxLength:
732+
xAxisConfig?.truncate ??
733+
(isHorizontalBarChart ? MAX_LENGTH_FOR_HORIZONTAL_BAR_CHART : undefined),
734+
truncate: isHorizontalBarChart ? ('middle' as const) : undefined,
772735
},
773736
axisTitle: {
774737
visible: xAxisConfig?.showTitle,
@@ -817,7 +780,7 @@ export function XYChart({
817780
return (
818781
<>
819782
<GlobalXYChartStyles />
820-
<div ref={chartContainerRef} css={chartContainerStyle}>
783+
<div css={chartContainerStyle}>
821784
{showLegend !== undefined && uiState && (
822785
<LegendToggle
823786
onClick={toggleLegend}

0 commit comments

Comments
 (0)