Newer
Older
import { TimeStep } from 'enums'
import { capitalize } from 'lodash'
import { DateTime } from 'luxon'
import DateChartService from 'services/dateChart.service'
import { useAppSelector } from 'store/hooks'
timeStep: TimeStep
width: number
selectedDate: DateTime
function TextAxis({
index,
dataload,
timeStep,
width,
selectedDate,
const dateChartService = new DateChartService()
const isSelectedDate = dateChartService.compareStepDate(
timeStep,
selectedDate,
dataload.date
)
const style = isSelectedDate
? 'tick-text tick-text-selected chart-ticks-x-text'
: 'tick-text chart-ticks-x-text'
switch (timeStep) {
case TimeStep.YEAR:
return (
<text y="10" dy="0.71em" transform={`translate(${width})`}>
<tspan className={style} textAnchor="middle">
{dataload.date.toLocaleString({ year: 'numeric' })}
</tspan>
</text>
)
case TimeStep.MONTH:
return (
<text y="10" dy="0.71em" transform={`translate(${width})`}>
<tspan className={style} textAnchor="middle">
{dataload.date.toLocaleString({ month: 'narrow' })}
case TimeStep.DAY: {
const renderText = () => {
if (displayAllDays) {
return (
<>
<tspan className={style} x="0" textAnchor="middle">
{dataload.date.toLocaleString({ weekday: 'narrow' })}
</tspan>
<tspan className={style} x="0" dy="1.2em" textAnchor="middle">
{dataload.date.toLocaleString({ day: 'numeric' })}
)
} else if (dataload.date.weekday === 1) {
return (
<>
<tspan className={style} x="0" textAnchor="middle">
{capitalize(
dataload.date
.toLocaleString({ weekday: 'short' })
.substring(0, 3)
)}
</tspan>
<tspan className={style} x="0" dy="1.2em" textAnchor="middle">
{dataload.date.toLocaleString({ day: 'numeric' })}
)
}
return null
}
return (
<text y="10" dy="0.71em" transform={`translate(${width})`}>
{renderText()}
<tspan className={style} x="0" textAnchor="middle">
{dataload.date.toLocaleString({ weekday: 'narrow' })}
</tspan>
<tspan className={style} x="0" dy="1.2em" textAnchor="middle">
{dataload.date.toLocaleString({ day: 'numeric' })}
case TimeStep.HALF_AN_HOUR:
return !(index % 4) ? (
<text x={width} y="10" dy="0.71em">
<tspan className={style} textAnchor="middle">
{dataload.date.hour}
</tspan>
</text>
) : null
default:
return (
<text x={width} y="10" dy="0.71em">
<tspan className={style} textAnchor="middle">
</tspan>
</text>
)
}
}
interface AxisBottomProps {
xScale: ScaleBand<string>
height: number
marginLeft: number
marginBottom: number
xScale,
height,
marginLeft,
marginBottom,
const { selectedDate } = useAppSelector(state => state.ecolyo.chart)
const dateChartService = new DateChartService()
const displayAllDays = isDuel && data.length <= 15
return (
<g
className="axis x"
transform={`translate(${marginLeft}, ${height - marginBottom})`}
>
{data.map((d, index) => (
<g
className="tick"
opacity="1"
transform={`translate(${xScale(
d.date.toLocaleString(DateTime.DATETIME_SHORT)
)}, 0)`}
>
<TextAxis
index={index}
dataload={d}
width={xScale.bandwidth() / 2}
selectedDate={selectedDate}
displayAllDays={displayAllDays}
DateTime.local().setZone('utc', {
keepLocalTime: true,
}),
<line
stroke="white"
strokeLinecap="round"
strokeDasharray={dashArray}
x1={xScale.bandwidth() / 2}
x2={xScale.bandwidth() / 2}
y1="0"
y2={-(height - marginBottom)}
) : null}
</g>
))}
</g>
)
}
export default AxisBottom