Skip to content

Legend items do not adhere to the sort order given in the query #1237

Open
@AndreKR

Description

@AndreKR

Take a normal time series graph query with a timestamp, a label and a value:

Timestamp Label Value
2025-05-07 00:00:10 b 26
2025-05-07 00:00:10 a 79
2025-05-07 00:00:10 c 85
2025-05-07 00:00:20 b 56
2025-05-07 00:00:20 a 21
2025-05-07 00:00:20 c 27
... ... ...

Here is a query that generates such a result (with timestamps within the last half hour):

SELECT
    now() - INTERVAL 30 MINUTE + INTERVAL ts_idx * 10 SECOND AS timestamp,
    ['b', 'a', 'c'][label_idx + 1] AS label,
    rand() % 100 AS value
FROM
(
    SELECT
        ts_idxs.number AS ts_idx,
        label_idxs.number AS label_idx
    FROM numbers(180) AS ts_idxs
    CROSS JOIN numbers(3) AS label_idxs
)
ORDER BY timestamp

Note that I have intentionally sorted the labels in the order "b", "a", "c".

Despite this, the items in the legend are sorted alphabetically and I haven't found a configuration that would change that:

Image

I would expect the sort order from the query to be retained. That is also the behavior of at least the InfluxDB and the Prometheus data source which I believe both allow sorting in the query.

Environment:

  • Grafana version: v11.6.0
  • Plugin version: 4.5.1
  • OS Grafana is installed on: Ubuntu
  • User OS & Browser: Windows, Chrome

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Incoming

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions