grafana / grafana/clickhouse-datasource

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

Open
#1,237 1 comment 0 reactions 0 assignees View on GitHub
datasource/ClickHouse type/bug
Dominant language
TypeScript
Stars
221
Forks
129
Avg merge
4d 17h
Merged PRs (30d)
21

Description

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](https://github.com/user-attachments/assets/700e0f63-99f3-435c-8b41-3be1208cdab4)

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.