Skip to content

Commit

Permalink
[charts] Harmonize charts TS
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfauquette committed Jun 4, 2024
1 parent c4c0b18 commit cd76f19
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 30 deletions.
8 changes: 5 additions & 3 deletions packages/x-charts/src/ChartsLegend/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type AnchorPosition = { horizontal: AnchorX; vertical: AnchorY };

export type Direction = 'row' | 'column';

const legendGetter: { [T in ChartSeriesType]: LegendGetter<T> } = {
const legendGetter: { [T in ChartSeriesType]?: LegendGetter<T> } = {
bar: getBarLegend,
scatter: getScatterLegend,
line: getLineLegend,
Expand All @@ -21,7 +21,9 @@ const legendGetter: { [T in ChartSeriesType]: LegendGetter<T> } = {

export function getSeriesToDisplay(series: FormattedSeries) {
return (Object.keys(series) as ChartSeriesType[]).flatMap(
<T extends ChartSeriesType>(seriesType: T) =>
legendGetter[seriesType as T](series[seriesType as T]!),
<T extends ChartSeriesType>(seriesType: T) => {
const getter = legendGetter[seriesType as T];
return getter === undefined ? [] : getter(series[seriesType as T]!);
},
);
}
43 changes: 38 additions & 5 deletions packages/x-charts/src/models/seriesType/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,68 @@ import { DefaultizedProps, MakeOptional } from '../helpers';
import { StackingGroupsType } from '../../internals/stackSeries';
import { SeriesId } from './common';

interface ChartsSeriesConfig {
export interface ChartsSeriesConfig {
bar: {
/**
* Series type when passed to the formatter (some ids are defaultised to simplify the DX)
*/
seriesInput: DefaultizedProps<BarSeriesType, 'id'> & { color: string };
/**
* Series type when stored in the context (with all the preprocessing added))
*/
series: DefaultizedBarSeriesType;
canBeStacked: true;
/**
* Series typing such that the one user need to provide
*/
seriesProp: BarSeriesType;
itemIdentifier: BarItemIdentifier;
canBeStacked: true;
cartesian: true;
};
line: {
seriesInput: DefaultizedProps<LineSeriesType, 'id'> & { color: string };
series: DefaultizedLineSeriesType;
canBeStacked: true;
seriesProp: LineSeriesType;
itemIdentifier: LineItemIdentifier;
canBeStacked: true;
cartesian: true;
};
scatter: {
seriesInput: DefaultizedProps<ScatterSeriesType, 'id'> & { color: string };
series: DefaultizedScatterSeriesType;
seriesProp: ScatterSeriesType;
itemIdentifier: ScatterItemIdentifier;
cartesian: true;
};
pie: {
seriesInput: Omit<DefaultizedProps<PieSeriesType, 'id'>, 'data'> & {
data: (MakeOptional<PieValueType, 'id'> & { color: string })[];
};
series: DefaultizedPieSeriesType;
seriesProp: PieSeriesType<MakeOptional<PieValueType, 'id'>>;
itemIdentifier: PieItemIdentifier;
};
}

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 function isCartesianSeriesType(seriesType: string): seriesType is CartesianChartSeriesType {
return ['bar', 'line', 'scatter', 'heatmap'].includes(seriesType);
}

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

export type ChartSeries<T extends ChartSeriesType> = ChartsSeriesConfig[T] extends {
canBeStacked: true;
Expand Down
44 changes: 22 additions & 22 deletions packages/x-charts/src/models/seriesType/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
import { BarItemIdentifier, BarSeriesType, DefaultizedBarSeriesType } from './bar';
import { DefaultizedLineSeriesType, LineItemIdentifier, LineSeriesType } from './line';
import { DefaultizedScatterSeriesType, ScatterItemIdentifier, ScatterSeriesType } from './scatter';
import { DefaultizedPieSeriesType, PieSeriesType, PieItemIdentifier, PieValueType } from './pie';
import { MakeOptional } from '../helpers';
import { BarSeriesType, DefaultizedBarSeriesType } from './bar';
import {
CartesianChartSeriesType,
ChartSeriesType,
ChartsSeriesConfig,
StackableChartSeriesType,
} from './config';

type AllSeriesType =
| BarSeriesType
| LineSeriesType
| ScatterSeriesType
| PieSeriesType<MakeOptional<PieValueType, 'id'>>;
// Series definition

type CartesianSeriesType = BarSeriesType | LineSeriesType | ScatterSeriesType;
type AllSeriesType<T extends ChartSeriesType = ChartSeriesType> =
ChartsSeriesConfig[T]['seriesProp'];

type DefaultizedCartesianSeriesType =
| DefaultizedBarSeriesType
| DefaultizedLineSeriesType
| DefaultizedScatterSeriesType;
type CartesianSeriesType = AllSeriesType<CartesianChartSeriesType>;

type DefaultizedSeriesType = DefaultizedCartesianSeriesType | DefaultizedPieSeriesType;
type DefaultizedSeriesType<T extends ChartSeriesType = ChartSeriesType> =
ChartsSeriesConfig[T]['series'];

type StackableSeriesType = DefaultizedBarSeriesType | DefaultizedLineSeriesType;
type DefaultizedCartesianSeriesType = DefaultizedSeriesType<CartesianChartSeriesType>;

export type SeriesItemIdentifier =
| BarItemIdentifier
| LineItemIdentifier
| ScatterItemIdentifier
| PieItemIdentifier;
type StackableSeriesType = DefaultizedSeriesType<StackableChartSeriesType>;

// item identifier

export type SeriesItemIdentifier<T extends ChartSeriesType = ChartSeriesType> =
ChartsSeriesConfig[T]['itemIdentifier'];

export * from './line';
export * from './bar';
Expand All @@ -39,6 +37,8 @@ export type {
StackableSeriesType,
};

// Helpers

export function isDefaultizedBarSeries(
series: DefaultizedSeriesType,
): series is DefaultizedBarSeriesType {
Expand Down

0 comments on commit cd76f19

Please sign in to comment.