Skip to content

[charts-pro] Funnel gap keep boundary #17725

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion packages/x-charts-pro/src/FunnelChart/FunnelPlot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ export interface FunnelPlotProps extends FunnelPlotSlotExtension {
) => void;
}

const useAggregatedData = (gap: number | undefined) => {
const useAggregatedData = (gapIn: number | undefined) => {
const seriesData = useFunnelSeriesContext();
const { xAxis, xAxisIds } = useXAxes();
const { yAxis, yAxisIds } = useYAxes();

const gap = gapIn ?? 0;

const allData = React.useMemo(() => {
if (seriesData === undefined) {
return [];
Expand Down
13 changes: 13 additions & 0 deletions packages/x-charts-pro/src/FunnelChart/seriesConfig/getRange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { CartesianRangeGetter } from '@mui/x-charts/internals';

export const xRangeGetter: CartesianRangeGetter<'funnel'> = ({ drawingArea, axis }) => {
const range: [number, number] = [drawingArea.left, drawingArea.left + drawingArea.width];
const gap = (axis.gap ?? 0) / 2;
return axis.reverse ? [range[1] + gap, range[0] - gap] : [range[0] - gap, range[1] + gap];
};

export const yRangeGetter: CartesianRangeGetter<'funnel'> = ({ drawingArea, axis }) => {
const range: [number, number] = [drawingArea.top + drawingArea.height, drawingArea.top];
const gap = (axis.gap ?? 0) / 2;
return axis.reverse ? [range[1] - gap, range[0] + gap] : [range[0] + gap, range[1] - gap];
};
3 changes: 3 additions & 0 deletions packages/x-charts-pro/src/FunnelChart/seriesConfig/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import getColor from './getColor';
import legendGetter from './legend';
import tooltipGetter from './tooltip';
import getSeriesWithDefaultValues from './getSeriesWithDefaultValues';
import { xRangeGetter, yRangeGetter } from './getRange';

export const seriesConfig: ChartSeriesTypeConfig<'funnel'> = {
seriesProcessor,
Expand All @@ -13,5 +14,7 @@ export const seriesConfig: ChartSeriesTypeConfig<'funnel'> = {
tooltipGetter,
xExtremumGetter: getExtremumX,
yExtremumGetter: getExtremumY,
xRangeGetter,
yRangeGetter,
getSeriesWithDefaultValues,
};
9 changes: 7 additions & 2 deletions packages/x-charts-pro/src/FunnelChart/useFunnelChartProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ function getCategoryAxisConfig(
categoryAxis: FunnelChartProps['categoryAxis'],
series: FunnelChartProps['series'],
isHorizontal: boolean,
gap: number,
direction: 'y',
): YAxis;
function getCategoryAxisConfig(
categoryAxis: FunnelChartProps['categoryAxis'],
series: FunnelChartProps['series'],
isHorizontal: boolean,
gap: number,
direction: 'x',
): XAxis;
function getCategoryAxisConfig<D extends 'x' | 'y' = 'x' | 'y'>(
categoryAxis: FunnelChartProps['categoryAxis'],
series: FunnelChartProps['series'],
isHorizontal: boolean,
gap: number,
direction: D,
): XAxis | YAxis {
const maxSeriesLength = Math.max(...series.map((s) => (s.data ?? []).length), 0);
Expand Down Expand Up @@ -62,6 +65,7 @@ function getCategoryAxisConfig<D extends 'x' | 'y' = 'x' | 'y'>(
// If the scaleType is not defined or is 'band', our job is simple.
if (!categoryAxis?.scaleType || categoryAxis.scaleType === 'band') {
return {
gap,
categoryGapRatio: 0,
// Use the categories as the domain if they are defined.
data: categoryAxis?.categories
Expand All @@ -85,6 +89,7 @@ function getCategoryAxisConfig<D extends 'x' | 'y' = 'x' | 'y'>(
];

return {
gap,
domainLimit: 'strict',
tickLabelPlacement: 'middle',
tickInterval: tickValues,
Expand Down Expand Up @@ -144,11 +149,11 @@ export const useFunnelChartProps = (props: FunnelChartProps) => {
} as const;

const xAxis = isHorizontal
? getCategoryAxisConfig(categoryAxis, series, isHorizontal, 'x')
? getCategoryAxisConfig(categoryAxis, series, isHorizontal, gap ?? 0, 'x')
: valueAxisConfig;
const yAxis = isHorizontal
? valueAxisConfig
: getCategoryAxisConfig(categoryAxis, series, isHorizontal, 'y');
: getCategoryAxisConfig(categoryAxis, series, isHorizontal, gap ?? 0, 'y');

const chartContainerProps: ChartContainerProProps<'funnel'> = {
...rest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,14 @@ import { getTickNumber } from '../../../../hooks/useTicks';
import { getScale } from '../../../getScale';
import { zoomScaleRange } from './zoom';
import { getAxisExtremum } from './getAxisExtremum';
import { getAxisRange } from './getAxisRange';
import type { ChartDrawingArea } from '../../../../hooks';
import { ChartSeriesConfig } from '../../models/seriesConfig';
import { ComputedAxisConfig, DefaultizedZoomOptions } from './useChartCartesianAxis.types';
import { ProcessedSeries } from '../../corePlugins/useChartSeries/useChartSeries.types';
import { GetZoomAxisFilters, ZoomData } from './zoom.types';
import { getAxisTriggerTooltip } from './getAxisTriggerTooltip';

function getRange(
drawingArea: ChartDrawingArea,
axisDirection: 'x' | 'y', // | 'rotation' | 'radius',
axis: AxisConfig<ScaleName, any, ChartsAxisProps>,
): [number, number] {
const range: [number, number] =
axisDirection === 'x'
? [drawingArea.left, drawingArea.left + drawingArea.width]
: [drawingArea.top + drawingArea.height, drawingArea.top];

return axis.reverse ? [range[1], range[0]] : range;
}

const isDateData = (data?: readonly any[]): data is Date[] => data?.[0] instanceof Date;

function createDateFormatter(
Expand Down Expand Up @@ -111,15 +99,12 @@ export function computeAxisValue<T extends ChartSeriesType>({
const zoomOption = zoomOptions?.[axis.id];
const zoom = zoomMap?.get(axis.id);
const zoomRange: [number, number] = zoom ? [zoom.start, zoom.end] : [0, 100];
const range = getRange(drawingArea, axisDirection, axis);

const [minData, maxData] = getAxisExtremum(
axis,
const range = getAxisRange(
drawingArea,
axisDirection,
axis,
seriesConfig as ChartSeriesConfig<CartesianChartSeriesType>,
axisIndex,
formattedSeries,
zoom === undefined && !zoomOption ? getFilters : undefined, // Do not apply filtering if zoom is already defined.
);

const triggerTooltip = !axis.ignoreTooltip && axisIdsTriggeringTooltip.has(axis.id);
Expand Down Expand Up @@ -191,6 +176,15 @@ export function computeAxisValue<T extends ChartSeriesType>({

const domainLimit = axis.domainLimit ?? 'nice';

const [minData, maxData] = getAxisExtremum(
axis,
axisDirection,
seriesConfig as ChartSeriesConfig<CartesianChartSeriesType>,
axisIndex,
formattedSeries,
zoom === undefined && !zoomOption ? getFilters : undefined, // Do not apply filtering if zoom is already defined.
);

const axisExtremums = [axis.min ?? minData, axis.max ?? maxData];

if (typeof domainLimit === 'function') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import type { AxisConfig } from '../../../../models';
import type { CartesianChartSeriesType } from '../../../../models/seriesType/config';
import type { ChartSeriesConfig } from '../../models/seriesConfig';
import type { ProcessedSeries } from '../../corePlugins/useChartSeries/useChartSeries.types';
import type {
CartesianRangeGetter,
CartesianRangeGetterResult,
} from '../../models/seriesConfig/cartesianRangeGetter.types';
import type { ChartDrawingArea } from '../../../../hooks/useDrawingArea';
import { isCartesianSeriesType } from '../../../isCartesian';

const defaultGetRangeX: CartesianRangeGetter<any> = ({ drawingArea, axis }) => {
const range: [number, number] = [drawingArea.left, drawingArea.left + drawingArea.width];
return axis.reverse ? [range[1], range[0]] : range;
};

const defaultGetRangeY: CartesianRangeGetter<any> = ({ drawingArea, axis }) => {
const range: [number, number] = [drawingArea.top + drawingArea.height, drawingArea.top];
return axis.reverse ? [range[1], range[0]] : range;
};

const axisRangeCallback = <TSeriesType extends CartesianChartSeriesType>(
acc: CartesianRangeGetterResult,
chartType: TSeriesType,
axis: AxisConfig,
axisDirection: 'x' | 'y',
seriesConfig: ChartSeriesConfig<TSeriesType>,
formattedSeries: ProcessedSeries<TSeriesType>,
drawingArea: ChartDrawingArea,
): CartesianRangeGetterResult => {
const getter =
axisDirection === 'x'
? (seriesConfig[chartType].xRangeGetter ?? defaultGetRangeX)
: (seriesConfig[chartType].yRangeGetter ?? defaultGetRangeY);
const series = formattedSeries[chartType]?.series ?? {};

const [minChartTypeData, maxChartTypeData] = (getter as CartesianRangeGetter<TSeriesType>)?.({
axis,
drawingArea,
series,
}) ?? [Infinity, -Infinity];

const [minData, maxData] = acc;

return [Math.min(minChartTypeData, minData), Math.max(maxChartTypeData, maxData)];
};

export const getAxisRange = <T extends CartesianChartSeriesType>(
drawingArea: ChartDrawingArea,
axisDirection: 'x' | 'y',
axis: AxisConfig,
seriesConfig: ChartSeriesConfig<T>,
formattedSeries: ProcessedSeries<T>,
) => {
const charTypes = Object.keys(seriesConfig).filter(isCartesianSeriesType);

const ranges = charTypes.reduce<CartesianRangeGetterResult>(
(acc, charType) =>
axisRangeCallback(
acc,
charType as T,
axis,
axisDirection,
seriesConfig,
formattedSeries,
drawingArea,
),
[Infinity, -Infinity],
);

if (Number.isNaN(ranges[0]) || Number.isNaN(ranges[1])) {
return [Infinity, -Infinity];
}

return ranges;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type {
CartesianChartSeriesType,
ChartSeriesDefaultized,
} from '../../../../models/seriesType/config';
import type { AxisConfig } from '../../../../models/axis';
import type { ChartDrawingArea } from '../../../../hooks/useDrawingArea';
import type { SeriesId } from '../../../../models/seriesType/common';

export type CartesianRangeGetterParams<TSeriesType extends CartesianChartSeriesType> = {
axis: AxisConfig;
drawingArea: ChartDrawingArea;
series: Record<SeriesId, ChartSeriesDefaultized<TSeriesType>>;
};

export type CartesianRangeGetterResult = [number, number];

export type CartesianRangeGetter<TSeriesType extends CartesianChartSeriesType> = (
params: CartesianRangeGetterParams<TSeriesType>,
) => CartesianRangeGetterResult;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './colorProcessor.types';
export * from './cartesianExtremumGetter.types';
export * from './cartesianRangeGetter.types';
export * from './polarExtremumGetter.types';
export * from './seriesConfig.types';
export * from './seriesProcessor.types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
} from '../../../../models/seriesType/config';
import type { ColorProcessor } from './colorProcessor.types';
import type { CartesianExtremumGetter } from './cartesianExtremumGetter.types';
import type { CartesianRangeGetter } from './cartesianRangeGetter.types';
import type { LegendGetter } from './legendGetter.types';
import type { AxisTooltipGetter, TooltipGetter } from './tooltipGetter.types';
import { PolarExtremumGetter } from './polarExtremumGetter.types';
Expand All @@ -21,6 +22,8 @@ export type ChartSeriesTypeConfig<TSeriesType extends ChartSeriesType> = {
? {
xExtremumGetter: CartesianExtremumGetter<TSeriesType>;
yExtremumGetter: CartesianExtremumGetter<TSeriesType>;
xRangeGetter?: CartesianRangeGetter<TSeriesType>;
yRangeGetter?: CartesianRangeGetter<TSeriesType>;
axisTooltipGetter?: AxisTooltipGetter<TSeriesType, 'x' | 'y'>;
}
: {}) &
Expand Down
Loading