2021-05-18 09:16:26 -05:00
|
|
|
import React, { FC, useState } from 'react';
|
|
|
|
import AutoSizer from 'react-virtualized-auto-sizer';
|
|
|
|
import { css } from '@emotion/css';
|
2021-05-19 10:14:12 -05:00
|
|
|
import { GrafanaTheme2, PanelData } from '@grafana/data';
|
2021-07-01 05:02:41 -05:00
|
|
|
import { PanelRenderer } from '@grafana/runtime';
|
|
|
|
import { useStyles2 } from '@grafana/ui';
|
2021-05-19 10:14:12 -05:00
|
|
|
import { PanelOptions } from 'app/plugins/panel/table/models.gen';
|
|
|
|
import { useVizHeight } from '../../hooks/useVizHeight';
|
2021-07-01 05:02:41 -05:00
|
|
|
import { SupportedPanelPlugins, PanelPluginsButtonGroup } from '../PanelPluginsButtonGroup';
|
2021-05-18 09:16:26 -05:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
data: PanelData;
|
2021-05-27 03:46:06 -05:00
|
|
|
currentPanel: SupportedPanelPlugins;
|
|
|
|
changePanel: (panel: SupportedPanelPlugins) => void;
|
2021-05-18 09:16:26 -05:00
|
|
|
}
|
|
|
|
|
2021-05-27 03:46:06 -05:00
|
|
|
export const VizWrapper: FC<Props> = ({ data, currentPanel, changePanel }) => {
|
2021-05-19 10:14:12 -05:00
|
|
|
const [options, setOptions] = useState<PanelOptions>({
|
|
|
|
frameIndex: 0,
|
|
|
|
showHeader: true,
|
|
|
|
});
|
2021-05-27 03:46:06 -05:00
|
|
|
const vizHeight = useVizHeight(data, currentPanel, options.frameIndex);
|
2021-05-27 09:15:43 -05:00
|
|
|
const styles = useStyles2(getStyles(vizHeight));
|
2021-05-18 09:16:26 -05:00
|
|
|
|
|
|
|
if (!options || !data) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.wrapper}>
|
|
|
|
<div className={styles.buttonGroup}>
|
2021-07-01 05:02:41 -05:00
|
|
|
<PanelPluginsButtonGroup onChange={changePanel} value={currentPanel} />
|
2021-05-18 09:16:26 -05:00
|
|
|
</div>
|
2021-05-27 09:15:43 -05:00
|
|
|
<AutoSizer>
|
|
|
|
{({ width }) => {
|
|
|
|
if (width === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<div style={{ height: `${vizHeight}px`, width: `${width}px` }}>
|
2021-05-18 09:16:26 -05:00
|
|
|
<PanelRenderer
|
2021-05-27 09:15:43 -05:00
|
|
|
height={vizHeight}
|
2021-05-18 09:16:26 -05:00
|
|
|
width={width}
|
|
|
|
data={data}
|
2021-05-27 03:46:06 -05:00
|
|
|
pluginId={currentPanel}
|
2021-05-18 09:16:26 -05:00
|
|
|
title="title"
|
2021-05-19 10:14:12 -05:00
|
|
|
onOptionsChange={setOptions}
|
2021-05-18 09:16:26 -05:00
|
|
|
options={options}
|
|
|
|
/>
|
2021-05-27 09:15:43 -05:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</AutoSizer>
|
2021-05-18 09:16:26 -05:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-05-27 09:15:43 -05:00
|
|
|
const getStyles = (visHeight: number) => (theme: GrafanaTheme2) => ({
|
2021-05-18 09:16:26 -05:00
|
|
|
wrapper: css`
|
|
|
|
padding: 0 ${theme.spacing(2)};
|
2021-05-27 09:15:43 -05:00
|
|
|
height: ${visHeight + theme.spacing.gridSize * 4}px;
|
2021-05-18 09:16:26 -05:00
|
|
|
`,
|
|
|
|
buttonGroup: css`
|
|
|
|
display: flex;
|
|
|
|
justify-content: flex-end;
|
|
|
|
`,
|
|
|
|
});
|