miniBarChart.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Import to ensure echarts components are loaded.
  2. import './components/markPoint';
  3. import React from 'react';
  4. import set from 'lodash/set';
  5. import {getFormattedDate} from 'app/utils/dates';
  6. import theme from 'app/utils/theme';
  7. import BarChart, {BarChartSeries} from './barChart';
  8. import BaseChart from './baseChart';
  9. import {truncationFormatter} from './utils';
  10. type Marker = {
  11. name: string;
  12. value: string | number | Date;
  13. color: string;
  14. symbolSize?: number;
  15. };
  16. const defaultProps = {
  17. /**
  18. * Colors to use on the chart.
  19. */
  20. colors: [theme.gray200, theme.purple300, theme.purple300] as string[],
  21. /**
  22. * Show max/min values on yAxis
  23. */
  24. labelYAxisExtents: false,
  25. /**
  26. * Whether not the series should be stacked.
  27. *
  28. * Some of our stats endpoints return data where the 'total' series includes
  29. * breakdown data (issues). For these results `stacked` should be false.
  30. * Other endpoints return decomposed results that need to be stacked (outcomes).
  31. */
  32. stacked: false,
  33. };
  34. type ChartProps = React.ComponentProps<typeof BaseChart>;
  35. type BarChartProps = React.ComponentProps<typeof BarChart>;
  36. type Props = Omit<ChartProps, 'series'> &
  37. typeof defaultProps & {
  38. /**
  39. * A list of series to be rendered as markLine components on the chart
  40. * This is often used to indicate start/end markers on the xAxis
  41. */
  42. markers?: Marker[];
  43. /**
  44. * Whether timestamps are should be shown in UTC or local timezone.
  45. */
  46. utc?: boolean;
  47. /**
  48. * A list of colors to use on hover.
  49. * By default hover state will shift opacity from 0.6 to 1.0.
  50. * You can use this prop to also shift colors on hover.
  51. */
  52. emphasisColors?: string[];
  53. /**
  54. * Delay time for hiding tooltip, in ms.
  55. */
  56. hideDelay?: number;
  57. /**
  58. * Function to format tooltip values
  59. */
  60. tooltipFormatter?: (value: number) => string;
  61. series?: BarChartProps['series'];
  62. };
  63. class MiniBarChart extends React.Component<Props> {
  64. static defaultProps = defaultProps;
  65. render() {
  66. const {
  67. markers,
  68. emphasisColors,
  69. colors,
  70. series: _series,
  71. labelYAxisExtents,
  72. stacked,
  73. series,
  74. hideDelay,
  75. tooltipFormatter,
  76. ...props
  77. } = this.props;
  78. const {ref: _ref, ...barChartProps} = props;
  79. let chartSeries: BarChartSeries[] = [];
  80. // Ensure bars overlap and that empty values display as we're disabling the axis lines.
  81. if (series && series.length) {
  82. chartSeries = series.map((original, i: number) => {
  83. const updated = {
  84. ...original,
  85. cursor: 'normal',
  86. type: 'bar',
  87. } as BarChartSeries;
  88. if (i === 0) {
  89. updated.barMinHeight = 1;
  90. if (stacked === false) {
  91. updated.barGap = '-100%';
  92. }
  93. }
  94. if (stacked) {
  95. updated.stack = 'stack1';
  96. }
  97. set(updated, 'itemStyle.color', colors[i]);
  98. set(updated, 'itemStyle.opacity', 0.6);
  99. set(updated, 'itemStyle.emphasis.opacity', 1.0);
  100. set(updated, 'itemStyle.emphasis.color', emphasisColors?.[i] ?? colors[i]);
  101. return updated;
  102. });
  103. }
  104. if (markers) {
  105. const markerTooltip = {
  106. show: true,
  107. trigger: 'item',
  108. formatter: ({data}) => {
  109. const time = getFormattedDate(data.coord[0], 'MMM D, YYYY LT', {
  110. local: !this.props.utc,
  111. });
  112. const name = truncationFormatter(data.name, props?.xAxis?.truncate);
  113. return [
  114. '<div class="tooltip-series">',
  115. `<div><span class="tooltip-label"><strong>${name}</strong></span></div>`,
  116. '</div>',
  117. '<div class="tooltip-date">',
  118. time,
  119. '</div>',
  120. '</div>',
  121. '<div class="tooltip-arrow"></div>',
  122. ].join('');
  123. },
  124. };
  125. const markPoint = {
  126. data: markers.map((marker: Marker) => ({
  127. name: marker.name,
  128. coord: [marker.value, 0],
  129. tooltip: markerTooltip,
  130. symbol: 'circle',
  131. symbolSize: marker.symbolSize ?? 8,
  132. itemStyle: {
  133. color: marker.color,
  134. borderColor: '#ffffff',
  135. },
  136. })),
  137. };
  138. chartSeries[0].markPoint = markPoint;
  139. }
  140. const yAxisOptions = labelYAxisExtents
  141. ? {
  142. showMinLabel: true,
  143. showMaxLabel: true,
  144. interval: Infinity,
  145. }
  146. : {
  147. axisLabel: {
  148. show: false,
  149. },
  150. };
  151. const chartOptions = {
  152. tooltip: {
  153. trigger: 'axis' as const,
  154. hideDelay,
  155. valueFormatter: tooltipFormatter
  156. ? (value: number) => tooltipFormatter(value)
  157. : undefined,
  158. },
  159. yAxis: {
  160. max(value) {
  161. // This keeps small datasets from looking 'scary'
  162. // by having full bars for < 10 values.
  163. return Math.max(10, value.max);
  164. },
  165. splitLine: {
  166. show: false,
  167. },
  168. ...yAxisOptions,
  169. },
  170. grid: {
  171. // Offset to ensure there is room for the marker symbols at the
  172. // default size.
  173. top: labelYAxisExtents ? 6 : 0,
  174. bottom: markers || labelYAxisExtents ? 4 : 0,
  175. left: markers ? 4 : 0,
  176. right: markers ? 4 : 0,
  177. },
  178. xAxis: {
  179. axisLine: {
  180. show: false,
  181. },
  182. axisTick: {
  183. show: false,
  184. alignWithLabel: true,
  185. },
  186. axisLabel: {
  187. show: false,
  188. },
  189. axisPointer: {
  190. type: 'line' as const,
  191. label: {
  192. show: false,
  193. },
  194. lineStyle: {
  195. width: 0,
  196. },
  197. },
  198. },
  199. options: {
  200. animation: false,
  201. },
  202. };
  203. return <BarChart series={chartSeries} {...chartOptions} {...barChartProps} />;
  204. }
  205. }
  206. export default MiniBarChart;