insertClickableAreasIntoSeries.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import MarkArea from 'sentry/components/charts/components/markArea';
  2. import {LineChartSeries} from 'sentry/components/charts/lineChart';
  3. export function insertClickableAreasIntoSeries(series: LineChartSeries[], color: string) {
  4. const {data} = series[0];
  5. const startTime = data[0].name;
  6. const endTime = data[data.length - 1].name;
  7. const intervals = divideIntoIntervals(startTime as number, endTime as number, 40);
  8. const areaMarkData = intervals.map(([start, end]) => [
  9. {name: 'start', xAxis: start, emphasis: {disabled: false}},
  10. {name: 'end', xAxis: end, emphasis: {disabled: false}},
  11. ]);
  12. series.push({
  13. seriesName: 'Clickable Area',
  14. color,
  15. data: [],
  16. silent: false,
  17. emphasis: {disabled: false},
  18. markArea: MarkArea({
  19. silent: false,
  20. itemStyle: {
  21. color,
  22. opacity: 0,
  23. },
  24. label: {
  25. show: false,
  26. },
  27. emphasis: {disabled: false, itemStyle: {opacity: 0.2}},
  28. // I know this is gross but we don't have access to the types needed to satisfy the linter
  29. data: areaMarkData as any,
  30. }),
  31. });
  32. }
  33. function divideIntoIntervals(startTime: number, endTime: number, numSections: number) {
  34. const spaceBetweenIntervals = 0;
  35. const diff = endTime - startTime;
  36. const intervalLength = Math.round(diff / numSections);
  37. const intervals: number[][] = [];
  38. let currentTime = startTime;
  39. while (currentTime < endTime) {
  40. const currentInterval = [
  41. currentTime,
  42. currentTime + intervalLength - spaceBetweenIntervals,
  43. ];
  44. intervals.push(currentInterval);
  45. currentTime += intervalLength;
  46. }
  47. return intervals;
  48. }