lineChartWidgetVisualization.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import BaseChart from 'sentry/components/charts/baseChart';
  2. import LineSeries from 'sentry/components/charts/series/lineSeries';
  3. import {useChartZoom} from 'sentry/components/charts/useChartZoom';
  4. import type {Meta, TimeseriesData} from '../common/types';
  5. import {formatChartValue} from './formatChartValue';
  6. export interface LineChartWidgetVisualizationProps {
  7. timeseries: TimeseriesData[];
  8. meta?: Meta;
  9. utc?: boolean;
  10. }
  11. export function LineChartWidgetVisualization(props: LineChartWidgetVisualizationProps) {
  12. const {timeseries, meta} = props;
  13. const chartZoomProps = useChartZoom({
  14. saveOnZoom: true,
  15. });
  16. // TODO: Raise error if attempting to plot series of different types or units
  17. const firstSeriesField = timeseries[0]?.field;
  18. const type = meta?.fields?.[firstSeriesField] ?? 'number';
  19. const unit = meta?.units?.[firstSeriesField] ?? undefined;
  20. return (
  21. <BaseChart
  22. series={timeseries.map(timeserie => {
  23. return LineSeries({
  24. name: timeserie.field,
  25. color: timeserie.color,
  26. animation: false,
  27. data: timeserie.data.map(datum => {
  28. return [datum.timestamp, datum.value];
  29. }),
  30. });
  31. })}
  32. utc={props.utc}
  33. legend={{
  34. top: 0,
  35. left: 0,
  36. }}
  37. showTimeInTooltip
  38. tooltip={{
  39. valueFormatter: value => {
  40. return formatChartValue(value, type, unit);
  41. },
  42. }}
  43. yAxis={{
  44. axisLabel: {
  45. formatter(value: number) {
  46. return formatChartValue(value, type, unit);
  47. },
  48. },
  49. }}
  50. {...chartZoomProps}
  51. isGroupedByDate
  52. />
  53. );
  54. }