profilingSparklineChart.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import {useMemo} from 'react';
  2. import {LineChart, LineChartProps} from 'sentry/components/charts/lineChart';
  3. import {makeFormatter} from 'sentry/utils/profiling/units/units';
  4. const durationFormatter = makeFormatter('nanoseconds', 0);
  5. function asSeries(seriesName: string, data: {timestamp: number; value: number}[]) {
  6. return {
  7. data: data.map(p => ({
  8. name: p.timestamp * 1e3,
  9. value: p.value ?? 0,
  10. })),
  11. seriesName,
  12. };
  13. }
  14. interface ProfilingSparklineChartProps {
  15. name: string;
  16. points: {timestamp: number; value: number}[];
  17. chartProps?: Partial<LineChartProps>;
  18. }
  19. export function ProfilingSparklineChart(props: ProfilingSparklineChartProps) {
  20. const chartProps: LineChartProps = useMemo(() => {
  21. const baseProps: LineChartProps = {
  22. height: 26,
  23. width: 'auto',
  24. series: [asSeries(props.name, props.points)],
  25. grid: [
  26. {
  27. containLabel: false,
  28. top: '2px',
  29. left: '2px',
  30. right: '2px',
  31. bottom: '2px',
  32. },
  33. {
  34. containLabel: false,
  35. top: '2px',
  36. left: '2px',
  37. right: '2px',
  38. bottom: '2px',
  39. },
  40. ],
  41. tooltip: {
  42. valueFormatter: (v: number) => durationFormatter(v),
  43. },
  44. axisPointer: {},
  45. xAxes: [
  46. {
  47. gridIndex: 0,
  48. type: 'time' as const,
  49. show: false,
  50. },
  51. ],
  52. yAxes: [
  53. {
  54. scale: true,
  55. show: false,
  56. },
  57. ],
  58. ...(props.chartProps ? props.chartProps : {}),
  59. };
  60. return baseProps;
  61. }, [props.points, props.name, props.chartProps]);
  62. return <LineChart {...chartProps} isGroupedByDate showTimeInTooltip />;
  63. }