feat: item color map, fix regex matching, fix sort order, fix resize handle
This commit is contained in:
@@ -1,19 +1,8 @@
|
||||
import uPlot from 'uplot';
|
||||
import type { ChartConfig } from '@/lib/types';
|
||||
import { formatSI } from '@/lib/formatNumber';
|
||||
|
||||
// --- Color helpers ---
|
||||
|
||||
function djb2(s: string): number {
|
||||
let h = 5381;
|
||||
for (let i = 0; i < s.length; i++) h = (h * 33) ^ s.charCodeAt(i);
|
||||
return h >>> 0;
|
||||
}
|
||||
|
||||
function hslColor(key: string): string {
|
||||
const hue = djb2(key) % 360;
|
||||
return `hsl(${hue},70%,60%)`;
|
||||
}
|
||||
import type { ColorMap } from '@/lib/colors';
|
||||
import { getItemColor } from '@/lib/colors';
|
||||
|
||||
const SEMANTIC_GREEN = '#4ade80';
|
||||
const SEMANTIC_RED = '#f87171';
|
||||
@@ -24,10 +13,11 @@ export interface SeriesStyle {
|
||||
}
|
||||
|
||||
export function getSeriesStyle(
|
||||
key: string,
|
||||
uCombs: number,
|
||||
uItems: number,
|
||||
uSigs: number,
|
||||
key: string,
|
||||
uCombs: number,
|
||||
uItems: number,
|
||||
uSigs: number,
|
||||
colorMap: ColorMap = new Map(),
|
||||
): SeriesStyle {
|
||||
const [combinator, item_key, sig] = key.split('::');
|
||||
|
||||
@@ -35,9 +25,9 @@ export function getSeriesStyle(
|
||||
return { color: sig === 'green' ? SEMANTIC_GREEN : SEMANTIC_RED, dash: undefined };
|
||||
}
|
||||
if (uItems > 1) {
|
||||
return { color: hslColor(item_key), dash: uSigs > 1 && sig === 'red' ? [4, 2] : undefined };
|
||||
return { color: getItemColor(item_key, colorMap), dash: uSigs > 1 && sig === 'red' ? [4, 2] : undefined };
|
||||
}
|
||||
return { color: hslColor(combinator), dash: uSigs > 1 && sig === 'red' ? [4, 2] : undefined };
|
||||
return { color: getItemColor(combinator, colorMap), dash: uSigs > 1 && sig === 'red' ? [4, 2] : undefined };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -137,6 +127,7 @@ export function makeSignalsSeries(
|
||||
keys: string[],
|
||||
timeMode: 'real' | 'tick',
|
||||
resolveName: (key: string) => string,
|
||||
colorMap: ColorMap = new Map(),
|
||||
): uPlot.Series[] {
|
||||
const uCombs = new Set(keys.map(k => k.split('::')[0])).size;
|
||||
const uItems = new Set(keys.map(k => k.split('::')[1])).size;
|
||||
@@ -154,7 +145,7 @@ export function makeSignalsSeries(
|
||||
xSeries,
|
||||
...keys.map(k => {
|
||||
const [, item_key] = k.split('::');
|
||||
const { color, dash } = getSeriesStyle(k, uCombs, uItems, uSigs);
|
||||
const { color, dash } = getSeriesStyle(k, uCombs, uItems, uSigs, colorMap);
|
||||
return {
|
||||
label: getSeriesLabel(k, uCombs, uItems, uSigs, resolveName(item_key)),
|
||||
stroke: color,
|
||||
|
||||
Reference in New Issue
Block a user