DebugOverlay.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { css } from '@emotion/css';
  2. import { Map } from 'ol';
  3. import { Coordinate } from 'ol/coordinate';
  4. import { transform } from 'ol/proj';
  5. import React, { PureComponent } from 'react';
  6. import tinycolor from 'tinycolor2';
  7. import { GrafanaTheme } from '@grafana/data';
  8. import { stylesFactory } from '@grafana/ui';
  9. import { config } from 'app/core/config';
  10. interface Props {
  11. map: Map;
  12. }
  13. interface State {
  14. zoom?: number;
  15. center: Coordinate;
  16. }
  17. export class DebugOverlay extends PureComponent<Props, State> {
  18. style = getStyles(config.theme);
  19. constructor(props: Props) {
  20. super(props);
  21. this.state = { zoom: 0, center: [0, 0] };
  22. }
  23. updateViewState = () => {
  24. const view = this.props.map.getView();
  25. this.setState({
  26. zoom: view.getZoom(),
  27. center: transform(view.getCenter()!, view.getProjection(), 'EPSG:4326'),
  28. });
  29. };
  30. componentDidMount() {
  31. this.props.map.on('moveend', this.updateViewState);
  32. this.updateViewState();
  33. }
  34. render() {
  35. const { zoom, center } = this.state;
  36. return (
  37. <div className={this.style.infoWrap}>
  38. <table>
  39. <tbody>
  40. <tr>
  41. <th>Zoom:</th>
  42. <td>{zoom?.toFixed(1)}</td>
  43. </tr>
  44. <tr>
  45. <th>Center:&nbsp;</th>
  46. <td>
  47. {center[0].toFixed(5)}, {center[1].toFixed(5)}
  48. </td>
  49. </tr>
  50. </tbody>
  51. </table>
  52. </div>
  53. );
  54. }
  55. }
  56. const getStyles = stylesFactory((theme: GrafanaTheme) => ({
  57. infoWrap: css`
  58. color: ${theme.colors.text};
  59. background: ${tinycolor(theme.colors.panelBg).setAlpha(0.7).toString()};
  60. border-radius: 2px;
  61. padding: 8px;
  62. `,
  63. }));