Skip to content

Commit 3758756

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

1 file changed

Lines changed: 10 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: 10 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,15 @@ 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;
748707

749708
if (isHorizontalTimeAxis) {
750709
return {
751710
tickLabel: {
752711
visible: Boolean(xAxisConfig?.showLabels),
753712
fill: xAxisConfig?.labelColor,
754-
truncation: truncateStyle,
713+
maxLength: xAxisConfig?.truncate,
755714
},
756715
tickLine: {
757716
visible: Boolean(xAxisConfig?.showLabels),
@@ -768,7 +727,8 @@ export function XYChart({
768727
rotation: xAxisConfig?.labelsOrientation,
769728
padding: linesPaddings.bottom != null ? { inner: linesPaddings.bottom } : undefined,
770729
fill: xAxisConfig?.labelColor,
771-
truncation: truncateStyle,
730+
maxLength: xAxisConfig?.truncate ?? (isHorizontalBarChart ? '50%' : undefined),
731+
truncate: isHorizontalBarChart ? ('middle' as const) : undefined,
772732
},
773733
axisTitle: {
774734
visible: xAxisConfig?.showTitle,
@@ -817,7 +777,7 @@ export function XYChart({
817777
return (
818778
<>
819779
<GlobalXYChartStyles />
820-
<div ref={chartContainerRef} css={chartContainerStyle}>
780+
<div css={chartContainerStyle}>
821781
{showLegend !== undefined && uiState && (
822782
<LegendToggle
823783
onClick={toggleLegend}

0 commit comments

Comments
 (0)