All files / modules/10-common/components/SparklineChart SparklineChart.tsx

100% Statements 18/18
55.56% Branches 5/9
100% Functions 5/5
100% Lines 15/15

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              11x 11x 11x 11x 11x   11x                     11x     3x                                                                                         11x 3x   11x 3x 3x 3x 3x                    
/*
 * 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, { useMemo } from 'react'
import HighchartsReact from 'highcharts-react-official'
import Highcharts, { SeriesLineOptions } from 'highcharts'
import { merge, noop } from 'lodash-es'
import cx from 'classnames'
 
import css from './Sparkline.module.scss'
 
export interface SparklineChartProps {
  title?: string
  data: SeriesLineOptions['data']
  options?: Highcharts.Options
  sparklineChartContainerStyles?: string
  titleStyles?: string
  onClick?: () => void
}
 
const getSparklineDefaultOptions = (
  data: SparklineChartProps['data'],
  isTitlePresent: boolean
): Highcharts.Options => ({
  title: {
    text: ''
  },
  credits: {
    enabled: false
  },
  xAxis: {
    visible: false,
    startOnTick: false,
    endOnTick: false
  },
  yAxis: {
    visible: false,
    startOnTick: false,
    endOnTick: false
  },
  legend: {
    enabled: false
  },
  series: [
    {
      name: '',
      type: 'line',
      data
    }
  ],
  tooltip: {
    enabled: false
  },
  plotOptions: {
    series: {
      animation: false,
      lineWidth: 1.5,
      color: 'var(--primary-6)',
      shadow: false,
      marker: {
        enabled: false
      },
      enableMouseTracking: false
    }
  },
  chart: { backgroundColor: '', width: 100, height: 54, margin: [isTitlePresent ? 24 : 10, 6, 10, 6] }
})
 
const getParsedOptions = (defaultOptions: Highcharts.Options, options: Highcharts.Options): Highcharts.Options =>
  merge(defaultOptions, options)
 
export const SparklineChart: React.FC<SparklineChartProps> = props => {
  const { title, data, options = {}, sparklineChartContainerStyles = '', titleStyles = '', onClick } = props
  const defaultOptions = useMemo(() => getSparklineDefaultOptions(data, !!title), [data, title])
  const parsedOptions = useMemo(() => getParsedOptions(defaultOptions, options), [defaultOptions, options])
  return (
    <div
      className={cx(css.sparklineChartContainer, sparklineChartContainerStyles, { [css.hover]: onClick })}
      onClick={onClick || noop}
    >
      {title ? <div className={cx(css.title, titleStyles)}>{title}</div> : <></>}
      <HighchartsReact highcharts={Highcharts} options={parsedOptions} />
    </div>
  )
}