Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Mar 10, 2025
1 parent 33a6082 commit d61e4c5
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions lib/support/Chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,8 @@ export type ChartColor = keyof typeof chartColors | (string & {})
export type KV = { key: string; value: number; color?: ChartColor }

export function getColor(...colors: (ChartColor | string | null | undefined)[]): string {
for (const color of colors) {
if (color != null) {
return chartColors[color as keyof typeof chartColors] || color
}
}
return chartColors.blue
const color = colors.find((color) => color != null)
return chartColors[color as keyof typeof chartColors] || color || chartColors.blue
}

const { gray: _gray, ...rest } = chartColors
Expand All @@ -59,11 +55,10 @@ export function scheme<T extends { key: string; color?: ChartColor }>(
): (d: T) => string {
const map = new Map<string, string>()

for (let i = 0; i < data.length; i++) {
const d = data[i]
data.forEach((d, i) => {
const color = getColor(d.color, colors?.[i % (colors?.length ?? 1)])
map.set(d.key, color)
}
})

return (d: T): string => {
return map.get(d.key) ?? unknown
Expand Down

0 comments on commit d61e4c5

Please sign in to comment.