Skip to content

Commit 546c1c3

Browse files
committed
Sorting change when x axis is a date
1 parent 7e5a0cb commit 546c1c3

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

apps/webapp/app/components/code/QueryResultsChart.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,8 @@ function aggregateValues(values: number[], aggregation: AggregationType): number
671671
function sortData(
672672
data: Record<string, unknown>[],
673673
sortByColumn: string | null,
674-
sortDirection: "asc" | "desc"
674+
sortDirection: "asc" | "desc",
675+
xAxisColumn?: string | null
675676
): Record<string, unknown>[] {
676677
if (!sortByColumn) return data;
677678

@@ -684,12 +685,14 @@ function sortData(
684685
if (aVal == null) return sortDirection === "asc" ? -1 : 1;
685686
if (bVal == null) return sortDirection === "asc" ? 1 : -1;
686687

687-
// Try to compare as dates first (using __rawDate if available)
688-
const aDate = a.__rawDate as Date | null;
689-
const bDate = b.__rawDate as Date | null;
690-
if (aDate && bDate) {
691-
const diff = aDate.getTime() - bDate.getTime();
692-
return sortDirection === "asc" ? diff : -diff;
688+
// Only use date comparison when sorting by the X-axis column
689+
if (sortByColumn === xAxisColumn) {
690+
const aDate = a.__rawDate as Date | null;
691+
const bDate = b.__rawDate as Date | null;
692+
if (aDate && bDate) {
693+
const diff = aDate.getTime() - bDate.getTime();
694+
return sortDirection === "asc" ? diff : -diff;
695+
}
693696
}
694697

695698
// Compare as numbers if possible
@@ -737,9 +740,9 @@ export const QueryResultsChart = memo(function QueryResultsChart({
737740
const data = useMemo(() => {
738741
if (isDateBased) {
739742
// Always sort by timestamp for date-based axes
740-
return sortData(unsortedData, xDataKey, "asc");
743+
return sortData(unsortedData, xDataKey, "asc", xDataKey);
741744
}
742-
return sortData(unsortedData, sortByColumn, sortDirection);
745+
return sortData(unsortedData, sortByColumn, sortDirection, xDataKey);
743746
}, [unsortedData, sortByColumn, sortDirection, isDateBased, xDataKey]);
744747

745748
// Detect time granularity for the data

0 commit comments

Comments
 (0)