Skip to content

Commit

Permalink
Merge pull request #1666 from visualize-admin/fix/container-size
Browse files Browse the repository at this point in the history
fix: Container size
  • Loading branch information
bprusinowski authored Jul 23, 2024
2 parents d16964c + 6674899 commit 4319c83
Show file tree
Hide file tree
Showing 26 changed files with 74 additions and 92 deletions.
4 changes: 2 additions & 2 deletions app/charts/area/areas-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
useAreasStateVariables,
} from "@/charts/area/areas-state-props";
import {
getChartBounds,
useChartBounds,
useChartPadding,
} from "@/charts/shared/chart-dimensions";
import {
Expand Down Expand Up @@ -336,7 +336,7 @@ const useAreasState = (
bottom,
left,
};
const bounds = getChartBounds(width, margins, height);
const bounds = useChartBounds(width, margins, height);
const { chartWidth, chartHeight } = bounds;

/** Adjust scales according to dimensions */
Expand Down
2 changes: 1 addition & 1 deletion app/charts/area/chart-area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const ChartAreas = memo((props: ChartProps<AreaConfig>) => {

return (
<AreaChart {...props}>
<ChartContainer type="area">
<ChartContainer>
<ChartSvg>
<AxisTime /> <AxisHeightLinear />
<Areas /> <AxisTimeDomain />
Expand Down
6 changes: 3 additions & 3 deletions app/charts/column/chart-column.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const ChartColumns = memo((props: ChartProps<ColumnConfig>) => {
<>
{fields.segment?.componentIri && fields.segment.type === "stacked" ? (
<StackedColumnsChart {...props}>
<ChartContainer type="column">
<ChartContainer>
<ChartSvg>
<AxisHeightLinear /> <AxisWidthBand />
<ColumnsStacked /> <AxisWidthBandDomain />
Expand Down Expand Up @@ -77,7 +77,7 @@ const ChartColumns = memo((props: ChartProps<ColumnConfig>) => {
</StackedColumnsChart>
) : fields.segment?.componentIri && fields.segment.type === "grouped" ? (
<GroupedColumnChart {...props}>
<ChartContainer type="column">
<ChartContainer>
<ChartSvg>
<AxisHeightLinear />
<AxisWidthBand />
Expand Down Expand Up @@ -108,7 +108,7 @@ const ChartColumns = memo((props: ChartProps<ColumnConfig>) => {
</GroupedColumnChart>
) : (
<ColumnChart {...props}>
<ChartContainer type="column">
<ChartContainer>
<ChartSvg>
<AxisHeightLinear />
<AxisWidthBand />
Expand Down
4 changes: 2 additions & 2 deletions app/charts/column/columns-grouped-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
PADDING_WITHIN,
} from "@/charts/column/constants";
import {
getChartBounds,
useChartBounds,
useChartPadding,
} from "@/charts/shared/chart-dimensions";
import {
Expand Down Expand Up @@ -339,7 +339,7 @@ const useColumnsGroupedState = (
bottom,
left,
};
const bounds = getChartBounds(width, margins, height);
const bounds = useChartBounds(width, margins, height);
const { chartWidth, chartHeight } = bounds;

// Adjust of scales based on chart dimensions
Expand Down
4 changes: 2 additions & 2 deletions app/charts/column/columns-stacked-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
} from "@/charts/column/columns-stacked-state-props";
import { PADDING_INNER, PADDING_OUTER } from "@/charts/column/constants";
import {
getChartBounds,
useChartBounds,
useChartPadding,
} from "@/charts/shared/chart-dimensions";
import {
Expand Down Expand Up @@ -397,7 +397,7 @@ const useColumnsStackedState = (
bottom,
left,
};
const bounds = getChartBounds(width, margins, height);
const bounds = useChartBounds(width, margins, height);
const { chartWidth, chartHeight } = bounds;

xScale.range([0, chartWidth]);
Expand Down
4 changes: 2 additions & 2 deletions app/charts/column/columns-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from "@/charts/column/columns-state-props";
import { PADDING_INNER, PADDING_OUTER } from "@/charts/column/constants";
import {
getChartBounds,
useChartBounds,
useChartPadding,
} from "@/charts/shared/chart-dimensions";
import {
Expand Down Expand Up @@ -199,7 +199,7 @@ const useColumnsState = (
left,
};

const bounds = getChartBounds(width, margins, height);
const bounds = useChartBounds(width, margins, height);
const { chartWidth, chartHeight } = bounds;

xScale.range([0, chartWidth]);
Expand Down
2 changes: 1 addition & 1 deletion app/charts/combo/chart-combo-line-column.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const ChartComboLineColumn = memo(
const dashboardFilters = useDashboardInteractiveFilters();
return (
<ComboLineColumnChart {...props}>
<ChartContainer type="comboLineColumn">
<ChartContainer>
<ChartSvg>
<AxisHeightLinearDual orientation="left" />
<AxisHeightLinearDual orientation="right" />
Expand Down
2 changes: 1 addition & 1 deletion app/charts/combo/chart-combo-line-dual.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const ChartComboLineDual = memo((props: ChartProps<ComboLineDualConfig>) => {
const dashboardFilters = useDashboardInteractiveFilters();
return (
<ComboLineDualChart {...props}>
<ChartContainer type="comboLineDual">
<ChartContainer>
<ChartSvg>
<AxisHeightLinearDual orientation="left" />
<AxisHeightLinearDual orientation="right" /> <AxisTime />
Expand Down
2 changes: 1 addition & 1 deletion app/charts/combo/chart-combo-line-single.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const ChartComboLineSingle = memo(
const dashboardFilters = useDashboardInteractiveFilters();
return (
<ComboLineSingleChart {...props}>
<ChartContainer type="comboLineSingle">
<ChartContainer>
<ChartSvg>
<AxisHeightLinear /> <AxisTime /> <AxisTimeDomain />
<ComboLineSingle />
Expand Down
4 changes: 2 additions & 2 deletions app/charts/combo/combo-line-column-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from "@/charts/combo/combo-state";
import { TICK_PADDING } from "@/charts/shared/axis-height-linear";
import {
getChartBounds,
useChartBounds,
useChartPadding,
} from "@/charts/shared/chart-dimensions";
import {
Expand Down Expand Up @@ -145,7 +145,7 @@ const useComboLineColumnState = (
);
const right = Math.max(maxRightTickWidth, 40);
const margins = getMargins({ left, right, bottom });
const bounds = getChartBounds(width, margins, height);
const bounds = useChartBounds(width, margins, height);
const { chartWidth, chartHeight } = bounds;
const xScales = [xScale, xScaleTime, xScaleTimeRange];
const yScales = [yScale, yScaleLeft, yScaleRight];
Expand Down
4 changes: 2 additions & 2 deletions app/charts/combo/combo-line-dual-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from "@/charts/combo/combo-state";
import { TICK_PADDING } from "@/charts/shared/axis-height-linear";
import {
getChartBounds,
useChartBounds,
useChartPadding,
} from "@/charts/shared/chart-dimensions";
import {
Expand Down Expand Up @@ -128,7 +128,7 @@ const useComboLineDualState = (
);
const right = Math.max(maxRightTickWidth, 40);
const margins = getMargins({ left, right, bottom });
const bounds = getChartBounds(width, margins, height);
const bounds = useChartBounds(width, margins, height);
const { chartWidth, chartHeight } = bounds;
const xScales = [xScale, xScaleTimeRange];
const yScales = [yScale, yScaleLeft, yScaleRight];
Expand Down
4 changes: 2 additions & 2 deletions app/charts/combo/combo-line-single-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
useYScales,
} from "@/charts/combo/combo-state";
import {
getChartBounds,
useChartBounds,
useChartPadding,
} from "@/charts/shared/chart-dimensions";
import {
Expand Down Expand Up @@ -109,7 +109,7 @@ const useComboLineSingleState = (
formatNumber,
});
const margins = getMargins({ left, bottom });
const bounds = getChartBounds(width, margins, height);
const bounds = useChartBounds(width, margins, height);
const { chartWidth, chartHeight } = bounds;
const xScales = [xScale, xScaleTimeRange];
const yScales = [yScale];
Expand Down
2 changes: 1 addition & 1 deletion app/charts/line/chart-lines.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const ChartLines = memo((props: ChartProps<LineConfig>) => {
const dashboardFilters = useDashboardInteractiveFilters();
return (
<LineChart {...props}>
<ChartContainer type="line">
<ChartContainer>
<ChartSvg>
<AxisHeightLinear /> <AxisTime /> <AxisTimeDomain />
<Lines />
Expand Down
4 changes: 2 additions & 2 deletions app/charts/line/lines-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
useLinesStateVariables,
} from "@/charts/line/lines-state-props";
import {
getChartBounds,
useChartBounds,
useChartPadding,
} from "@/charts/shared/chart-dimensions";
import { getWideData } from "@/charts/shared/chart-helpers";
Expand Down Expand Up @@ -228,7 +228,7 @@ const useLinesState = (
bottom,
left,
};
const bounds = getChartBounds(width, margins, height);
const bounds = useChartBounds(width, margins, height);
const { chartWidth, chartHeight } = bounds;

xScale.range([0, chartWidth]);
Expand Down
2 changes: 1 addition & 1 deletion app/charts/map/chart-map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ const ChartMap = memo((props: ChartMapProps) => {
const filters = useChartConfigFilters(chartConfig);
return (
<MapChart {...props}>
<ChartContainer type="map">
<ChartContainer>
<MapComponent />
<MapTooltip />
</ChartContainer>
Expand Down
2 changes: 1 addition & 1 deletion app/charts/pie/chart-pie.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const ChartPie = memo((props: ChartProps<PieConfig>) => {

return (
<PieChart {...props}>
<ChartContainer type="pie">
<ChartContainer>
<ChartSvg>
<Pie />
</ChartSvg>
Expand Down
4 changes: 2 additions & 2 deletions app/charts/pie/pie-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
usePieStateData,
usePieStateVariables,
} from "@/charts/pie/pie-state-props";
import { getChartBounds } from "@/charts/shared/chart-dimensions";
import { useChartBounds } from "@/charts/shared/chart-dimensions";
import {
ChartContext,
ChartStateData,
Expand Down Expand Up @@ -147,7 +147,7 @@ const usePieState = (
bottom: 40,
left: 40,
};
const bounds = getChartBounds(width, margins, height);
const bounds = useChartBounds(width, margins, height);
const { chartWidth, chartHeight } = bounds;

// Pie values
Expand Down
2 changes: 1 addition & 1 deletion app/charts/scatterplot/chart-scatterplot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const ChartScatterplot = memo((props: ChartProps<ScatterPlotConfig>) => {
const filters = useChartConfigFilters(chartConfig);
return (
<ScatterplotChart {...props}>
<ChartContainer type="scatterplot">
<ChartContainer>
<ChartSvg>
<AxisWidthLinear />
<AxisHeightLinear />
Expand Down
4 changes: 2 additions & 2 deletions app/charts/scatterplot/scatterplot-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
useScatterplotStateVariables,
} from "@/charts/scatterplot//scatterplot-state-props";
import {
getChartBounds,
useChartBounds,
useChartPadding,
} from "@/charts/shared/chart-dimensions";
import {
Expand Down Expand Up @@ -158,7 +158,7 @@ const useScatterplotState = (
bottom,
left,
};
const bounds = getChartBounds(width, margins, height);
const bounds = useChartBounds(width, margins, height);
const { chartWidth, chartHeight } = bounds;

xScale.range([0, chartWidth]);
Expand Down
19 changes: 15 additions & 4 deletions app/charts/shared/chart-dimensions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import { BRUSH_BOTTOM_SPACE } from "@/charts/shared/brush/constants";
import { getTickNumber } from "@/charts/shared/ticks";
import { TICK_FONT_SIZE } from "@/charts/shared/use-chart-theme";
import { Bounds, Margins } from "@/charts/shared/use-size";
import { ChartConfig } from "@/configurator";
import { CHART_GRID_MIN_HEIGHT } from "@/components/react-grid";
import {
ChartConfig,
hasChartConfigs,
isLayoutingFreeCanvas,
useConfiguratorState,
} from "@/configurator";
import { useDashboardInteractiveFilters } from "@/stores/interactive-filters";
import { getTextWidth } from "@/utils/get-text-width";

Expand Down Expand Up @@ -107,20 +113,25 @@ export const useChartPadding = (props: ComputeChartPaddingProps) => {
]);
};

export const getChartBounds = (
const ASPECT_RATIO = 2 / 5;

export const useChartBounds = (
width: number,
margins: Margins,
height: number
): Bounds => {
const [state] = useConfiguratorState(hasChartConfigs);
const { left, top, right, bottom } = margins;

const chartWidth = width - left - right;
const chartHeight = height - top - bottom;
const chartHeight = isLayoutingFreeCanvas(state)
? Math.max(CHART_GRID_MIN_HEIGHT, height - top - bottom)
: chartWidth * ASPECT_RATIO;

return {
width,
height: chartHeight + top + bottom,
aspectRatio: chartHeight / chartWidth,
aspectRatio: ASPECT_RATIO,
margins,
chartWidth,
chartHeight,
Expand Down
Loading

0 comments on commit 4319c83

Please sign in to comment.