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

[charts] Support BarChart with Date data #13471

Merged
merged 6 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
cleaning
  • Loading branch information
alexfauquette committed Jun 18, 2024
commit f0bb34ab3dc15b14e81a65cfb69fe2b2da69cf81
43 changes: 31 additions & 12 deletions packages/x-charts/src/context/CartesianContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,14 @@ function CartesianContextProvider(props: CartesianContextProviderProps) {
? getOrdinalColorScale({ values: axis.data, ...axis.colorMap })
: getColorScale(axis.colorMap)),
};
if (axis.scaleType === 'ordinal-time') {
if (axis.data?.[0] instanceof Date) {
const timeScale = scaleTime(axis.data!, range);
completedXAxis[axis.id].valueFormatter =
axis.valueFormatter ??
((v, { location }) =>
location === 'tick'
? timeScale.tickFormat(axis.tickNumber)(v)
: `${v.toLocaleString()}`);
// completedXAxis[axis.id].tickInterval = timeScale.ticks(axis.tickNumber);
}
}
if (isPointScaleConfig(axis)) {
Expand All @@ -246,12 +245,17 @@ function CartesianContextProvider(props: CartesianContextProviderProps) {
? getOrdinalColorScale({ values: axis.data, ...axis.colorMap })
: getColorScale(axis.colorMap)),
};
if (axis.data?.[0] instanceof Date) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would a isDateData function work here?

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

const timeScale = scaleTime(axis.data!, range);
completedXAxis[axis.id].valueFormatter =
axis.valueFormatter ??
((v, { location }) =>
location === 'tick'
? timeScale.tickFormat(axis.tickNumber)(v)
: `${v.toLocaleString()}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably also create a dateValueFormatter and just completedXAxis[axis.id].valueFormatter = dateValueFormatter(...)

}
}
if (
axis.scaleType === 'band' ||
axis.scaleType === 'point' ||
axis.scaleType === 'ordinal-time'
) {
if (axis.scaleType === 'band' || axis.scaleType === 'point') {
// Could be merged with the two previous "if conditions" but then TS does not get that `axis.scaleType` can't be `band` or `point`.
return;
}
Expand Down Expand Up @@ -311,6 +315,15 @@ function CartesianContextProvider(props: CartesianContextProviderProps) {
? getOrdinalColorScale({ values: axis.data, ...axis.colorMap })
: getColorScale(axis.colorMap)),
};
if (axis.data?.[0] instanceof Date) {
const timeScale = scaleTime(axis.data!, range);
completedXAxis[axis.id].valueFormatter =
axis.valueFormatter ??
((v, { location }) =>
location === 'tick'
? timeScale.tickFormat(axis.tickNumber)(v)
: `${v.toLocaleString()}`);
}
}
if (isPointScaleConfig(axis)) {
completedYAxis[axis.id] = {
Expand All @@ -323,12 +336,18 @@ function CartesianContextProvider(props: CartesianContextProviderProps) {
? getOrdinalColorScale({ values: axis.data, ...axis.colorMap })
: getColorScale(axis.colorMap)),
};
if (axis.data?.[0] instanceof Date) {
const timeScale = scaleTime(axis.data!, range);
completedXAxis[axis.id].valueFormatter =
axis.valueFormatter ??
((v, { location }) =>
location === 'tick'
? timeScale.tickFormat(axis.tickNumber)(v)
: `${v.toLocaleString()}`);
}
}
if (
axis.scaleType === 'band' ||
axis.scaleType === 'point' ||
axis.scaleType === 'ordinal-time'
) {

if (axis.scaleType === 'band' || axis.scaleType === 'point') {
// Could be merged with the two previous "if conditions" but then TS does not get that `axis.scaleType` can't be `band` or `point`.
return;
}
Expand Down
30 changes: 2 additions & 28 deletions packages/x-charts/src/models/axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,23 +177,6 @@ export interface AxisScaleConfig {
barGapRatio: number;
colorMap?: OrdinalColorConfig | ContinuousColorConfig | PiecewiseColorConfig;
} & Pick<TickParams, 'tickPlacement' | 'tickLabelPlacement'>;
'ordinal-time': {
scaleType: 'ordinal-time';
scale: ScaleBand<number | Date | string>;
/**
* The ratio between the space allocated for padding between two categories and the category width.
* 0 means no gap, and 1 no data.
* @default 0.2
*/
categoryGapRatio: number;
/**
* The ratio between the width of a bar, and the gap between two bars.
* 0 means no gap, and 1 no bar.
* @default 0.1
*/
barGapRatio: number;
colorMap?: OrdinalColorConfig | ContinuousColorConfig | PiecewiseColorConfig;
} & Pick<TickParams, 'tickPlacement' | 'tickLabelPlacement'>;
point: {
scaleType: 'point';
scale: ScalePoint<number | Date | string>;
Expand Down Expand Up @@ -239,13 +222,6 @@ export interface AxisScaleComputedConfig {
| ScaleSequential<string, string | null>
| ScaleThreshold<number | Date, string | null>;
};
'ordinal-time': {
colorScale?:
| ScaleOrdinal<string | number | Date, string, string | null>
| ScaleOrdinal<number, string, string | null>
| ScaleSequential<string, string | null>
| ScaleThreshold<number | Date, string | null>;
};
point: {
colorScale?:
| ScaleOrdinal<string | number | Date, string, string | null>
Expand Down Expand Up @@ -346,10 +322,8 @@ export type AxisDefaultized<

export function isBandScaleConfig(
scaleConfig: AxisConfig<ScaleName>,
): scaleConfig is
| (AxisConfig<'band'> & { scaleType: 'band' })
| (AxisConfig<'ordinal-time'> & { scaleType: 'ordinal-time' }) {
return scaleConfig.scaleType === 'band' || scaleConfig.scaleType === 'ordinal-time';
): scaleConfig is AxisConfig<'band'> & { scaleType: 'band' } {
return scaleConfig.scaleType === 'band';
}

export function isPointScaleConfig(
Expand Down
Loading