Apps

Apps

DDX Brick: Page Filter

Hi Everyone,

I have created a DDX brick which is correctly making a gauge chart however it only shows the total aggregated number and doesn't update when a page filter is applied.

Does anyone know what further code needs to be applied? I want it to update when the column DATE is applied on the page.

Thank you for your help!

Gauge Chart.png
Tagged:

Welcome!

It looks like you're new here. Members get access to exclusive content, events, rewards, and more. Sign in or register to get started.
Sign In

Best Answer

  • Coach
    Answer ✓

    Hi @pwhite8 ,

    You might want to look into domo.onFiltersUpdate() to do something in your brick when the filters update.

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**

Answers

  • Coach
    Answer ✓

    Hi @pwhite8 ,

    You might want to look into domo.onFiltersUpdate() to do something in your brick when the filters update.

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**
  • Member
    edited August 2023

    Thanks so much @GrantSmith. how does this need to be applied? the documentation doesnt give any examples so not sure how to get it to work. I tried this but when using the filter it says NaN on the chart.

    var domo = window.domo;
    var datasets = window.datasets;

    // pass in dataset0
    domo.get(data/v1/${datasets[0]})
    .then(function (data) {
    // Function to calculate the sum of an array of objects' property
    function calculateSumOfProperty(arrayOfObjects, property) {
    return arrayOfObjects.reduce((acc, curr) => acc + curr[property], 0);
    }

    1. // Calculate the sum of "On Time"
    2. const sumOnTime = calculateSumOfProperty(data, "On Time");

    3. // Calculate the sum of "Planned"
    4. const sumPlanned = calculateSumOfProperty(data, "Planned");

    5. // Calculate the ratio of "On Time" to "Planned"
    6. const punctuality = sumOnTime / sumPlanned;

    7. // Set the punctuality value to the gauge chart
    8. const gaugeValueElement = document.getElementById("gaugeValue");
    9. const gaugeFillElement = document.getElementById("gaugeFill");
    10. const percentage = Math.min(Math.max(punctuality * 100, 0), 100);

    11. gaugeValueElement.textContent = `${percentage.toFixed(1)}%`;
    12. gaugeFillElement.style.transform = `rotate(${1.8 * percentage}deg)`;

    13. // Set the color of gaugeFillElement based on the percentage value
    14. if (percentage >= 92) {
    15. gaugeFillElement.style.backgroundColor = "#008B00";
    16. } else if (percentage >= 87) {
    17. gaugeFillElement.style.backgroundColor = "#FFA500";
    18. } else {
    19. gaugeFillElement.style.backgroundColor = "#FF0000";
    20. }

    21. // Set the "On Time / Planned" text below the gauge chart
    22. const onTimePlannedTextElement = document.getElementById("onTimePlannedText");
    23. onTimePlannedTextElement.textContent = `${sumOnTime} / ${sumPlanned}`;

    24. // Function to update the filter
    25. function updateDataWithFilter(data) {
    26. const filteredData = data.filter((item) => {

    27. const filterValue = data.filters.find((filter) => filter.column === "SERVICE_DATE");

    28. return item.SERVICE_DATE === filterValue.value;
    29. });

    30. // Recalculate the sum of "On Time" and "Planned" for the filtered data
    31. const sumOnTimeFiltered = calculateSumOfProperty(filteredData, "On Time");
    32. const sumPlannedFiltered = calculateSumOfProperty(filteredData, "Planned");

    33. // Calculate the ratio of "On Time" to "Planned" for the filtered data
    34. const punctualityFiltered = sumOnTimeFiltered / sumPlannedFiltered;

    35. // Update the gauge chart with the filtered data
    36. const percentageFiltered = Math.min(Math.max(punctualityFiltered * 100, 0), 100);
    37. gaugeValueElement.textContent = `${percentageFiltered.toFixed(1)}%`;
    38. gaugeFillElement.style.transform = `rotate(${1.8 * percentageFiltered}deg)`;

    39. // Set the color of gaugeFillElement based on the percentage value of the filtered data
    40. if (percentageFiltered >= 92) {
    41. gaugeFillElement.style.backgroundColor = "#008B00";
    42. } else if (percentageFiltered >= 87) {
    43. gaugeFillElement.style.backgroundColor = "#FFA500";
    44. } else {
    45. gaugeFillElement.style.backgroundColor = "#FF0000";
    46. }

    47. // Set the "On Time / Planned" text below the gauge chart for the filtered data
    48. onTimePlannedTextElement.textContent = `${sumOnTimeFiltered} / ${sumPlannedFiltered}`;
    49. }

    50. // Attach the filter update event handler
    51. domo.onFiltersUpdate(function (data) {
    52. // Call the function to update data with the filter
    53. updateDataWithFilter(data);
    54. });

    });

  • Domo Employee
    edited August 2023
    Screenshot 2023-08-08 at 1.34.46 PM.png

    Are you referring to the quick filter drop down in the top right? That does not support custom apps you will want to use the standard page filters.

  • No I am not. I am refering standard page filters. Do you know if there are any examples of this being used?

  • Domo Employee

    Are the datasets you are querying wired to the app?

  • yes they are. if i apply a page filter with the column there is no impact on the card. Grant mentioned you need to do something with  domo.onFiltersUpdate() within the script. Do you know if there are any examples of this being used (the documentation doesnt give any examples)?

  • Domo Employee

    Sorry for such a late response but onFiltersUpdate is used to suppress the filter change from refreshing the app and then you will have to code what those filters will do in your app. If you just want the app to reflect the standard page filters and if the dataset being filtered is wired to the app it should reflect any changes to the page filters.

  • you will have to code what those filters will do in your app

    And how do we get the chart to refresh after we have coded what those filters will do in our app?

  • Member
    edited November 2024

    So I have finally figured this out, I'm going to add it here because I couldn't find a clear answer anywhere on the web. The secret seemed to be resetting the innerHTML of the chart to empty so it will actually re-render. This really should be in the documentation somewhere.

    1. domo.onFiltersUpdate((filters) => {
    2. //custom logic to react to filters
    3. fetchData().then(function(data){
    4. chartIt(data);
    5. }
    6. }
    7.  
    8. function fetchData(){
    9. return return domo.get();
    10. }
    11.  
    12. function chartIt(){
    13. //This here is the important bit
    14. document.getElementById('chart').innerHTML = '';
    15. //all the normal logic for rendering the chart
    16. }

Welcome!

It looks like you're new here. Members get access to exclusive content, events, rewards, and more. Sign in or register to get started.
Sign In