All files / modules/75-ce/components/BudgetDetailsChart BudgetDetailsChart.tsx

92.11% Statements 35/38
77.78% Branches 14/18
77.78% Functions 7/9
91.43% Lines 32/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144              1x   1x 1x 1x   1x 1x           1x         12x 12x   1x   1x 12x 1x   11x     1x 12x 1x   11x     1x           1x               1x 1x             1x             1x               1x 1x   1x       1x             12x   1x   1x                                                                                     1x  
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React from 'react'
import type { SeriesColumnOptions, SeriesLineOptions } from 'highcharts/highcharts'
import { Container } from '@wings-software/uicore'
import { useStrings } from 'framework/strings'
import { CCM_CHART_TYPES } from '@ce/constants'
import type { BudgetData, BudgetCostData, BudgetPeriod } from 'services/ce/services'
import CEChart from '../CEChart/CEChart'
import { computeCategories } from './budgetCategoryUtil'
 
const getChartSeriesData: (
  chartData: BudgetCostData[],
  forecastedCost: number,
  chartLabels: Record<string, string>
) => (SeriesColumnOptions | SeriesLineOptions)[] = (
  chartData: BudgetCostData[],
  forecastedCost: number,
  chartLabels
) => {
  const columnChartData = chartData.map(item => [item.actualCost])
  const lineChartData = chartData.map(item => [item.budgeted])
 
  const lastChartEntry = chartData[chartData.length - 1] as any
 
  const lastMonthBudget = chartData.map((_, idx) => {
    if (idx === chartData.length - 1) {
      return [forecastedCost]
    }
    return 0
  })
 
  const lastMonthActual = chartData.map((_, idx) => {
    if (idx === chartData.length - 1) {
      return [lastChartEntry.actualCost]
    }
    return 0
  })
 
  const lastMonthBudgetChartSeries: SeriesColumnOptions = {
    name: chartLabels.FORECAST_COST,
    type: CCM_CHART_TYPES.COLUMN,
    data: lastMonthBudget,
    color: 'var(--teal-50)'
  }
  const lastMonthActualChartSeries: SeriesColumnOptions = {
    name: chartLabels.CURRENT_MONTH_COST,
    type: CCM_CHART_TYPES.COLUMN,
    data: lastMonthActual,
    color: 'var(--teal-400)'
  }
 
  // remove last element because its handeled in lastMonthChartData
  columnChartData.pop()
  const columnChartSeries: SeriesColumnOptions = {
    name: chartLabels.ACTUAL_COST,
    type: CCM_CHART_TYPES.COLUMN,
    data: columnChartData,
    color: 'var(--primary-8)'
  }
 
  const lineChartSeries: SeriesLineOptions = {
    name: chartLabels.BUDGETED_COST,
    type: CCM_CHART_TYPES.LINE,
    data: lineChartData,
    color: 'var(--yellow-900)'
  }
 
  return [lastMonthBudgetChartSeries, lastMonthActualChartSeries, columnChartSeries, lineChartSeries]
}
 
interface BudgetDetailsChartProps {
  chartData: BudgetData
  budgetPeriod: BudgetPeriod
}
 
const BudgetDetailsChart: (props: BudgetDetailsChartProps) => JSX.Element | null = ({ chartData, budgetPeriod }) => {
  const { getString } = useStrings()
 
  Iif (!chartData?.costData?.length) {
    return null
  }
 
  const chartLabels: Record<string, string> = {
    ACTUAL_COST: getString('ce.budgets.detailsPage.chartNames.actualCost'),
    BUDGETED_COST: getString('ce.budgets.detailsPage.chartNames.budgetedCost'),
    FORECAST_COST: getString('ce.budgets.detailsPage.chartNames.forecastCost'),
    CURRENT_MONTH_COST: getString('ce.budgets.detailsPage.chartNames.currentMonthCost')
  }
 
  const filteredData = chartData.costData.filter(e => e?.time) as BudgetCostData[]
 
  const chartSeriesData = getChartSeriesData(filteredData, chartData.forecastCost, chartLabels)
 
  return (
    <Container>
      <CEChart
        options={{
          series: chartSeriesData,
          plotOptions: {
            column: {
              borderColor: undefined,
              groupPadding: 0.2,
              grouping: false
            }
          },
          yAxis: {
            tickAmount: 3,
            title: {
              text: ''
            },
            labels: {
              step: 2,
              formatter: function () {
                return `$ ${this['value']}`
              }
            }
          },
          xAxis: {
            categories: computeCategories(filteredData, budgetPeriod),
            ordinal: true,
            labels: {
              formatter: function () {
                return `${this.value}`
              }
            }
          },
          legend: {
            align: 'right',
            layout: 'horizontal'
          }
        }}
      />
    </Container>
  )
}
 
export default BudgetDetailsChart