2021-07-28 18:34:42 -07:00
|
|
|
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';
|
2021-09-23 19:31:36 -07:00
|
|
|
import { FeatureLike } from 'ol/Feature';
|
2021-07-28 18:34:42 -07:00
|
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
|
data?: DataFrame; // source data
|
2021-09-23 19:31:36 -07:00
|
|
|
feature?: FeatureLike;
|
2021-07-28 18:34:42 -07:00
|
|
|
rowIndex?: number; // the hover row
|
|
|
|
|
columnIndex?: number; // the hover column
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class DataHoverView extends PureComponent<Props> {
|
|
|
|
|
style = getStyles(config.theme2);
|
|
|
|
|
|
|
|
|
|
render() {
|
2021-09-23 19:31:36 -07:00
|
|
|
const { data, feature, rowIndex, columnIndex } = this.props;
|
|
|
|
|
|
|
|
|
|
if (feature) {
|
|
|
|
|
return (
|
|
|
|
|
<table className={this.style.infoWrap}>
|
|
|
|
|
<tbody>
|
|
|
|
|
{Object.entries(feature.getProperties()).map(
|
|
|
|
|
(e, i) =>
|
|
|
|
|
e[0] === 'geometry' || ( //don't include geojson feature geometry
|
|
|
|
|
<tr key={`${e}-${i}`}>
|
|
|
|
|
<th>{`${e[0]}: `}</th>
|
|
|
|
|
<td>{`${e[1]}`}</td>
|
|
|
|
|
</tr>
|
|
|
|
|
)
|
|
|
|
|
)}
|
|
|
|
|
</tbody>
|
|
|
|
|
</table>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-28 18:34:42 -07:00
|
|
|
if (!data || rowIndex == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<table className={this.style.infoWrap}>
|
|
|
|
|
<tbody>
|
2021-12-14 15:32:17 -08:00
|
|
|
{data.fields
|
|
|
|
|
.filter((f) => !Boolean(f.config.custom?.hideFrom?.tooltip))
|
|
|
|
|
.map((f, i) => (
|
|
|
|
|
<tr key={`${i}/${rowIndex}`} className={i === columnIndex ? this.style.highlight : ''}>
|
|
|
|
|
<th>{getFieldDisplayName(f, data)}:</th>
|
|
|
|
|
<td>{fmt(f, rowIndex)}</td>
|
|
|
|
|
</tr>
|
|
|
|
|
))}
|
2021-07-28 18:34:42 -07:00
|
|
|
</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 {
|
2021-08-26 10:17:03 +02:00
|
|
|
font-weight: ${theme.typography.fontWeightMedium};
|
|
|
|
|
padding: ${theme.spacing(0.25, 2)};
|
2021-07-28 18:34:42 -07:00
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
highlight: css`
|
|
|
|
|
background: ${theme.colors.action.hover};
|
|
|
|
|
`,
|
|
|
|
|
}));
|