|
| 1 | +import * as d3 from 'd3' |
| 2 | +import React, { useEffect, useRef } from 'react' |
| 3 | +import cx from 'classnames' |
| 4 | +import { curryRight, flow, toNumber } from 'lodash' |
| 5 | + |
| 6 | +import { formatBytes, toBytes } from 'uiSrc/utils' |
| 7 | +import styles from './styles.module.scss' |
| 8 | + |
| 9 | +export interface BarChartData { |
| 10 | + y: number |
| 11 | + x: number |
| 12 | + xlabel: string |
| 13 | + ylabel: string |
| 14 | +} |
| 15 | + |
| 16 | +interface IDatum extends BarChartData{ |
| 17 | + index: number |
| 18 | +} |
| 19 | + |
| 20 | +export enum BarChartDataType { |
| 21 | + Bytes = 'bytes' |
| 22 | +} |
| 23 | + |
| 24 | +interface IProps { |
| 25 | + name?: string |
| 26 | + data?: BarChartData[] |
| 27 | + dataType?: BarChartDataType |
| 28 | + barWidth?: number |
| 29 | + minBarHeight?: number |
| 30 | + width?: number |
| 31 | + height?: number |
| 32 | + yCountTicks?: number |
| 33 | + divideLastColumn?: boolean |
| 34 | + multiplierGrid?: number |
| 35 | + classNames?: { |
| 36 | + bar?: string |
| 37 | + dashedLine?: string |
| 38 | + tooltip?: string |
| 39 | + scatterPoints?: string |
| 40 | + } |
| 41 | + tooltipValidation?: (val: any, index: number) => string |
| 42 | + leftAxiosValidation?: (val: any, index: number) => any |
| 43 | + bottomAxiosValidation?: (val: any, index: number) => any |
| 44 | +} |
| 45 | + |
| 46 | +export const DEFAULT_MULTIPLIER_GRID = 5 |
| 47 | +export const DEFAULT_Y_TICKS = 8 |
| 48 | +export const DEFAULT_BAR_WIDTH = 40 |
| 49 | +export const MIN_BAR_HEIGHT = 3 |
| 50 | +let cleanedData: IDatum[] = [] |
| 51 | + |
| 52 | +const BarChart = (props: IProps) => { |
| 53 | + const { |
| 54 | + data = [], |
| 55 | + name, |
| 56 | + width: propWidth = 0, |
| 57 | + height: propHeight = 0, |
| 58 | + barWidth = DEFAULT_BAR_WIDTH, |
| 59 | + yCountTicks = DEFAULT_Y_TICKS, |
| 60 | + minBarHeight = MIN_BAR_HEIGHT, |
| 61 | + dataType, |
| 62 | + classNames, |
| 63 | + divideLastColumn, |
| 64 | + multiplierGrid = DEFAULT_MULTIPLIER_GRID, |
| 65 | + tooltipValidation = (val) => val, |
| 66 | + leftAxiosValidation = (val) => val, |
| 67 | + bottomAxiosValidation = (val) => val, |
| 68 | + } = props |
| 69 | + |
| 70 | + const margin = { top: 10, right: 0, bottom: 32, left: 60 } |
| 71 | + const width = propWidth - margin.left - margin.right |
| 72 | + const height = propHeight - margin.top - margin.bottom |
| 73 | + |
| 74 | + const svgRef = useRef<SVGSVGElement>(null) |
| 75 | + |
| 76 | + const getRoundedYMaxValue = (number: number): number => { |
| 77 | + const numLen = number.toString().length |
| 78 | + const dividerValue = toNumber(`1${'0'.repeat(numLen - 1)}`) |
| 79 | + |
| 80 | + return Math.ceil(number / dividerValue) * dividerValue |
| 81 | + } |
| 82 | + |
| 83 | + useEffect(() => { |
| 84 | + if (data.length === 0) { |
| 85 | + return undefined |
| 86 | + } |
| 87 | + |
| 88 | + const tooltip = d3.select('body').append('div') |
| 89 | + .attr('class', cx(styles.tooltip, classNames?.tooltip || '')) |
| 90 | + .style('opacity', 0) |
| 91 | + |
| 92 | + d3 |
| 93 | + .select(svgRef.current) |
| 94 | + .select('g') |
| 95 | + .remove() |
| 96 | + |
| 97 | + // append the svg object to the body of the page |
| 98 | + const svg = d3.select(svgRef.current) |
| 99 | + .attr('data-testid', `bar-${name}`) |
| 100 | + .attr('width', width + margin.left + margin.right) |
| 101 | + .attr('height', height + margin.top + margin.bottom + 30) |
| 102 | + .append('g') |
| 103 | + .attr('transform', |
| 104 | + `translate(${margin.left},${margin.top})`) |
| 105 | + |
| 106 | + const tempData = [...data] |
| 107 | + |
| 108 | + tempData.push({ x: 0, y: 0, xlabel: '', ylabel: '', }) |
| 109 | + cleanedData = tempData.map((datum, index) => ({ |
| 110 | + index, |
| 111 | + xlabel: `${datum?.xlabel || ''}`, |
| 112 | + ylabel: `${datum?.ylabel || ''}`, |
| 113 | + y: datum.y || 0, |
| 114 | + x: datum.x || 0, |
| 115 | + })) |
| 116 | + |
| 117 | + // Add X axis |
| 118 | + const xAxis = d3.scaleLinear() |
| 119 | + .domain(d3.extent(cleanedData, (d) => d.index) as [number, number]) |
| 120 | + .range([0, width]) |
| 121 | + |
| 122 | + let maxY = d3.max(cleanedData, (d) => d.y) || yCountTicks |
| 123 | + |
| 124 | + if (dataType === BarChartDataType.Bytes) { |
| 125 | + const curriedTyBytes = curryRight(toBytes) |
| 126 | + const [maxYFormatted, type] = formatBytes(maxY, 1, true) |
| 127 | + |
| 128 | + maxY = flow( |
| 129 | + toNumber, |
| 130 | + Math.ceil, |
| 131 | + getRoundedYMaxValue, |
| 132 | + curriedTyBytes(`${type}`) |
| 133 | + )(maxYFormatted) |
| 134 | + } |
| 135 | + |
| 136 | + // Add Y axis |
| 137 | + const yAxis = d3.scaleLinear() |
| 138 | + .domain([0, maxY || 0]) |
| 139 | + .range([height, 0]) |
| 140 | + |
| 141 | + // divider for last column |
| 142 | + if (divideLastColumn) { |
| 143 | + svg.append('line') |
| 144 | + .attr('class', cx(styles.dashedLine, classNames?.dashedLine)) |
| 145 | + .attr('x1', xAxis(cleanedData.length - 2.3)) |
| 146 | + .attr('x2', xAxis(cleanedData.length - 2.3)) |
| 147 | + .attr('y1', 0) |
| 148 | + .attr('y2', height) |
| 149 | + } |
| 150 | + |
| 151 | + // squared background for Y axis |
| 152 | + svg.append('g') |
| 153 | + .call( |
| 154 | + d3.axisLeft(yAxis) |
| 155 | + .tickSize(-width + ((2 * width) / ((cleanedData.length) * multiplierGrid)) + 6) |
| 156 | + .tickValues([...d3.range(0, maxY, maxY / yCountTicks), maxY]) |
| 157 | + .tickFormat((d, i) => leftAxiosValidation(d, i)) |
| 158 | + .ticks(cleanedData.length * multiplierGrid) |
| 159 | + .tickPadding(10) |
| 160 | + ) |
| 161 | + |
| 162 | + const yTicks = d3.selectAll('.tick') |
| 163 | + yTicks.attr('data-testid', (d, i) => `ytick-${d}-${i}`) |
| 164 | + |
| 165 | + // squared background for X axis |
| 166 | + svg.append('g') |
| 167 | + .attr('transform', `translate(0,${height})`) |
| 168 | + .call( |
| 169 | + d3.axisBottom(xAxis) |
| 170 | + .ticks(cleanedData.length * multiplierGrid) |
| 171 | + .tickFormat((d, i) => bottomAxiosValidation(d, i)) |
| 172 | + .tickSize(-height) |
| 173 | + .tickPadding(22) |
| 174 | + ) |
| 175 | + |
| 176 | + // TODO: hide last 2 columns of background grid |
| 177 | + const allTicks = d3.selectAll('.tick') |
| 178 | + allTicks.attr('opacity', (_a, i) => |
| 179 | + (i === allTicks.size() - 1 || i === allTicks.size() - 2 ? 0 : 1)) |
| 180 | + |
| 181 | + // moving X axios labels under the center of Bar |
| 182 | + svg.selectAll('text') |
| 183 | + .attr('x', barWidth / 2) |
| 184 | + |
| 185 | + // roll back all changes for Y axios labels |
| 186 | + yTicks.attr('opacity', '1') |
| 187 | + yTicks.selectAll('text') |
| 188 | + .attr('x', -10) |
| 189 | + |
| 190 | + // bars |
| 191 | + svg |
| 192 | + .selectAll('.bar') |
| 193 | + .data(cleanedData) |
| 194 | + .enter() |
| 195 | + .append('rect') |
| 196 | + .attr('class', cx(styles.bar, classNames?.bar)) |
| 197 | + .attr('x', (d) => xAxis(d.index)) |
| 198 | + .attr('width', barWidth) |
| 199 | + // set minimal height for Bar |
| 200 | + .attr('y', (d) => (d.y && height - yAxis(d.y) < minBarHeight ? height - minBarHeight : yAxis(d.y))) |
| 201 | + .attr('height', (d) => { |
| 202 | + const initialHeight = height - yAxis(d.y) |
| 203 | + return initialHeight && initialHeight < minBarHeight ? minBarHeight : initialHeight |
| 204 | + }) |
| 205 | + .attr('data-testid', (d) => `bar-${d.x}-${d.y}`) |
| 206 | + .on('mouseenter mousemove', (event, d) => { |
| 207 | + tooltip |
| 208 | + .style('opacity', 1) |
| 209 | + tooltip.html(tooltipValidation(d.y, d.index)) |
| 210 | + .style('left', `${event.pageX + 16}px`) |
| 211 | + .style('top', `${event.pageY + 16}px`) |
| 212 | + .attr('data-testid', 'bar-tooltip') |
| 213 | + }) |
| 214 | + .on('mouseleave', () => { |
| 215 | + tooltip |
| 216 | + .style('opacity', 0) |
| 217 | + }) |
| 218 | + |
| 219 | + return () => { |
| 220 | + tooltip.remove() |
| 221 | + } |
| 222 | + }, [data, width, height]) |
| 223 | + |
| 224 | + if (!data.length) { |
| 225 | + return null |
| 226 | + } |
| 227 | + |
| 228 | + return ( |
| 229 | + <div className={styles.wrapper} style={{ width: propWidth, height: propHeight }}> |
| 230 | + <svg ref={svgRef} className={styles.svg} /> |
| 231 | + </div> |
| 232 | + ) |
| 233 | +} |
| 234 | + |
| 235 | +export default BarChart |
0 commit comments