I needed to use grafa for 3d visualisation and here are the steps I used to use r3f inside a panel plugin:
1: make a new panel plugin
htt ps://grafana.com/developers/plugin-tools/tutorials/build-a-panel-plugin
if you haven’t already, I find myself needing to run npm i before npm run dev
2: intall compatible versions with grafana’s react.
grafana uses r18 so fiber8 works with it
npm install three @react-three/fiber@8 @react-three/drei@9
npm install --save-dev @types/three
3: change webpack config:
official docs (htt ps://grafana.com/developers/plugin-tools/how-to-guides/extend-configurations)
add webpack.config.ts to your project root containing the following
// webpack.config.ts (Project Root)
import type { Configuration } from 'webpack';
import { merge } from 'webpack-merge';
import grafanaConfig, { Env } from './.config/webpack/webpack.config';
import path from 'path';
const config = async (env: Env): Promise<Configuration> => {
const baseConfig = await grafanaConfig(env);
return merge(baseConfig, {
resolve: {
alias: {
// Intercepts the dev build of react-reconciler to prevent the production flag crash
'react-reconciler': path.resolve(__dirname, 'node_modules/react-reconciler/cjs/react-reconciler.production.min.js'),
},
},
});
};
export default config;
then in package.json as in the docs(htt ps://grafana.com/developers/plugin-tools/how-to-guides/extend-configurations#3-update-the-packagejson-to-use-the-new-webpack-config)
-"build": "webpack -c ./.config/webpack/webpack.config.ts --env production",
+"build": "webpack -c ./webpack.config.ts --env production",
-"dev": "webpack -w -c ./.config/webpack/webpack.config.ts --env development",
+"dev": "webpack -w -c ./webpack.config.ts --env development",
4: use fiber
then in src/components/SimplePanel.tsx
import React from 'react';
import { PanelProps } from '@grafana/data';
import { SimpleOptions } from 'types';
import { Canvas } from '@react-three/fiber';
import { CameraControls } from '@react-three/drei';
interface Props extends PanelProps<SimpleOptions> { }
export const SimplePanel: React.FC<Props> = ({ options, data, width, height, fieldConfig, id }) => {
return (
<div id="canvas-container" style={{ width, height, position: 'relative', overflow: 'hidden' }}>
<Canvas>
<directionalLight color="white" position={[0, 2, 5]} />
<mesh position={[0, 0, 0]} onPointerOver={(e) => { e.stopPropagation() }}>
<boxGeometry args={[1, 1, 1]} />
<meshStandardMaterial />
</mesh>
<CameraControls />
</Canvas>
</div>
);
};
and alhamdo li Allah everything is now working correctly:
it solves this topic which was closed automatically. @tomglenn
thanks to this topic for the start of the solution.
extra
We could even do more complex things (for the job, won’t share more)

