Skip to content

Commit

Permalink
PanelBuilders: Mixin function to share config (#932)
Browse files Browse the repository at this point in the history
  • Loading branch information
torkelo authored Oct 9, 2024
1 parent 7e38318 commit efa57ca
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docusaurus/docs/visualizations.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,17 @@ const scene = new EmbeddedScene({

This built panel is now ready to be used in a scene.

### Extract common visualization config to a mixin function

```ts
function latencyGraphMixin(builder: ReturnType<typeof PanelBuilders["timeseries"]>) {
builder.setMin(0);
builder.setOption('legend', { showLegend: false: true })
}

const panel = PanelBuilders.timeseries().applyMixin(latencyGraphMixin).setData(...)
```

## Custom visualizations

If you want to determine how data is visualized in your Grafana app plugin, you can do so in two ways. You always have the option to create a custom `SceneObject`, but you won't get the `PanelChrome` with loading state and other features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ describe('VizPanelBuilder', () => {
}
`);
});

it('allows mixin function', () => {
const mixin = (builder: ReturnType<typeof getTestBuilder>) => {
builder.setOption('numeric', 2);
};

const p1 = getTestBuilder().applyMixin(mixin).build();
expect(p1.state.options.numeric).toEqual(2);
});
});

describe('overrides', () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/scenes/src/core/PanelBuilders/VizPanelBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ export class VizPanelBuilder<TOptions extends {}, TFieldConfig extends {}>
return this;
}

/**
* Makes it possible to shared config between different builders
*/
public applyMixin(mixin: (builder: this) => void): this {
mixin(this);
return this;
}

/**
* Build the panel.
*/
Expand Down

0 comments on commit efa57ca

Please sign in to comment.