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'
import { Client } from 'cozy-client'
import { UsageEventType } from 'enum/usageEvent.enum'
import UsageEventService from 'services/usageEvent.service'
showCompare: boolean
xScale: ScaleBand<string>
yScale: ScaleLinear<number, number>
height: number
const Bar = ({
index,
dataload,
compareDataload,
showCompare,
xScale,
yScale,
height,
isSwitching,
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 handleClick = async () => {
await UsageEventService.addEvent(client, {
type: UsageEventType.CONSUMPTION_INTERACT_EVENT,
context: FluidType[fluidType] + ' / ' + TimeStep[timeStep],
})
}
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 barClass = clicked
? `bar-${fluidStyle} selected bounce-${
browser && browser.name !== 'edge' ? '2' : '3'
} delay`
: isSelectedDate
? animationEnded
? `bar-${fluidStyle} selected`
: `bar-${fluidStyle} selected bounce-${
browser && browser.name !== 'edge' ? '1' : '3'
} delay--${index % 13}`
: animationEnded
? `bar-${fluidStyle} `
: `bar-${fluidStyle} 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)`}>
onClick={handleClick}
x="0"
y="0"
width={showCompare ? getBandWidth() * 2 : getBandWidth()}
height={height + 40}
className={`background-${barBackgroundClass}`}
/>
</g>
) : null}
{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,
getBandWidth(),
onClick={handleClick}
onAnimationEnd={onAnimationEnd}
/>
</g>
) : null}
{showCompare &&
compareDataload &&
compareDataload.value &&
dataload.value >= 0 &&
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
)}