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 2 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
24 changes: 21 additions & 3 deletions packages/x-charts/src/context/CartesianContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { scaleBand, scalePoint } from 'd3-scale';
import { scaleBand, scalePoint, scaleTime } from 'd3-scale';
import {
AxisConfig,
AxisDefaultized,
Expand Down Expand Up @@ -224,6 +224,16 @@ function CartesianContextProvider(props: CartesianContextProviderProps) {
? getOrdinalColorScale({ values: axis.data, ...axis.colorMap })
: getColorScale(axis.colorMap)),
};
if (axis.scaleType === 'ordinal-time') {
Copy link
Member Author

Choose a reason for hiding this comment

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

We could skip the craetion of the special scaletype with

Suggested change
if (axis.scaleType === 'ordinal-time') {
if (axis.data[0] instanceof Date) {

But that would add some kind of magic

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);
Copy link
Member Author

Choose a reason for hiding this comment

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

Need to be sure the generated tick exist in the data

}
}
if (isPointScaleConfig(axis)) {
completedXAxis[axis.id] = {
Expand All @@ -237,7 +247,11 @@ function CartesianContextProvider(props: CartesianContextProviderProps) {
: getColorScale(axis.colorMap)),
};
}
if (axis.scaleType === 'band' || axis.scaleType === 'point') {
if (
axis.scaleType === 'band' ||
axis.scaleType === 'point' ||
axis.scaleType === 'ordinal-time'
) {
// 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 @@ -310,7 +324,11 @@ function CartesianContextProvider(props: CartesianContextProviderProps) {
: getColorScale(axis.colorMap)),
};
}
if (axis.scaleType === 'band' || axis.scaleType === 'point') {
if (
axis.scaleType === 'band' ||
axis.scaleType === 'point' ||
axis.scaleType === 'ordinal-time'
) {
// 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
6 changes: 5 additions & 1 deletion packages/x-charts/src/hooks/useTicks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,12 @@ export function useTicks(

if (scale.bandwidth() > 0) {
// scale type = 'band'
const filteredDomain =
(typeof tickInterval === 'function' && domain.filter(tickInterval)) ||
(typeof tickInterval === 'object' && tickInterval) ||
domain;
return [
...domain.map((value) => ({
...filteredDomain.map((value) => ({
Comment on lines +104 to +109
Copy link
Member Author

Choose a reason for hiding this comment

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

A bit out of scope, but it allows filtering tickets for band scale

value,
formattedValue: valueFormatter?.(value, { location: 'tick' }) ?? `${value}`,
offset:
Expand Down
32 changes: 29 additions & 3 deletions packages/x-charts/src/models/axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export interface ChartsXAxisProps extends ChartsAxisProps {
position?: 'top' | 'bottom';
}

export type ScaleName = 'linear' | 'band' | 'point' | 'log' | 'pow' | 'sqrt' | 'time' | 'utc';
export type ScaleName = keyof AxisScaleConfig;
export type ContinuousScaleName = 'linear' | 'log' | 'pow' | 'sqrt' | 'time' | 'utc';

export interface AxisScaleConfig {
Expand All @@ -177,6 +177,23 @@ 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 @@ -222,6 +239,13 @@ 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 @@ -322,8 +346,10 @@ export type AxisDefaultized<

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

export function isPointScaleConfig(
Expand Down
Loading