diff --git a/public/app/features/scenes/components/VizPanel/VizPanel.tsx b/public/app/features/scenes/components/VizPanel/VizPanel.tsx index f028216b986..22d6b453a08 100644 --- a/public/app/features/scenes/components/VizPanel/VizPanel.tsx +++ b/public/app/features/scenes/components/VizPanel/VizPanel.tsx @@ -1,3 +1,4 @@ +import { DeepPartial } from '@reduxjs/toolkit'; import React from 'react'; import { AbsoluteTimeRange, FieldConfigSource, PanelModel, PanelPlugin, toUtc } from '@grafana/data'; @@ -15,23 +16,21 @@ import { VizPanelRenderer } from './VizPanelRenderer'; export interface VizPanelState extends SceneLayoutChildState { title: string; pluginId: string; - options: TOptions; - fieldConfig: FieldConfigSource; + options: DeepPartial; + fieldConfig: FieldConfigSource>; pluginVersion?: string; // internal state pluginLoadError?: string; } -export class VizPanel extends SceneObjectBase< - VizPanelState, TFieldConfig> -> { +export class VizPanel extends SceneObjectBase> { public static Component = VizPanelRenderer; public static Editor = VizPanelEditor; // Not part of state as this is not serializable private _plugin?: PanelPlugin; - public constructor(state: Partial, TFieldConfig>>) { + public constructor(state: Partial>) { super({ options: {}, fieldConfig: { defaults: {}, overrides: [] }, @@ -109,7 +108,7 @@ export class VizPanel extends SceneObjectBase< this.setState({ options }); }; - public onFieldConfigChange = (fieldConfig: FieldConfigSource) => { + public onFieldConfigChange = (fieldConfig: FieldConfigSource) => { this.setState({ fieldConfig }); }; } diff --git a/public/app/features/scenes/components/VizPanel/panelBuilders.ts b/public/app/features/scenes/components/VizPanel/panelBuilders.ts new file mode 100644 index 00000000000..5fb453715c2 --- /dev/null +++ b/public/app/features/scenes/components/VizPanel/panelBuilders.ts @@ -0,0 +1,32 @@ +import { GraphFieldConfig, TableFieldOptions } from '@grafana/schema'; +import { PanelOptions as BarGaugePanelOptions } from 'app/plugins/panel/bargauge/models.gen'; +import { PanelOptions as TablePanelOptions } from 'app/plugins/panel/table/models.gen'; +import { TimeSeriesOptions } from 'app/plugins/panel/timeseries/types'; + +import { VizPanel, VizPanelState } from './VizPanel'; + +export type TypedVizPanelState = Omit< + Partial>, + 'pluginId' +>; + +export const panelBuilders = { + newTable: (state: TypedVizPanelState) => { + return new VizPanel({ + ...state, + pluginId: 'table', + }); + }, + newGraph: (state: TypedVizPanelState) => { + return new VizPanel({ + ...state, + pluginId: 'timeseries', + }); + }, + newBarGauge: (state: TypedVizPanelState) => { + return new VizPanel({ + ...state, + pluginId: 'bargauge', + }); + }, +}; diff --git a/public/app/features/scenes/scenes/demo.tsx b/public/app/features/scenes/scenes/demo.tsx index 98793e5fe6b..456cc924006 100644 --- a/public/app/features/scenes/scenes/demo.tsx +++ b/public/app/features/scenes/scenes/demo.tsx @@ -8,6 +8,7 @@ import { VizPanel, } from '../components'; import { EmbeddedScene } from '../components/Scene'; +import { panelBuilders } from '../components/VizPanel/panelBuilders'; import { SceneTimeRange } from '../core/SceneTimeRange'; import { SceneEditManager } from '../editor/SceneEditManager'; @@ -19,21 +20,27 @@ export function getFlexLayoutTest(standalone: boolean): Scene { layout: new SceneFlexLayout({ direction: 'row', children: [ - new VizPanel({ + panelBuilders.newGraph({ size: { minWidth: '70%' }, - pluginId: 'timeseries', title: 'Dynamic height and width', $data: getQueryRunnerWithRandomWalkQuery({}, { maxDataPointsFromWidth: true }), }), new SceneFlexLayout({ direction: 'column', children: [ - new VizPanel({ - pluginId: 'timeseries', + panelBuilders.newGraph({ title: 'Fill height', + options: {}, + fieldConfig: { + defaults: { + custom: { + fillOpacity: 20, + }, + }, + overrides: [], + }, }), - new VizPanel({ - pluginId: 'timeseries', + panelBuilders.newGraph({ title: 'Fill height', }), new SceneCanvasText({ @@ -42,10 +49,9 @@ export function getFlexLayoutTest(standalone: boolean): Scene { fontSize: 20, align: 'center', }), - new VizPanel({ - size: { height: 300 }, - pluginId: 'timeseries', + panelBuilders.newGraph({ title: 'Fixed height', + size: { height: 300 }, }), ], }),