durationChart.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import type {ComponentProps} from 'react';
  2. import type {EChartHighlightHandler, Series} from 'sentry/types/echarts';
  3. import {CHART_HEIGHT} from 'sentry/views/performance/http/settings';
  4. import {AVG_COLOR} from 'sentry/views/starfish/colors';
  5. import Chart, {ChartType} from 'sentry/views/starfish/components/chart';
  6. import ChartPanel from 'sentry/views/starfish/components/chartPanel';
  7. import {getDurationChartTitle} from 'sentry/views/starfish/views/spans/types';
  8. interface Props {
  9. isLoading: boolean;
  10. series: Series[];
  11. error?: Error | null;
  12. onHighlight?: (highlights: Highlight[], event: Event) => void; // TODO: Correctly type this
  13. scatterPlot?: ComponentProps<typeof Chart>['scatterPlot'];
  14. }
  15. interface Highlight {
  16. dataPoint: Series['data'][number];
  17. series: Series[];
  18. }
  19. export function DurationChart({
  20. series,
  21. scatterPlot,
  22. isLoading,
  23. error,
  24. onHighlight,
  25. }: Props) {
  26. // TODO: This is duplicated from `DurationChart` in `SampleList`. Resolve the duplication
  27. const handleChartHighlight: EChartHighlightHandler = function (event) {
  28. // TODO: Gross hack. Even though `scatterPlot` is a separate prop, it's just an array of `Series` that gets appended to the main series. To find the point that was hovered, we re-construct the correct series order. It would have been cleaner to just pass the scatter plot as its own, single series
  29. const allSeries = [...series, ...(scatterPlot ?? [])];
  30. const highlightedDataPoints = event.batch.map(batch => {
  31. const {seriesIndex, dataIndex} = batch;
  32. const highlightedSeries = allSeries?.[seriesIndex];
  33. const highlightedDataPoint = highlightedSeries.data?.[dataIndex];
  34. return {series: highlightedSeries, dataPoint: highlightedDataPoint};
  35. });
  36. onHighlight?.(highlightedDataPoints, event);
  37. };
  38. return (
  39. <ChartPanel title={getDurationChartTitle('http')}>
  40. <Chart
  41. height={CHART_HEIGHT}
  42. grid={{
  43. left: '0',
  44. right: '0',
  45. top: '8px',
  46. bottom: '0',
  47. }}
  48. data={series}
  49. onHighlight={handleChartHighlight}
  50. scatterPlot={scatterPlot}
  51. loading={isLoading}
  52. error={error}
  53. chartColors={[AVG_COLOR]}
  54. type={ChartType.LINE}
  55. aggregateOutputFormat="duration"
  56. />
  57. </ChartPanel>
  58. );
  59. }