-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Changes from 3 commits
20f608f
242b64f
f0bb34a
b3bec07
5eddb6c
06d6827
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
|
@@ -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] = { | ||
|
@@ -236,6 +245,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()}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could probably also create a |
||
} | ||
} | ||
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`. | ||
|
@@ -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] = { | ||
|
@@ -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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
There was a problem hiding this comment.
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?