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 3 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
39 changes: 38 additions & 1 deletion 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,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)) {
completedXAxis[axis.id] = {
Expand All @@ -236,6 +245,15 @@ 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') {
// Could be merged with the two previous "if conditions" but then TS does not get that `axis.scaleType` can't be `band` or `point`.
Expand Down Expand Up @@ -297,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 @@ -309,7 +336,17 @@ 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') {
// 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
2 changes: 1 addition & 1 deletion 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 Down
Loading