Pie Chart (above, in between, below)

  • What Grafana version and what operating system are you using?

I am running Grafana v11.4.0 (b58701869e)

  • What are you trying to achieve?

I have data in the form

dateString | sgv
2025-01-02 13:28:36 | 90
2025-01-02 13:28:40 | 67
2025-01-02 13:28:50 | 110
2025-01-02 13:28:56 | 190

and I would like to have a visualization, e.g. a pie chart to show me the percentage of the values below a given minValue = 70, above maxValue = 180 or in between those?

I think it should be possible to achieve this using Transformations, but I struggle to understand how to get there. I know how to filter the entire dataset but I have not yet managed to filter it to get the pie chart to work.

Which transformations would I have to apply to get there?

What is your datasource? An api or a database?

I am using GitHub - haohanyang/mongodb-datasource: MongoDB data source for Grafana as data source. I have seen some resources on how to aggregate the data from flux but not that many on the transformation side.

I highly recommend you try to do it in Mongo’s query first, transformation as last resort

Hi,

I found something like this - maybe it’ll help with the aggregation?

Using the following (I have to admit, ChatGPT result) it works. Thanks @yosiasz for the suggestion.

[
    {
        "$match": {
            "date": {
                "$gt": {
                    "$numberLong": "$__from"
                },
                "$lt": {
                    "$numberLong": "$__to"
                }
            }
        }
    },
    {
        "$group": {
            "_id": null,
            "below": {
                "$sum": {
                    "$cond": [
                        { "$lt": ["$sgv", 70] },
                        1,
                        0
                    ]
                }
            },
            "between": {
                "$sum": {
                    "$cond": [
                        { "$and": [
                            { "$gte": ["$sgv", 70] },
                            { "$lte": ["$sgv", 180] }
                        ] },
                        1,
                        0
                    ]
                }
            },
            "above": {
                "$sum": {
                    "$cond": [
                        { "$gt": ["$sgv", 180] },
                        1,
                        0
                    ]
                }
            }
        }
    },
    {
        "$project": {
            "_id": 0,
            "below": 1,
            "between": 1,
            "above": 1
        }
    }
]