import * as d3 from 'd3'; import { CHART_WIDTH, HEALTHY_STATUS_COLORS, LINE_CHART_LARGE_HEIGHT, LINE_CHART_SMALL_HEIGHT, LINE_COLOR, LINE_LABEL_FONT_SIZE, LINE_LABEL_Y_PADDING, LINE_SMALL_LABEL_FONT_SIZE, LINE_WIDTH, } from './consts'; import { EHealthyStatus } from './Types'; const LineChartSmall = ({ data, format = d => d, unit = '', threshold, }: { data: number[]; format?: (d: any) => string; unit?: string; threshold: number; }) => { const length = data.length; const width = CHART_WIDTH; const height = LINE_CHART_SMALL_HEIGHT - 3; const fontSize = LINE_SMALL_LABEL_FONT_SIZE; const xDomain = [0, length]; const xRange = [0, CHART_WIDTH]; let maxData = d3.max(data, d => d) as number; maxData = maxData === 0 ? 1 : maxData; const yDomain = [0, maxData * 1.1]; const yRange = [height, 0]; const xScale = d3.scaleLinear(xDomain, xRange); const yScale = d3.scaleLinear(yDomain, yRange); const nodes = data .map((d, i) => (d >= 0 ? [xScale(i + 0.5), yScale(d)] : undefined)) .filter(a => a) as [number, number][]; const line = d3 .line() .curve(d3.curveBumpX) .x(d => d[0]) .y(d => d[1]); return ( {0} {format(maxData)} {unit && ( ({unit}) )} {maxData >= threshold && ( )} ); }; export default LineChartSmall;