durationChart.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. // 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. let {seriesIndex} = batch;
  32. const {dataIndex} = batch;
  33. // 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.
  34. seriesIndex = seriesIndex % allSeries.length;
  35. const highlightedSeries = allSeries?.[seriesIndex];
  36. const highlightedDataPoint = highlightedSeries?.data?.[dataIndex];
  37. return {series: highlightedSeries, dataPoint: highlightedDataPoint};
  38. });
  39. onHighlight?.(highlightedDataPoints, event);
  40. };
  41. return (
  42. <ChartPanel title={getDurationChartTitle('http')}>
  43. <Chart
  44. height={CHART_HEIGHT}
  45. grid={{
  46. left: '0',
  47. right: '0',
  48. top: '8px',
  49. bottom: '0',
  50. }}
  51. data={series}
  52. onHighlight={handleChartHighlight}
  53. scatterPlot={scatterPlot}
  54. loading={isLoading}
  55. error={error}
  56. chartColors={[AVG_COLOR]}
  57. type={ChartType.LINE}
  58. aggregateOutputFormat="duration"
  59. />
  60. </ChartPanel>
  61. );
  62. }