Skip to content

Commit

Permalink
add a heat map series in the config
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfauquette committed May 22, 2024
1 parent 1e6f41d commit 1e78d80
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 13 deletions.
23 changes: 21 additions & 2 deletions packages/x-charts-pro/src/Heatmap/HeatmapContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import {
CartesianContextProvider,
CartesianContextProviderProps,
HighlightProvider,
ExtremumGetter,
} from '@mui/x-charts/internals';

export type ChartContainerProps = Omit<
export type HeatmapContainerProps = Omit<
ChartsSurfaceProps &
SeriesContextProviderProps &
Omit<DrawingProviderProps, 'svgRef'> &
Expand All @@ -24,7 +25,25 @@ export type ChartContainerProps = Omit<
children?: React.ReactNode;
};

const HeatmapContainer = React.forwardRef(function ChartContainer(props: ChartContainerProps, ref) {
const getBaseExtremum: ExtremumGetter<'heatmap'> = (params) => {
const { axis } = params;

const minX = Math.min(...(axis.data ?? []));
const maxX = Math.max(...(axis.data ?? []));
return [minX, maxX];
};

const xExtremumGetters = {
heatmap: getBaseExtremum,
};
const yExtremumGetters = {
heatmap: getBaseExtremum,
};

export const HeatmapContainer = React.forwardRef(function HeatmapContainer(
props: HeatmapContainerProps,
ref,
) {
const {
width,
height,
Expand Down
1 change: 1 addition & 0 deletions packages/x-charts-pro/src/typeOverloads/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './modules';
14 changes: 14 additions & 0 deletions packages/x-charts-pro/src/typeOverloads/modules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { DefaultizedProps } from '@mui/x-charts/internals';
import { BarItemIdentifier, BarSeriesType, DefaultizedBarSeriesType } from '@mui/x-charts/models';

declare module '@mui/x-charts/internals' {
interface ChartsSeriesConfig {
heatmap: {
seriesInput: DefaultizedProps<BarSeriesType, 'id'> & { color: string };
series: DefaultizedBarSeriesType;
itemIdentifier: BarItemIdentifier;
canBeStacked: true;
cartesian: true;
};
}
}
12 changes: 6 additions & 6 deletions packages/x-charts/src/context/CartesianContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ export type CartesianContextProviderProps = {
/**
* An object with x-axis extremum getters per series type.
*/
xExtremumGetters: { [T in CartesianChartSeriesType]: ExtremumGetter<T> };
xExtremumGetters: { [T in CartesianChartSeriesType]?: ExtremumGetter<T> };
/**
* An object with y-axis extremum getters per series type.
*/
yExtremumGetters: { [T in CartesianChartSeriesType]: ExtremumGetter<T> };
yExtremumGetters: { [T in CartesianChartSeriesType]?: ExtremumGetter<T> };
children: React.ReactNode;
};

Expand Down Expand Up @@ -133,17 +133,17 @@ function CartesianContextProvider(props: CartesianContextProviderProps) {
acc: ExtremumGetterResult,
chartType: T,
axis: AxisConfig,
getters: { [T2 in CartesianChartSeriesType]: ExtremumGetter<T2> },
getters: { [T2 in CartesianChartSeriesType]?: ExtremumGetter<T2> },
isDefaultAxis: boolean,
): ExtremumGetterResult => {
const getter = getters[chartType];
const series = (formattedSeries[chartType]?.series as Record<SeriesId, ChartSeries<T>>) ?? {};

const [minChartTypeData, maxChartTypeData] = getter({
const [minChartTypeData, maxChartTypeData] = getter?.({
series,
axis,
isDefaultAxis,
});
}) ?? [null, null];

const [minData, maxData] = acc;

Expand All @@ -160,7 +160,7 @@ function CartesianContextProvider(props: CartesianContextProviderProps) {

const getAxisExtremum = (
axis: AxisConfig,
getters: { [T in CartesianChartSeriesType]: ExtremumGetter<T> },
getters: { [T in CartesianChartSeriesType]?: ExtremumGetter<T> },
isDefaultAxis: boolean,
) => {
const charTypes = Object.keys(getters) as CartesianChartSeriesType[];
Expand Down
5 changes: 5 additions & 0 deletions packages/x-charts/src/internals/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ export * from '../context/HighlightProvider';
export * from '../context/InteractionProvider';
export * from '../context/SeriesContextProvider';
export * from '../context/ZAxisContextProvider';

// series configuration
export * from '../models/seriesType/config';

export * from '../models/helpers';
19 changes: 14 additions & 5 deletions packages/x-charts/src/models/seriesType/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@ import { DefaultizedProps, MakeOptional } from '../helpers';
import { StackingGroupsType } from '../../internals/stackSeries';
import { SeriesId } from './common';

interface ChartsSeriesConfig {
export interface ChartsSeriesConfig {
bar: {
seriesInput: DefaultizedProps<BarSeriesType, 'id'> & { color: string };
series: DefaultizedBarSeriesType;
canBeStacked: true;
itemIdentifier: BarItemIdentifier;
canBeStacked: true;
cartesian: true;
};
line: {
seriesInput: DefaultizedProps<LineSeriesType, 'id'> & { color: string };
series: DefaultizedLineSeriesType;
canBeStacked: true;
itemIdentifier: LineItemIdentifier;
canBeStacked: true;
cartesian: true;
};
scatter: {
seriesInput: DefaultizedProps<ScatterSeriesType, 'id'> & { color: string };
series: DefaultizedScatterSeriesType;
itemIdentifier: ScatterItemIdentifier;
cartesian: true;
};
pie: {
seriesInput: Omit<DefaultizedProps<PieSeriesType, 'id'>, 'data'> & {
Expand All @@ -34,8 +37,14 @@ interface ChartsSeriesConfig {
};
}

export type CartesianChartSeriesType = 'bar' | 'line' | 'scatter';
export type ChartSeriesType = 'bar' | 'line' | 'scatter' | 'pie';
export type ChartSeriesType = keyof ChartsSeriesConfig;

export type CartesianChartSeriesType = keyof Pick<
ChartsSeriesConfig,
{
[Key in ChartSeriesType]: ChartsSeriesConfig[Key] extends { cartesian: true } ? Key : never;
}[ChartSeriesType]
>;

export type ChartSeries<T extends ChartSeriesType> = ChartsSeriesConfig[T] extends {
canBeStacked: true;
Expand Down

0 comments on commit 1e78d80

Please sign in to comment.