Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(mcp-host): draw a bar's border color as its outline, and other chart gaps

marcoteb436c07484386
diff --git a/mcp-host/src/workflow/__tests__/dashboardCharts.table.test.ts b/mcp-host/src/workflow/__tests__/dashboardCharts.table.test.ts
index fdb37cb15..86f454d83 100644
--- a/mcp-host/src/workflow/__tests__/dashboardCharts.table.test.ts
+++ b/mcp-host/src/workflow/__tests__/dashboardCharts.table.test.ts
@@ -50,10 +50,27 @@ describe('the table shown in place of a chart', () => {
)
expect(prepared.table).toEqual({
labels: ['Start', 'Sales', 'Costs', 'Total'],
- series: [{ label: 'Cash', values: ['0.1', '0.2', '-0.125', '0.18'] }],
+ series: [{ label: 'Cash', values: ['0.1', '0.2', '-0.125', '0.175'] }],
})
})
+ it('reads the first number a gauge was sent, as the PNG gauge does', () => {
+ const prepared = prepareDashboardChart(
+ { type: 'gauge', labels: ['CPU'], datasets: [{ data: [null, 50] }] },
+ 'charts[0]'
+ )
+ expect(prepared.table?.series[0].values).toEqual(['50', '100'])
+ expect(prepared.warnings.join(' ')).not.toContain('left out')
+ })
+
+ it('heads an unlabeled gauge with no made-up position', () => {
+ const prepared = prepareDashboardChart(
+ { type: 'gauge', datasets: [{ label: 'Load', data: [40] }] },
+ 'charts[0]'
+ )
+ expect(prepared.table?.labels).toEqual(['', 'Max'])
+ })
+
it('keeps every series of an ordinary chart', () => {
const prepared = prepareDashboardChart(
{
diff --git a/mcp-host/src/workflow/__tests__/seriesPaint.parity.test.ts b/mcp-host/src/workflow/__tests__/seriesPaint.parity.test.ts
index 748ebbe58..c2fea6a51 100644
--- a/mcp-host/src/workflow/__tests__/seriesPaint.parity.test.ts
+++ b/mcp-host/src/workflow/__tests__/seriesPaint.parity.test.ts
@@ -85,6 +85,25 @@ function pngRoles(kind: string, own: Own) {
})
}
+/** The first dataset each output draws. */
+function pngDataset(kind: string, own: Own): Record<string, unknown> {
+ const normalized = normalizeChartData(chartData(kind, own), { chartType: kind })
+ const { datasets } = paintedDatasets(
+ kind,
+ normalized.datasets as never,
+ normalized.labels,
+ theme,
+ {},
+ []
+ )
+ return datasets[0] as unknown as Record<string, unknown>
+}
+
+function dashboardDataset(kind: string, own: Own): Record<string, unknown> {
+ return prepareDashboardChart({ type: kind, ...chartData(kind, own) }, 'charts[0]').chart!
+ .datasets[0]
+}
+
function dashboardRoles(kind: string, own: Own) {
const prepared = prepareDashboardChart({ type: kind, ...chartData(kind, own) }, 'charts[0]')
expect(prepared.error).toBeUndefined()
@@ -149,14 +168,36 @@ describe('how a series is painted', () => {
}
})
- it("fills a bar with the caller's color, which an outline would not show", () => {
- const own = { borderColor: '#f00' }
- for (const each of [pngRoles('bar', own), dashboardRoles('bar', own)]) {
- expect(each[0]).toEqual({
- border: 'own borderColor',
- fill: 'own borderColor',
- points: 'none',
- })
+ it("draws a caller's bar color as the outline it is, over the palette fill", () => {
+ // A white or transparent border must not hide the bar it outlines.
+ for (const borderColor of ['#f00', '#ffffff', 'transparent']) {
+ const own = { borderColor }
+ for (const each of [pngRoles('stackedBar', own), dashboardRoles('stackedBar', own)]) {
+ expect(each[0]).toEqual({ border: 'own borderColor', fill: 'palette', points: 'none' })
+ }
+ expect(pngDataset('stackedBar', own).borderWidth).toBe(2)
+ expect(dashboardDataset('stackedBar', own).borderWidth).toBe(2)
+ }
+ expect(pngDataset('bar', {}).borderWidth).toBeUndefined()
+ expect(dashboardDataset('bar', {}).borderWidth).toBeUndefined()
+ })
+
+ it('reads an empty borderColor list as no color', () => {
+ const own = { borderColor: [] as string[] }
+ for (const kind of ['bar', 'scatter', 'line'] as const) {
+ expect(pngRoles(kind, own)[0].fill).toBe(kind === 'line' ? 'palette tint' : 'palette')
+ expect(dashboardRoles(kind, own)[0].fill).toBe(kind === 'line' ? 'palette tint' : 'palette')
+ }
+ })
+
+ it('wraps a short color list over a fill list with gaps, as Chart.js wraps it', () => {
+ const own = {
+ borderColor: ['#ff0000', '#00ff00'],
+ backgroundColor: ['#0000ff', '#0000ff', 'brand'],
+ }
+ // A stacked area is filled in the series color, so its gap takes the wrapped border color.
+ for (const each of [pngDataset('stackedArea', own), dashboardDataset('stackedArea', own)]) {
+ expect(each.backgroundColor).toEqual(['#0000ff', '#0000ff', '#ff0000'])
}
})
@@ -194,6 +235,18 @@ describe('how a series is painted', () => {
expect(warnings.join(' ')).toContain('"brand-blue" is not a CSS color')
})
+ it('does not call a full list of slice colors short when one is not a CSS color', () => {
+ const warnings: string[] = []
+ const data = {
+ labels: ['A', 'B', 'C'],
+ datasets: [{ data: [1, 2, 3], backgroundColor: ['#f00', 'brand', '#00f'] }],
+ }
+ const normalized = normalizeChartData(data, { chartType: 'pie' })
+ paintedDatasets('pie', normalized.datasets as never, normalized.labels, theme, {}, warnings)
+ expect(warnings.join(' ')).toContain('"brand" is not a CSS color')
+ expect(warnings.join(' ')).not.toContain('color(s) for 3 slices')
+ })
+
it('falls back to the palette tint for a color chart.js cannot read', () => {
const own = { borderColor: 'oklch(0.6 0.2 30)' }
for (const each of [pngRoles('line', own), dashboardRoles('line', own)]) {
diff --git a/mcp-host/src/workflow/chartDatasets.ts b/mcp-host/src/workflow/chartDatasets.ts
index 9d8a1ee53..a23eff4a2 100644
--- a/mcp-host/src/workflow/chartDatasets.ts
+++ b/mcp-host/src/workflow/chartDatasets.ts
@@ -87,7 +87,9 @@ function applyThemePalette(
out.borderColor = out.borderColor.map(c => c ?? theme.backgroundColor)
}
const where = `data.datasets[${idx}]`
- if (padded) {
+ // Only a list shorter than the slices is short; an entry left empty
+ // for not being a CSS color has a warning of its own.
+ if (padded && requested!.length < ds.data.length) {
warnings.push(
`${where}.backgroundColor had ${requested!.length} color(s) for ${ds.data.length} ` +
'slices; the rest were taken from the theme. Send one color per slice.'
@@ -118,6 +120,8 @@ function applyThemePalette(
if (paint.pointBackgroundColor && point.pointBackgroundColor === undefined) {
point.pointBackgroundColor = paint.pointBackgroundColor
}
+ if (paint.borderWidth && point.borderWidth === undefined)
+ point.borderWidth = paint.borderWidth
const points = Array.isArray(ds.data) ? ds.data.length : 0
if (chartType === 'scatter') {
if (point.pointRadius === undefined) point.pointRadius = pointRadius(6, 100, points)
diff --git a/mcp-host/src/workflow/dashboardCharts.ts b/mcp-host/src/workflow/dashboardCharts.ts
index 792f16fd7..aafc3af5f 100644
--- a/mcp-host/src/workflow/dashboardCharts.ts
+++ b/mcp-host/src/workflow/dashboardCharts.ts
@@ -101,6 +101,7 @@ function seriesColors(ds: NormalizedDataset, index: number, type: string): Recor
borderColor: filled(ds.borderColor, paint.borderColor, color),
backgroundColor: filled(ds.backgroundColor, paint.backgroundColor, color),
...(paint.pointBackgroundColor ? { pointBackgroundColor: paint.pointBackgroundColor } : {}),
+ ...(paint.borderWidth ? { borderWidth: paint.borderWidth } : {}),
...dashed,
}
}
@@ -129,6 +130,11 @@ function readout(n: number): string {
return Number.isInteger(n) ? String(n) : String(Number(n.toFixed(2)))
}
+/** `n` at full precision, without the noise of adding binary fractions (0.1 + 0.2). */
+function plainNumber(n: number): string {
+ return String(Number(n.toPrecision(12)))
+}
+
function firstSeriesValues(ds: NormalizedDataset | undefined): number[] {
return ((ds?.data ?? []) as Array<number | null>).map(v => (typeof v === 'number' ? v : 0))
}
@@ -237,7 +243,7 @@ function translate(
const table = {
labels: chart.labels,
series: [
- { label: datasets[0]?.label ?? '', values: [...deltas.map(String), readout(total)] },
+ { label: datasets[0]?.label ?? '', values: [...deltas.map(String), plainNumber(total)] },
],
}
chart.datasets = [
@@ -259,7 +265,10 @@ function translate(
if (max !== requestedMax && spec.gaugeMax !== undefined && spec.gaugeMax !== null) {
warnings.push(`gaugeMax ${String(spec.gaugeMax)} is not a positive number; used 100.`)
}
- const values = firstSeriesValues(datasets[0])
+ // The first number sent, as the PNG gauge reads it; a gap is no reading.
+ const values = ((datasets[0]?.data ?? []) as Array<number | null>).filter(
+ (v): v is number => typeof v === 'number'
+ )
const raw = values[0] ?? 0
if (values.length > 1) {
warnings.push(
@@ -286,7 +295,8 @@ function translate(
]
chart.gauge = { value: readout(raw), max: readout(max) }
const table = {
- labels: [chart.labels[0], 'Max'],
+ // A gauge sent with no labels has only a numbered position, which names nothing.
+ labels: [Array.isArray(spec.labels) ? chart.labels[0] : '', 'Max'],
series: [{ label: datasets[0]?.label ?? '', values: [chart.gauge.value, chart.gauge.max] }],
}
return { chart, warnings, table }
diff --git a/mcp-host/src/workflow/seriesPaint.ts b/mcp-host/src/workflow/seriesPaint.ts
index 271d04bca..37c3973b8 100644
--- a/mcp-host/src/workflow/seriesPaint.ts
+++ b/mcp-host/src/workflow/seriesPaint.ts
@@ -10,6 +10,18 @@ import { isCssColor } from './chartColors'
/** Fills that lie over one another, drawn as a tint of the series color. */
const TINTED_FILL = new Set(['line', 'area', 'radar'])
+/** Marks filled in the series color: scatter points and stacked areas. */
+const SERIES_FILL = new Set(['scatter', 'stackedArea'])
+
+/**
+ * Bars, whose borderColor is their outline, as the schemas say; Chart.js draws
+ * a bar with no outline by default, so a caller's border gets a width.
+ */
+const OUTLINED = new Set(['bar', 'horizontalBar', 'stackedBar', 'funnel'])
+
+/** The width a caller's bar outline is drawn at. */
+const OUTLINE_WIDTH = 2
+
/** Lines and webs whose values are point marks in the series color, not the tint. */
const POINT_MARKS = new Set(['line', 'area', 'stackedArea', 'radar'])
@@ -30,6 +42,8 @@ export interface SeriesDefaults {
backgroundColor: SeriesColors
/** Set only for kinds that draw point marks. */
pointBackgroundColor?: SeriesColors
+ /** Set only for a bar whose caller sent a border color. */
+ borderWidth?: number
}
/** A caller's color at TINT_ALPHA, or undefined when chart.js cannot read it. */
@@ -44,10 +58,10 @@ export function tintOf(css: string): string | undefined {
* caller's borderColor, one color or one per value, else the palette's; a
* value with no usable color of its own takes the palette's. Fills that lie
* over one another (line, area, radar) are the tint of a single series color,
- * the palette's tint otherwise or when chart.js cannot read the color; every
- * other mark, a bar, a bubble, a scatter point or a stacked area, is filled in
- * the series color, so a caller's color shows (a bar's outline is not drawn).
- * Points on lines and webs take the series color.
+ * the palette's tint otherwise or when chart.js cannot read the color; points
+ * on lines and webs, scatter points and stacked areas take the series color.
+ * A bar or a bubble keeps the palette's fill, and the caller's color is its
+ * outline, drawn for a bar at OUTLINE_WIDTH.
*/
export function seriesDefaults(
kind: string,
@@ -56,24 +70,30 @@ export function seriesDefaults(
): SeriesDefaults {
const own = ds.borderColor
const single = typeof own === 'string' && isCssColor(own) ? own : undefined
- const perValue = Array.isArray(own)
- ? own.map(c => (typeof c === 'string' && isCssColor(c) ? c : palette.color))
- : undefined
+ const perValue =
+ Array.isArray(own) && own.length > 0
+ ? own.map(c => (typeof c === 'string' && isCssColor(c) ? c : palette.color))
+ : undefined
const seriesColor: SeriesColors = single ?? perValue ?? palette.color
const backgroundColor = TINTED_FILL.has(kind)
? ((single !== undefined ? tintOf(single) : undefined) ?? palette.tint)
- : seriesColor
+ : SERIES_FILL.has(kind)
+ ? seriesColor
+ : palette.color
return {
borderColor: palette.color,
backgroundColor,
...(POINT_MARKS.has(kind) ? { pointBackgroundColor: seriesColor } : {}),
+ ...(OUTLINED.has(kind) && (single ?? perValue) !== undefined
+ ? { borderWidth: OUTLINE_WIDTH }
+ : {}),
}
}
/**
* `value`, with `fallback` where it sets no color: for the whole series when
* it is unset, and for each empty entry of a per-value list, which takes the
- * fallback's color for that value, else `last`.
+ * fallback's color for that value (a short fallback list wraps), else `last`.
*/
export function filled(
value: string | Array<string | undefined> | undefined,
@@ -82,7 +102,16 @@ export function filled(
): SeriesColors {
if (value === undefined || value === '') return fallback
if (!Array.isArray(value)) return value
- return value.map((c, i) => c ?? (Array.isArray(fallback) ? (fallback[i] ?? last) : fallback))
+ // A short fallback list wraps over the values, as Chart.js wraps it.
+ return value.map(
+ (c, i) =>
+ c ??
+ (Array.isArray(fallback)
+ ? fallback.length > 0
+ ? fallback[i % fallback.length]
+ : last
+ : fallback)
+ )
}
/** The kind each dataset of a `chartType` chart is painted as. */