Skip to content
Snippets Groups Projects
BarChart.tsx 3.43 KiB
Newer Older
  • Learn to ignore specific revisions
  • Bastien DUMONT's avatar
    Bastien DUMONT committed
    import AxisBottom from 'components/Charts/AxisBottom'
    import AxisRight from 'components/Charts/AxisRight'
    import Bar from 'components/Charts/Bar'
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    import { scaleBand, ScaleBand, scaleLinear, ScaleLinear } from 'd3-scale'
    
    import { DataloadState, FluidType, TimeStep } from 'enums'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import { DateTime } from 'luxon'
    
    import { Datachart } from 'models'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import React from 'react'
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    
    
    interface BarChartProps {
    
    Yoan VALLET's avatar
    Yoan VALLET committed
      chartData: Datachart
    
      fluidType: FluidType
    
      timeStep: TimeStep
    
      /** showCompare should be false for analysis view */
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
      width?: number
      height?: number
      marginLeft?: number
      marginRight?: number
      marginTop?: number
      marginBottom?: number
    
      isSwitching: boolean
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    }
    
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    const BarChart = ({
    
      chartData,
    
      fluidType,
    
      timeStep,
    
      width = 600,
      height = 400,
      marginLeft = 10,
    
      marginRight = 55,
    
      marginTop = 20,
      marginBottom = 50,
      isSwitching,
    
    }: BarChartProps) => {
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
      const getContentWidth = () => {
        return width - marginLeft - marginRight
      }
    
      const getContentHeight = () => {
        return height - marginTop - marginBottom
      }
    
      const getMaxLoad = () => {
    
    CREY Romain's avatar
    CREY Romain committed
        const maxCompare = chartData.comparisonData
          ? Math.max(...chartData.comparisonData.map(d => d.value))
          : 0
    
          ? Math.max(...chartData.actualData.map(d => d.value))
          : 0
    
    
        if (showCompare) {
          if (max <= 0 && maxCompare <= 0) return 15
          else return Math.max(max, maxCompare)
        } else {
          if (max <= 0) {
            return 15
          } else return max
        }
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
      }
    
      const xScale: ScaleBand<string> = scaleBand()
        .domain(
          chartData.actualData.map(d =>
            d.date.toLocaleString(DateTime.DATETIME_SHORT)
          )
        )
        .range([0, getContentWidth()])
        .padding(0.2)
    
      const yScale: ScaleLinear<number, number> = scaleLinear()
        .domain([0, getMaxLoad()])
        .range([getContentHeight(), 0])
    
      const sum = chartData.actualData.reduce((a, b) => a + b.value, 0)
      const averageConsumption = sum / chartData.actualData.length
    
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
      return (
        <svg width={width} height={height}>
    
          <AxisRight
    
            fluidType={fluidType}
    
            yScale={yScale}
            width={width}
            marginRight={marginRight}
            marginTop={marginTop}
          />
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
          <g transform={`translate(${marginLeft},${marginTop})`}>
    
            {chartData.actualData.map((data, index) => (
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
              <Bar
                key={index}
                index={index}
    
                dataload={data}
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
                compareDataload={
    
                  chartData.comparisonData?.[index]
    
                    ? chartData.comparisonData[index]
                    : null
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
                }
    
                fluidType={fluidType}
    
                timeStep={timeStep}
    
                compare={showCompare}
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
                xScale={xScale}
                yScale={yScale}
                height={getContentHeight()}
    
                isSwitching={isSwitching}
    
                  data.state === DataloadState.AGGREGATED_WITH_EMPTY ||
                  data.state === DataloadState.AGGREGATED_WITH_COMING ||
                  data.state === DataloadState.AGGREGATED_WITH_HOLE_OR_MISSING
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
              />
            ))}
          </g>
          <AxisBottom
            data={chartData.actualData}
    
            timeStep={timeStep}
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
            xScale={xScale}
            height={height}
            marginLeft={marginLeft}
            marginBottom={marginBottom}
          />
        </svg>
      )
    }
    
    export default BarChart