Newer
Older
import { FluidType, TimeStep } from 'enums'
import React, { useEffect, useState } from 'react'
import DateChartService from 'services/dateChart.service'
import { useAppDispatch, useAppSelector } from 'store/hooks'
xScale: ScaleBand<string>
yScale: ScaleLinear<number, number>
height: number
isMultiMissingFluid?: boolean
weekdays?: 'week' | 'weekend'
clickable?: boolean
const Bar = ({
index,
dataload,
compareDataload,
xScale,
yScale,
height,
isSwitching,
isMultiMissingFluid,
weekdays,
clickable = true,
const dispatch = useAppDispatch()
const { selectedDate } = useAppSelector(state => state.ecolyo.chart)
const [clicked, setClicked] = useState(false)
const [animationEnded, setAnimationEnded] = useState(false)
const [compareAnimationEnded, setCompareAnimationEnded] = useState(false)
const fluidStyle =
fluidType === FluidType.MULTIFLUID ? 'MULTIFLUID' : FluidType[fluidType]
if (!isSwitching && !isDuel && clickable) {
}
const onAnimationEnd = () => {
setClicked(false)
setAnimationEnded(true)
}
const onCompareAnimationEnd = () => {
setClicked(false)
setCompareAnimationEnded(true)
}
const tempYScale: number | undefined = yScale(dataload.value)
const yScaleValue: number = tempYScale ? tempYScale : 0
const tempCompareYScale: number | undefined = yScale(
compareDataload ? compareDataload.value : 0
)
const yScaleCompareValue: number = tempCompareYScale ? tempCompareYScale : 0
const tempXScale: number | undefined = xScale(
dataload.date.toLocaleString(DateTime.DATETIME_SHORT)
)
const xScaleValue: number = tempXScale ? tempXScale : 0
const isSelectedDate = isDuel
? false
: new DateChartService().compareStepDate(
timeStep,
selectedDate,
dataload.date
)
const delayIndex = index % 13
const edgeBrowser = browser && browser.name !== 'edge'
const selected = isSelectedDate ? ' selected' : ''
const bounce = edgeBrowser ? '1' : '3'
const disabled = `${clickable ? '' : 'disabled'}`
const getBarClass = () => {
const bounceDelay = ` bounce-${bounce} delay--${delayIndex}`
const fluidWeekdays = `bar-${fluidStyle} ${weekdays}`
if (clicked) {
return `${fluidWeekdays}${selected} bounce-2 delay`
} else if (animationEnded) {
return `${fluidWeekdays}${selected} ${disabled}`
return `${fluidWeekdays}${bounceDelay}${selected} ${disabled}`
}
const getCompareBarClass = () => {
const bounceValue = clicked ? (edgeBrowser ? '2' : '3') : bounce
const bounceDelay = ` bounce-${bounceValue} delay--${
clicked ? 0 : delayIndex
}`
const fluidStyleClass = `bar-compare-${fluidStyle}`
if (clicked) {
return `${fluidStyleClass} ${selected}${bounceDelay}`
}
if (isSelectedDate) {
return compareAnimationEnded
? `${fluidStyleClass} ${selected}`
: `${fluidStyleClass} ${selected}${bounceDelay}`
}
return compareAnimationEnded
? fluidStyleClass
: `${fluidStyleClass} ${bounceDelay}`
}
const compareBarClass = getCompareBarClass()
const barClass = getBarClass()
const barBackgroundClass = isSelectedDate
return compare ? xScale.bandwidth() / 2 : xScale.bandwidth()
_x: number,
_y: number,
_width: number,
_height: number
): string => {
const radius = _height > 4 ? 4 : _height / 4
' a' +
radius +
',' +
radius +
' 0 0 1 ' +
radius +
',' +
-radius +
'h' +
'a' +
radius +
',' +
radius +
' 0 0 1 ' +
radius +
',' +
radius +
'v' +
<g
transform={`translate(${xScaleValue}, -40)`}
className={`barContainer ${disabled}`}
onClick={!weekdays ? handleClick : () => undefined}
width={compare ? getBandWidth() * 2 : getBandWidth()}
height={height + 40}
className={`background-${barBackgroundClass}`}
{height > 0 && dataload.value >= 0 && isMultiMissingFluid ? (

Guilhem CARRON
committed
<g
transform={`translate(${xScaleValue}, ${yScaleValue})`}
fill="#00000"

Guilhem CARRON
committed
>
<defs>
<linearGradient
id="gradient"
className={barClass}
x1="0"
x2="0"
y1="0"
y2="1"
>
<stop id="stop-color-1" offset="0%" />
<stop id="stop-color-2" offset="100%" />
</linearGradient>
<pattern
id="diagonalHatch"
patternUnits="userSpaceOnUse"

Guilhem CARRON
committed
patternTransform="scale(1) rotate(-45)"

Guilhem CARRON
committed
<rect x="0" y="0" width="100%" height="100%" fill="#121212" />
<path d="M0 8h20z" strokeWidth="3" stroke="#71612E" fill="none" />
</pattern>
<pattern
id="diagonalHatchSelected"
patternUnits="userSpaceOnUse"

Guilhem CARRON
committed
patternTransform="scale(1) rotate(-45)"

Guilhem CARRON
committed
<rect x="0" y="0" width="100%" height="100%" fill="#121212" />
<path d="M0 8h20z" strokeWidth="3" stroke="#E3B82A" fill="none" />
</pattern>
</defs>
<path
d={topRoundedRect(
compare ? getBandWidth() : 0,
0,
getBandWidth(),
height - yScaleValue
)}

Guilhem CARRON
committed
stroke={!isSelectedDate ? '#71612E' : '#e3b82a'}
fill={
isSelectedDate
? 'url(#diagonalHatchSelected)'
: 'url(#diagonalHatch)'
}
// className={isDuel ? 'bar-duel' : barClass}
onClick={!weekdays ? handleClick : () => undefined}
onAnimationEnd={onAnimationEnd}
/>
</g>
) : (
height > 0 &&
dataload.value &&
dataload.value >= 0 && (
<g transform={`translate(${xScaleValue}, ${yScaleValue})`}>
<defs>
<linearGradient
id="gradient"
className={barClass}
x1="0"
x2="0"
y1="0"
y2="1"
>
<stop id="stop-color-1" offset="0%" />
<stop id="stop-color-2" offset="100%" />
</linearGradient>
</defs>
<path
d={topRoundedRect(
compare ? getBandWidth() : 0,
0,
weekdays ? 3 : getBandWidth(),
height - yScaleValue
)}
fill="url(#gradient)"
className={isDuel ? 'bar-duel' : barClass}
onClick={!weekdays ? handleClick : () => undefined}
onAnimationEnd={onAnimationEnd}
/>
</g>
)
)}
{compare && compareDataload && compareDataload.value >= 0 && (
<g transform={`translate(${xScaleValue}, ${yScaleCompareValue})`}>
<defs>
<linearGradient
id="gradient-compare"
x1="0"
x2="0"
y1="0"
y2="1"
>
<stop id="stop-compare-color-1" offset="0%" />
<stop id="stop-compare-color-2" offset="100%" />
</linearGradient>
</defs>
<path
d={topRoundedRect(
0,
0,
getBandWidth(),
height - yScaleCompareValue
)}
fill="url(#gradient-compare)"
className={compareBarClass}
onClick={handleClick}
onAnimationEnd={onCompareAnimationEnd}
/>
</g>
)}