TooltipView.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { css } from '@emotion/css';
  2. import React, { PureComponent } from 'react';
  3. import { DataFrame, Field, formattedValueToString, getFieldDisplayName, GrafanaTheme2 } from '@grafana/data';
  4. import { stylesFactory } from '@grafana/ui';
  5. import { config } from 'app/core/config';
  6. import { ScatterSeries } from './types';
  7. export interface Props {
  8. series: ScatterSeries;
  9. data: DataFrame[]; // source data
  10. rowIndex?: number; // the hover row
  11. }
  12. export class TooltipView extends PureComponent<Props> {
  13. style = getStyles(config.theme2);
  14. render() {
  15. const { series, data, rowIndex } = this.props;
  16. if (!series || rowIndex == null) {
  17. return null;
  18. }
  19. const frame = series.frame(data);
  20. const y = undefined; // series.y(frame);
  21. return (
  22. <table className={this.style.infoWrap}>
  23. <tbody>
  24. {frame.fields.map((f, i) => (
  25. <tr key={`${i}/${rowIndex}`} className={f === y ? this.style.highlight : ''}>
  26. <th>{getFieldDisplayName(f, frame)}:</th>
  27. <td>{fmt(f, rowIndex)}</td>
  28. </tr>
  29. ))}
  30. </tbody>
  31. </table>
  32. );
  33. }
  34. }
  35. function fmt(field: Field, row: number): string {
  36. const v = field.values.get(row);
  37. if (field.display) {
  38. return formattedValueToString(field.display(v));
  39. }
  40. return `${v}`;
  41. }
  42. const getStyles = stylesFactory((theme: GrafanaTheme2) => ({
  43. infoWrap: css`
  44. padding: 8px;
  45. th {
  46. font-weight: ${theme.typography.fontWeightMedium};
  47. padding: ${theme.spacing(0.25, 2)};
  48. }
  49. `,
  50. highlight: css`
  51. background: ${theme.colors.action.hover};
  52. `,
  53. }));