Grafana - refresh a panel via code

Hello Guys, I want to refresh a panel via code anyone has good suggestions?

It’s not really clear what do you try to achieve. Most probably you are talking about events. use onOptionsChange, for example.

Thank you Mikhail Volkov, I have the REST API datasource, when the API gives a new data update I want to refresh the panel from datasource code, is there a way to implement it? I see the panelid will accessible from datasource side code, but not sure I can use it to get the panel and trigger refresh

What do you mean “the API gives a new data update” ?

If dashboard refreshed by interval, then datasource will request new data and it will be displayed. If you are using streaming you should be circular data frames.

Panels should trigger datasource, not the other way around. If you are looking for specific behavior you should implement custom panel.

Thank you for your explain

It’s hard to give the proper recommendation without understanding your use case

Actually, I want to refresh one panel when another panel in the same dashboard has a click event

I asked the same question Grafana team months ago. I believe it should be done using Dashboard variables, but I changed my approach since then. Maybe can help here.

As correctly mentioned, the currently recommended way to refresh a panel from another panel is through shared dashboard variables. Panels are refresh when any variables they depend on are updated. Since you can update dashboard variables from panel, you can use them to share state between panels.

  1. Can I change the variable via code ?
  2. And can I trigger single panel refresh when related variables changed
    ?
  1. Yes. Read more in the docs here: https://grafana.com/docs/grafana/latest/developers/plugins/add-support-for-variables/#set-a-variable-from-your-plugin
  2. When you update the variable, any panels using that variable will update automatically. No need to trigger it manually.

Thank you so much