ipsapps
Posts: 10
Joined: Tue Jul 16, 2024 12:51 pm

Updating highcharts KPIs with filters

I have a dashboard that has 2 charts and 2 KPI boxes
The charts each have drilldowns within them and extra fields to make the filtering functional. So the KPI boxes can't just take the "last value" from the window of data.

I have the following functional that works where it is going and finding the value in the html and updating it, but I don't love this approach as it is dependent on specific class naming.
All of my other filtering - I am using the highcharts.charts array to perform the filters, but I cannot do that with this. Are KPI values rendering in some other array? I am pretty new to highcharts/js so trying to muddle my way through. This is the last piece I have to figure out.


Code: Select all

function updateKpiElement(kpiConfig, selectedFilters) {
  const dataArray = window[kpiConfig.id];
  if (Array.isArray(dataArray)) {
    const filteredData = filterData(dataArray, selectedFilters);
    const sumOfYs = calculateSumForLastEntry(filteredData);
    window[`lastValue_${kpiConfig.id}`] = sumOfYs;

    const kpiValueElement = document.querySelector(
      `#${kpiConfig.cell} .highcharts-dashboards-component-kpi-value`
    );
    if (kpiValueElement) {
      kpiValueElement.innerText = sumOfYs.toLocaleString();
      console.log(
        `Updated KPI element for cell ${kpiConfig.cell} with value: ${sumOfYs}`
      );
    } else {
      console.error(`KPI value element not found for cell ${kpiConfig.cell}`);
    }
  } else {
    console.log(
      `Skipping KPI element for cell ${kpiConfig.cell} as data array is not found or not an array.`
    );
  }
}
User avatar
dawid.d
Site Moderator
Posts: 1084
Joined: Thu Oct 06, 2022 11:31 am

Re: Updating highcharts KPIs with filters

Hello,

Welcome to our forum and thanks for contacting us with your question!

There are two solutions for now; you must choose which suits you better.

First - edit the KPIComponent `getValue` method with the definition of the logic in which value is to be taken from the connector and define your own extremes sync handler. Demo below.

Custom Sync & Wrap Demo: https://jsfiddle.net/BlackLabel/rLekmgvb/
Docs: https://www.highcharts.com/docs/dashboa ... ronization

Second - Use Math Modifier. Then unfortunately if you use extremes synchronization, the values ​​will switch to the last one from the previous column, which is a bug. This can be fixed as above by also defining custom sync.

Math Modifier Demo https://jsfiddle.net/BlackLabel/cgz6Lhta/

Summary - Considering that there are two solutions, one is quite complicated and requires a lot of intervention, and the other is incomplete for all functionalities, I decided to request a feature that seems to be quite useful. There you can vote for its introduction and take part in any discussion: https://github.com/highcharts/highcharts/issues/21559

I hope it was useful and don't hesitate to ask in case of any further questions!
Best regards
Dawid Draguła
Highcharts Developer

Return to “Highcharts Dashboards”