import React, { FC } from 'react'; import { UncontrolledTooltip } from 'reactstrap'; import { Histogram } from '../../types/types'; import { bucketRangeString } from './DataTable'; type ScaleType = 'linear' | 'exponential'; const HistogramChart: FC<{ histogram: Histogram; index: number; scale: ScaleType }> = ({ index, histogram, scale }) => { const { buckets } = histogram; const rangeMax = buckets ? parseFloat(buckets[buckets.length - 1][2]) : 0; const countMax = buckets ? buckets.map((b) => parseFloat(b[3])).reduce((a, b) => Math.max(a, b)) : 0; const formatter = Intl.NumberFormat('en', { notation: 'compact' }); const positiveBuckets = buckets?.filter((b) => parseFloat(b[1]) >= 0); // we only want to show buckets with range >= 0 const xLabelTicks = scale === 'linear' ? [0.25, 0.5, 0.75, 1] : [1]; return (
{[1, 0.75, 0.5, 0.25].map((i) => (
{formatter.format(countMax * i)}
))}
0
{[0, 0.25, 0.5, 0.75, 1].map((i) => (
))} {positiveBuckets?.map((b, bIdx) => { const bucketIdx = `bucket-${index}-${bIdx}-${Math.ceil(parseFloat(b[3]) * 100)}`; const bucketLeft = scale === 'linear' ? (parseFloat(b[1]) / rangeMax) * 100 + '%' : (bIdx / positiveBuckets.length) * 100 + '%'; const bucketWidth = scale === 'linear' ? ((parseFloat(b[2]) - parseFloat(b[1])) / rangeMax) * 100 + '%' : 100 / positiveBuckets.length + '%'; return (
range: {bucketRangeString(b)}
count: {b[3]}
); })}
0
{xLabelTicks.map((i) => (
{formatter.format(rangeMax * i)}
))}
); }; export default HistogramChart;