Skip to content

Commit b46b595

Browse files
committed
color matching
1 parent f6f36ac commit b46b595

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

web/components/templates/dashboard/panels/modelsByCostPanel.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import StyledAreaChart from "../styledAreaChart";
2-
import { sortAndColorData } from "./utils";
2+
import { sortAndColorDataByName } from "./utils";
33
import { useExpandableBarList } from "./barListPanel";
44

55
interface ModelsByCostPanelProps {
@@ -17,12 +17,11 @@ interface ModelsByCostPanelProps {
1717
const ModelsByCostPanel = (props: ModelsByCostPanelProps) => {
1818
const { models } = props;
1919

20-
const modelData = sortAndColorData(
20+
const modelData = sortAndColorDataByName(
2121
models?.data?.map((model) => ({
2222
name: model.model,
2323
value: model.cost,
2424
})),
25-
"alt2", // Use alt2 color order
2625
);
2726

2827
const maxValue = modelData[0]?.value || 1;

web/components/templates/dashboard/panels/modelsPanel.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import StyledAreaChart from "../styledAreaChart";
2-
import { sortAndColorData } from "./utils";
2+
import { sortAndColorDataByName } from "./utils";
33
import { useExpandableBarList } from "./barListPanel";
44

55
interface ModelsPanelProps {
@@ -17,12 +17,11 @@ interface ModelsPanelProps {
1717
const ModelsPanel = (props: ModelsPanelProps) => {
1818
const { models } = props;
1919

20-
const modelData = sortAndColorData(
20+
const modelData = sortAndColorDataByName(
2121
models?.data?.map((model) => ({
2222
name: model.model,
2323
value: model.total_requests,
2424
})),
25-
"alt1", // Use alt1 color order
2625
);
2726

2827
const maxValue = modelData[0]?.value || 1;

web/components/templates/dashboard/panels/utils.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ const COLOR_ORDERS = {
1515
alt2: ["green", "orange", "purple", "cyan", "yellow", "blue", "pink"] as ListColor[],
1616
};
1717

18+
/**
19+
* Better hash function (DJB2) to deterministically assign colors based on model name
20+
* This provides better distribution to avoid color collisions
21+
*/
22+
function hashString(str: string): number {
23+
let hash = 5381;
24+
for (let i = 0; i < str.length; i++) {
25+
const char = str.charCodeAt(i);
26+
hash = ((hash << 5) + hash) + char; // hash * 33 + char
27+
}
28+
return Math.abs(hash);
29+
}
30+
1831
/**
1932
* Sorts data by value descending and assigns colors in specified order
2033
*/
@@ -37,3 +50,24 @@ export function sortAndColorData<T extends { name: string; value: number }>(
3750
color: colors[index % colors.length],
3851
}));
3952
}
53+
54+
/**
55+
* Sorts data by value descending and assigns colors deterministically based on name
56+
* This ensures the same model gets the same color across different charts
57+
*/
58+
export function sortAndColorDataByName<T extends { name: string; value: number }>(
59+
data: T[] | undefined,
60+
): DataWithColor[] {
61+
if (!data) return [];
62+
63+
return data
64+
.map((item) => ({
65+
name: item.name || "n/a",
66+
value: item.value,
67+
}))
68+
.sort((a, b) => b.value - a.value - (b.name === "n/a" ? 1 : 0))
69+
.map((item) => ({
70+
...item,
71+
color: LIST_COLORS[hashString(item.name) % LIST_COLORS.length],
72+
}));
73+
}

0 commit comments

Comments
 (0)