Let's break down what your code is doing step-by-step: 1. **Create...
Let's break down what your code is doing step-by-step:
-
Create a Set (
storeSet
):const storeSet = new Set();
A
Set
is created to store unique values of themetric.label.store
extracted fromseriesObj
. -
Iterate through
context.panel.data.series
:context.panel.data.series.forEach((seriesObj) => { storeSet.add(seriesObj.meta.custom.labels["metric.label.store"]); console.log(seriesObj.meta.custom.labels["metric.label.store"]); });
For each series object in
context.panel.data.series
:- The
metric.label.store
value frommeta.custom.labels
is added to thestoreSet
. - This ensures only unique values are added since
Set
automatically enforces uniqueness. - The extracted
metric.label.store
value is logged to the console.
- The
-
Log the
Set
values:console.log(storeSet);
The
Set
(storeSet
) is logged for debugging purposes. -
Convert the Set to an Array:
const storesArray = Array.from(storeSet);
The
storeSet
is converted into an array (storesArray
) so it can later be passed as a value to the chart's click handler. -
Add an event listener to the chart's
mouseover
event:context.panel.chart.on("mouseover", (params) => { context.grafana.locationService.partial({ "var-query0": storesArray }, true); });
This line adds a
mouseover
event listener to a chart.- When the mouseover event occurs on the chart, the code updates a variable
var-query0
in the Grafana URL with thestoresArray
as a query parameter. - The
true
parameter at the end indicates that it should replace or merge the parameter in the URL.
- When the mouseover event occurs on the chart, the code updates a variable
-
Return the title:
return { 'title': { "text": "update" } };
Finally, the function returns an object with the chart's title set to
"update"
.
Why might the text not update on click?
The text (title
) defined here ("update"
) is hardcoded in the return statement. Since there is nothing in your code that dynamically updates this title
value when you interact with the chart, clicking or triggering the mouseover event won't modify it.
Key reasons:
- The title
"update"
is static:- The
title
is defined in the return statement and does not depend on any dynamic parameters or events.
- The
- No
click
handler in place:- While you are using a
mouseover
event, the code has noclick
event listener or logic tied to the title update.
- While you are using a
- No UI-binding updates for the title:
- Grafana panels generally do not reactively update elements such as titles unless explicitly reinitialized with new parameters. Simply returning an updated title object won't automatically change the title in the UI once it is rendered.
Suggested Fix:
If you want to update the title dynamically (e.g., based on interactions with the chart), you need to explicitly set or render the title in response to the chart's interactions. Here's an example of how you could handle it:
context.panel.chart.on("mouseover", (params) => {
context.grafana.locationService.partial({ "var-query0": storesArray }, true);
// Dynamically set chart title
context.panel.chart.setOption({
title: { text: `Store Hovered: ${params.name}` }
});
});
This code extends the mouseover
event to dynamically set the chart title using the setOption
method (common in libraries like ECharts). Ensure that your chart library supports this method to update the title directly when the mouseover event occurs.