Geomap: implement basic tooltip support (#37318)

Co-authored-by: Bryan Uribe <buribe@hmc.edu>
This commit is contained in:
Ryan McKinley
2021-07-28 18:34:42 -07:00
committed by GitHub
parent 8b80d2256d
commit ced26bc624
7 changed files with 174 additions and 24 deletions

View File

@ -0,0 +1,56 @@
import React, { PureComponent } from 'react';
import { stylesFactory } from '@grafana/ui';
import { DataFrame, Field, formattedValueToString, getFieldDisplayName, GrafanaTheme2 } from '@grafana/data';
import { css } from '@emotion/css';
import { config } from 'app/core/config';
export interface Props {
data?: DataFrame; // source data
rowIndex?: number; // the hover row
columnIndex?: number; // the hover column
}
export class DataHoverView extends PureComponent<Props> {
style = getStyles(config.theme2);
render() {
const { data, rowIndex, columnIndex } = this.props;
if (!data || rowIndex == null) {
return null;
}
return (
<table className={this.style.infoWrap}>
<tbody>
{data.fields.map((f, i) => (
<tr key={`${i}/${rowIndex}`} className={i === columnIndex ? this.style.highlight : ''}>
<th>{getFieldDisplayName(f, data)}:</th>
<td>{fmt(f, rowIndex)}</td>
</tr>
))}
</tbody>
</table>
);
}
}
function fmt(field: Field, row: number): string {
const v = field.values.get(row);
if (field.display) {
return formattedValueToString(field.display(v));
}
return `${v}`;
}
const getStyles = stylesFactory((theme: GrafanaTheme2) => ({
infoWrap: css`
padding: 8px;
th {
font-weight: bold;
padding: 2px 10px 2px 0px;
}
`,
highlight: css`
background: ${theme.colors.action.hover};
`,
}));