import * as React from 'react'; import * as _ from 'lodash'; import { DecorativeAxis, FlexibleXYPlot, HorizontalGridLines, LabelSeries, LineSeries, XAxis } from 'react-vis'; import BundleSizeChartTooltip from './BundleSizeChartTooltip'; import { PerfData } from './PerfDataContext'; export type BundleSizeChartProps = { perfData: PerfData }; const sampleToXAxis = sample => { return new Date(sample.ts).getTime(); }; const formatXAxis = val => { const date = new Date(val); return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`; }; /** * Draws a bundle size chart for all items in perfData.bundleSize. * Shows tooltip with details for selected build on mouse hover. * x-axis is a build number * y-axis is a bundle size */ const BundleSizeChart: React.FC = ({ perfData }) => { const availableCharts: string[] = perfData .reduce((acc, next) => { return Array.from(new Set([...acc, ...Object.keys(next.bundleSize)])); }, [] as string[]) .sort(); const [nearestX, setNearestX] = React.useState(); const sizeColor = '#555555'; const tagColor = '#888888'; const lineSeries = (key, data = 'size', props) => availableCharts.map((chartName, index) => ( { const y = _.get(sample, `bundleSize.${chartName}.${data}`); if (_.isUndefined(y)) { return undefined; } return { x: sampleToXAxis(sample), y, }; }), )} /> )); return ( { setNearestX(undefined); }} > {/* git tags */} {perfData .filter(sample => sample.tag) .map(sample => { const data = [ { x: sampleToXAxis(sample), y: 0 }, { x: sampleToXAxis(sample), y: 1000 }, ]; return ( ); })} sample.tag) .map(sample => ({ x: sampleToXAxis(sample), y: 0, label: sample.tag, style: { fontSize: 10, fill: tagColor, }, labelAnchorX: 'end', yOffset: 0, xOffset: 0, }))} /> {lineSeries('curve-size', 'size', { opacity: 0.8, stroke: sizeColor, strokeWidth: '2px', })} ({ x: sampleToXAxis(sample), y: 0, }))} onNearestX={(d: { x: number }) => { setNearestX(d.x); }} /> {nearestX && ( sampleToXAxis(sample) === nearestX)} /> )} ); }; export default BundleSizeChart;