Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use passed size in pixel without waiting for resizeObserver #2270

Merged
merged 17 commits into from
Jan 31, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: use passed size in pixel without waiting for resizeObserver
markov00 committed Dec 6, 2023

Verified

This commit was signed with the committer’s verified signature.
markov00 Marco Vettorello
commit a173dceefa3f76f0d8b374a20a24ba94b30ec2be
13 changes: 10 additions & 3 deletions packages/charts/src/components/chart.tsx
Original file line number Diff line number Diff line change
@@ -23,14 +23,14 @@ import { getElementZIndex } from './portal/utils';
import { Colors } from '../common/colors';
import { LegendPositionConfig, PointerEvent } from '../specs';
import { SpecsParser } from '../specs/specs_parser';
import { updateChartTitles } from '../state/actions/chart_settings';
import { updateChartTitles, updateParentDimensions } from '../state/actions/chart_settings';
import { onExternalPointerEvent } from '../state/actions/events';
import { onComputedZIndex } from '../state/actions/z_index';
import { chartStoreReducer, GlobalChartState } from '../state/chart_state';
import { getChartThemeSelector } from '../state/selectors/get_chart_theme';
import { getInternalIsInitializedSelector, InitStatus } from '../state/selectors/get_internal_is_intialized';
import { getLegendConfigSelector } from '../state/selectors/get_legend_config_selector';
import { ChartSize, getChartSize } from '../utils/chart_size';
import { ChartSize, getChartSize, getFixedChartSize } from '../utils/chart_size';
import { LayoutDirection } from '../utils/common';
import { LIGHT_THEME } from '../utils/themes/light_theme';

@@ -121,10 +121,17 @@ export class Chart extends React.Component<ChartProps, ChartState> {
this.unsubscribeToStore();
}

componentDidUpdate({ title, description }: Readonly<ChartProps>) {
componentDidUpdate({ title, description, size }: Readonly<ChartProps>) {
if (title !== this.props.title || description !== this.props.description) {
this.chartStore.dispatch(updateChartTitles(this.props.title, this.props.description));
}
if (size !== this.props.size) {
nickofthyme marked this conversation as resolved.
Show resolved Hide resolved
const fixedSize = getFixedChartSize(this.props.size);
// if the size is specified in pixels then update directly the store
if (fixedSize) {
this.chartStore.dispatch(updateParentDimensions({ ...fixedSize, top: 0, left: 0 }));
}
}
}

getPNGSnapshot(
31 changes: 28 additions & 3 deletions packages/charts/src/utils/chart_size.ts
Original file line number Diff line number Diff line change
@@ -19,9 +19,6 @@ export type ChartSize = number | string | ChartSizeArray | ChartSizeObject;

/** @internal */
export function getChartSize(size?: ChartSize): ChartSizeObject {
if (size === undefined) {
return {};
}
if (Array.isArray(size)) {
return {
width: size[0] === undefined ? '100%' : size[0],
@@ -40,3 +37,31 @@ export function getChartSize(size?: ChartSize): ChartSizeObject {
height: sameSize,
};
}

/**
* Return the requested size if specified in pixel, null otherwise
* @internal
*/
export function getFixedChartSize(size?: ChartSize): { width: number; height: number } | null {
if (size === undefined) {
return null;
}
if (typeof size === 'number') {
return { width: size, height: size };
}
if (Array.isArray(size)) {
const width = size[0];
const height = size[1];
if (typeof width === 'number' && typeof height === 'number') {
return { width, height };
}
return null;
}
if (typeof size === 'object') {
const { width, height } = size;
if (typeof width === 'number' && typeof height === 'number') {
return { width, height };
}
}
return null;
}
2 changes: 1 addition & 1 deletion storybook/stories/metric/2_grid.story.tsx
Original file line number Diff line number Diff line change
@@ -210,7 +210,7 @@ export const Example: ChartsStory = (_, { title, description }) => {
.flat()
.map((d) => `[${d?.value}]`)
.join(' ')}
<Chart title={title} description={description}>
<Chart title={title} description={description} size={{ height: containerHeight, width: containerWidth }}>
<Settings
theme={{
metric: {