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-05-18 09:16:26 -05:00
|
|
|
import { config, PanelRenderer } from '@grafana/runtime';
|
2021-05-19 10:14:12 -05:00
|
|
|
import { RadioButtonGroup, useStyles2 } from '@grafana/ui';
|
|
|
|
import { PanelOptions } from 'app/plugins/panel/table/models.gen';
|
2021-05-27 03:46:06 -05:00
|
|
|
import { SupportedPanelPlugins } from './QueryWrapper';
|
2021-05-19 10:14:12 -05:00
|
|
|
import { useVizHeight } from '../../hooks/useVizHeight';
|
|
|
|
import { STAT, TABLE, TIMESERIES } from '../../utils/constants';
|
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-18 09:16:26 -05:00
|
|
|
const panels = getSupportedPanels();
|
2021-05-27 03:46:06 -05:00
|
|
|
const vizHeight = useVizHeight(data, currentPanel, options.frameIndex);
|
2021-05-19 10:14:12 -05:00
|
|
|
const styles = useStyles2(getStyles);
|
2021-05-18 09:16:26 -05:00
|
|
|
|
|
|
|
if (!options || !data) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.wrapper}>
|
|
|
|
<div className={styles.buttonGroup}>
|
2021-05-27 03:46:06 -05:00
|
|
|
<RadioButtonGroup options={panels} value={currentPanel} onChange={changePanel} />
|
2021-05-18 09:16:26 -05:00
|
|
|
</div>
|
2021-05-19 10:14:12 -05:00
|
|
|
<div style={{ height: vizHeight, width: '100%' }}>
|
2021-05-18 09:16:26 -05:00
|
|
|
<AutoSizer style={{ width: '100%', height: '100%' }}>
|
|
|
|
{({ width, height }) => {
|
|
|
|
if (width === 0 || height === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<PanelRenderer
|
|
|
|
height={height}
|
|
|
|
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}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</AutoSizer>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const getSupportedPanels = () => {
|
|
|
|
return Object.values(config.panels)
|
|
|
|
.filter((p) => p.id === TIMESERIES || p.id === TABLE || p.id === STAT)
|
|
|
|
.map((panel) => ({ value: panel.id, label: panel.name, imgUrl: panel.info.logos.small }));
|
|
|
|
};
|
|
|
|
|
|
|
|
const getStyles = (theme: GrafanaTheme2) => ({
|
|
|
|
wrapper: css`
|
|
|
|
padding: 0 ${theme.spacing(2)};
|
|
|
|
`,
|
2021-05-19 10:14:12 -05:00
|
|
|
autoSizerWrapper: css`
|
|
|
|
width: 100%;
|
|
|
|
height: 200px;
|
|
|
|
`,
|
2021-05-18 09:16:26 -05:00
|
|
|
buttonGroup: css`
|
|
|
|
display: flex;
|
|
|
|
justify-content: flex-end;
|
|
|
|
`,
|
|
|
|
});
|