Newer
Older
import { useDispatch, useSelector } from 'react-redux'
import { AppStore } from 'store'
import { ScaleBand, ScaleLinear } from 'd3-scale'
import { DateTime } from 'luxon'
import { TimeStep } from 'enum/timeStep.enum'
import { Dataload } from 'models'
import DateChartService from 'services/dateChart.service'
import {
setCurrentDatachartIndex,
setSelectedDate,
} from 'store/chart/chart.actions'
showCompare: boolean
xScale: ScaleBand<string>
yScale: ScaleLinear<number, number>
height: number
isMultiMissingFluid?: boolean
weekdays?: 'week' | 'weekend'
const Bar = ({
index,
dataload,
compareDataload,
showCompare,
xScale,
yScale,
height,
isSwitching,
isMultiMissingFluid,
weekdays,
const { selectedDate } = useSelector((state: AppStore) => 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]
}
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
)
? `bar-${fluidStyle} ${weekdays} selected bounce-${
browser && browser.name !== 'edge' ? '2' : '3'
} delay`
: isSelectedDate
? animationEnded
? `bar-${fluidStyle} ${weekdays} selected`
: `bar-${fluidStyle} ${weekdays} selected bounce-${
browser && browser.name !== 'edge' ? '1' : '3'
} delay--${index % 13}`
: animationEnded
? `bar-${fluidStyle} ${weekdays}`
: `bar-${fluidStyle} ${weekdays} bounce-${
browser && browser.name !== 'edge' ? '1' : '3'
} delay--${index % 13}`
const compareBarClass = clicked
? `bar-compare-${fluidStyle} selected bounce-${
browser && browser.name !== 'edge' ? '2' : '3'
} delay--0`
: isSelectedDate
? compareAnimationEnded
? `bar-compare-${fluidStyle} selected`
: `bar-compare-${fluidStyle} selected bounce-${
browser && browser.name !== 'edge' ? '1' : '3'
} delay--${index % 13}`
: compareAnimationEnded
? `bar-compare-${fluidStyle} `
: `bar-compare-${fluidStyle} bounce-${
browser && browser.name !== 'edge' ? '1' : '3'
} delay--${index % 13}`
const getBandWidth = (): number => {
return showCompare ? xScale.bandwidth() / 2 : xScale.bandwidth()
}
const topRoundedRect = (
_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' +
dispatch(setCurrentDatachartIndex(index))
<g
transform={`translate(${xScaleValue}, -40)`}
className="barContainer"
>

Guilhem CARRON
committed
onClick={!weekdays ? handleClick : () => {}}
x="0"
y="0"
width={showCompare ? getBandWidth() * 2 : getBandWidth()}
height={height + 40}
className={`background-${barBackgroundClass}`}
{height > 0 &&
dataload.value &&
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(
showCompare ? getBandWidth() : 0,
0,
getBandWidth(),
height - yScaleValue
)}

Guilhem CARRON
committed
stroke={!isSelectedDate ? '#71612E' : '#e3b82a'}
fill={
isSelectedDate
? 'url(#diagonalHatchSelected)'
: 'url(#diagonalHatch)'
}
// className={isDuel ? 'bar-duel' : barClass}

Guilhem CARRON
committed
onClick={!weekdays ? handleClick : () => {}}
onAnimationEnd={onAnimationEnd}
/>
</g>
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
) : (
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(
showCompare ? getBandWidth() : 0,
0,
weekdays ? 3 : getBandWidth(),
height - yScaleValue
)}
fill="url(#gradient)"
className={isDuel ? 'bar-duel' : barClass}
onClick={!weekdays ? handleClick : () => {}}
onAnimationEnd={onAnimationEnd}
/>
</g>
)
)}
compareDataload &&
compareDataload.value &&
compareDataload.value >= 0 && (
<g transform={`translate(${xScaleValue}, ${yScaleCompareValue})`}>
<defs>
<linearGradient
id="gradient-compare"
className={compareBarClass}
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)"
onClick={handleClick}
onAnimationEnd={onCompareAnimationEnd}
/>
</g>
)}