durationChart.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import type {ComponentProps} from 'react';
  2. import type {EChartHighlightHandler, Series} from 'sentry/types/echarts';
  3. import {AVG_COLOR} from 'sentry/views/insights/colors';
  4. import Chart, {ChartType} from 'sentry/views/insights/common/components/chart';
  5. import ChartPanel from 'sentry/views/insights/common/components/chartPanel';
  6. import {getDurationChartTitle} from 'sentry/views/insights/common/views/spans/types';
  7. import {CHART_HEIGHT} from 'sentry/views/insights/http/settings';
  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. // ignore mouse hovering over the chart legend
  29. if (!event.batch) return;
  30. // 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
  31. const allSeries = [...series, ...(scatterPlot ?? [])];
  32. const highlightedDataPoints = event.batch.map(batch => {
  33. let {seriesIndex} = batch;
  34. const {dataIndex} = batch;
  35. // TODO: More hacks. The Chart component partitions the data series into a complete and incomplete series. Wrap the series index to work around overflowing index.
  36. seriesIndex = seriesIndex % allSeries.length;
  37. const highlightedSeries = allSeries?.[seriesIndex];
  38. const highlightedDataPoint = highlightedSeries?.data?.[dataIndex];
  39. return {series: highlightedSeries, dataPoint: highlightedDataPoint};
  40. });
  41. onHighlight?.(highlightedDataPoints, event);
  42. };
  43. return (
  44. <ChartPanel title={getDurationChartTitle('http')}>
  45. <Chart
  46. height={CHART_HEIGHT}
  47. grid={{
  48. left: '0',
  49. right: '0',
  50. top: '8px',
  51. bottom: '0',
  52. }}
  53. data={series}
  54. onHighlight={handleChartHighlight}
  55. scatterPlot={scatterPlot}
  56. loading={isLoading}
  57. error={error}
  58. chartColors={[AVG_COLOR]}
  59. type={ChartType.LINE}
  60. aggregateOutputFormat="duration"
  61. />
  62. </ChartPanel>
  63. );
  64. }