Skip to content

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

TypeScript

fix(mcp-host): give scatter and bubble marks room inside the plot

marcoteb716d50c98011
diff --git a/mcp-host/src/workflow/__tests__/internalTools.charts.rendering.test.ts b/mcp-host/src/workflow/__tests__/internalTools.charts.rendering.test.ts
index a0b341051..e320d94af 100644
--- a/mcp-host/src/workflow/__tests__/internalTools.charts.rendering.test.ts
+++ b/mcp-host/src/workflow/__tests__/internalTools.charts.rendering.test.ts
@@ -658,6 +658,11 @@ describe('axes and points', () => {
interface Drawn {
scales: Record<string, { min: number; max: number }>
datasets: Array<{ pointRadius?: unknown; clip?: unknown }>
+ /** Per axis: its tick values, and the pixels from each end of the plot to a value. */
+ axes: Record<
+ string,
+ { ticks: number[]; fromStart(v: number): number; fromEnd(v: number): number }
+ >
}
/** The scales and datasets of the chart `args` draws, as Chart.js drew them. */
@@ -665,11 +670,28 @@ describe('axes and points', () => {
const seen: Drawn[] = []
const draw = Chart.prototype.draw
const spy = vi.spyOn(Chart.prototype, 'draw').mockImplementation(function (this: Chart) {
+ const area = this.chartArea
seen.push({
scales: Object.fromEntries(
Object.entries(this.scales).map(([id, s]) => [id, { min: s.min, max: s.max }])
),
datasets: this.data.datasets as Drawn['datasets'],
+ axes: Object.fromEntries(
+ Object.entries(this.scales).map(([id, s]) => {
+ const pixel = s.getPixelForValue.bind(s)
+ const [start, end] = s.isHorizontal()
+ ? [area.left, area.right]
+ : [area.bottom, area.top]
+ return [
+ id,
+ {
+ ticks: s.ticks.map(t => t.value),
+ fromStart: (v: number) => Math.abs(pixel(v) - start),
+ fromEnd: (v: number) => Math.abs(end - pixel(v)),
+ },
+ ]
+ })
+ ),
})
return draw.call(this)
})
@@ -708,7 +730,7 @@ describe('axes and points', () => {
expect(mixed.scales.y.max).toBeGreaterThan(100)
})
- it('ends a scatter at its data and draws the points on the edge whole', async () => {
+ it('keeps the ticks of a scatter on its data and draws the points on the edge whole', async () => {
const chart = await drawn({
filename: 's.png',
type: 'scatter',
@@ -725,9 +747,59 @@ describe('axes and points', () => {
],
},
})
- expect(chart.scales.x).toEqual({ min: 0, max: 10 })
- expect(chart.scales.y.min).toBe(0)
- expect(chart.datasets[0].clip).toBe(false)
+ const { x, y } = chart.axes
+ expect([x.ticks[0], x.ticks[x.ticks.length - 1]]).toEqual([0, 10])
+ expect([y.ticks[0], y.ticks[y.ticks.length - 1]]).toEqual([0, 10])
+ // A point of 6 px with half its 1 px border fits inside the plot.
+ for (const axis of [x, y]) {
+ expect(axis.fromStart(0)).toBeGreaterThan(6.49)
+ expect(axis.fromEnd(10)).toBeGreaterThan(6.49)
+ }
+ expect(chart.datasets[0]).not.toHaveProperty('clip')
+ })
+
+ it('keeps a bubble whole inside the plot, and thins the ticks the room crowds', async () => {
+ const chart = await drawn({
+ filename: 'b.png',
+ type: 'bubble',
+ data: {
+ datasets: [
+ {
+ label: 'B',
+ data: [
+ { x: 0, y: 0, r: 20 },
+ { x: 10, y: 10, r: 20 },
+ ],
+ },
+ ],
+ },
+ })
+ for (const axis of Object.values(chart.axes)) {
+ expect(axis.ticks[0]).toBe(0)
+ expect(axis.fromStart(0)).toBeGreaterThan(20.49)
+ expect(axis.fromEnd(10)).toBeGreaterThan(20.49)
+ }
+ const crowded = await drawn({
+ filename: 'c.png',
+ type: 'bubble',
+ data: {
+ datasets: [
+ {
+ label: 'B',
+ data: [
+ { x: 0, y: 0, r: 90 },
+ { x: 50, y: 50, r: 90 },
+ ],
+ },
+ ],
+ },
+ })
+ // Room for a bubble past a quarter of the axis is not given; the ticks,
+ // spaced for the whole axis, keep every other one.
+ const y = crowded.axes.y
+ expect(y.ticks).toEqual([0, 10, 20, 30, 40, 50])
+ const gaps = y.ticks.slice(1).map((v, i) => y.fromStart(v) - y.fromStart(y.ticks[i]))
+ expect(Math.min(...gaps)).toBeGreaterThan(15)
})
it('draws the points of a dense series smaller, so they do not merge', async () => {
diff --git a/mcp-host/src/workflow/__tests__/internalTools.dashboard.page.test.ts b/mcp-host/src/workflow/__tests__/internalTools.dashboard.page.test.ts
index 4b44338a8..807831b12 100644
--- a/mcp-host/src/workflow/__tests__/internalTools.dashboard.page.test.ts
+++ b/mcp-host/src/workflow/__tests__/internalTools.dashboard.page.test.ts
@@ -72,13 +72,30 @@ interface LegendItem {
datasetIndex: number
}
+/** The part of a Chart.js scale the axis callbacks read and set. */
+interface FakeScale {
+ min: number
+ max: number
+ width?: number
+ height?: number
+ isHorizontal(): boolean
+ ticks: Array<{ value: number }>
+}
+
+interface AxisOptions {
+ beginAtZero?: boolean
+ grace?: string
+ afterDataLimits?: (scale: FakeScale) => void
+ afterBuildTicks?: (scale: FakeScale) => void
+}
+
interface ChartOptions {
animation?: boolean
plugins: { legend: { labels: { sort?: (a: LegendItem, b: LegendItem) => number } } }
scales?: {
r?: { ticks: unknown; grid: { color: string }; pointLabels: { color: string } }
- x?: { beginAtZero?: boolean; grace?: string }
- y?: { beginAtZero?: boolean; grace?: string }
+ x?: AxisOptions
+ y?: AxisOptions
}
}
@@ -260,7 +277,7 @@ describe('page script', () => {
expect(r.pointLabels.color).toBe('#cbd5e1')
})
- it('starts stacked value axes at zero and draws scatter points whole at the edge', async () => {
+ it('starts stacked value axes at zero and gives scatter points room at the edge', async () => {
const html = await page({
data: {
title: 'T',
@@ -290,8 +307,52 @@ describe('page script', () => {
expect(scales('chart-0').y!.beginAtZero).toBe(true)
expect(scales('chart-1').x!).not.toHaveProperty('grace')
expect(scales('chart-1').y!).not.toHaveProperty('grace')
- expect(run.charts.get('chart-1')!.config.data.datasets[0].clip).toBe(false)
+ expect(run.charts.get('chart-1')!.config.data.datasets[0]).not.toHaveProperty('clip')
expect(scales('chart-2').y!).not.toHaveProperty('beginAtZero')
+ expect(scales('chart-2').y!).not.toHaveProperty('afterBuildTicks')
+
+ // The x axis of the scatter, 1 to 3 with ticks every half, on 200 px: the
+ // default point, 3 px and 4 more when hovered, with its 1 px border, fits
+ // whole past the outermost value, and the ticks stay on the data.
+ const ticks = [1, 1.5, 2, 2.5, 3].map(value => ({ value }))
+ const axis: FakeScale = { min: 1, max: 3, width: 200, isHorizontal: () => true, ticks }
+ const x = scales('chart-1').x!
+ x.afterDataLimits!(axis)
+ x.afterBuildTicks!(axis)
+ const px = (value: number) => ((value - axis.min) / (axis.max - axis.min)) * 200
+ expect(px(1)).toBeCloseTo(8, 3)
+ expect(200 - px(3)).toBeCloseTo(8, 3)
+ expect(axis.ticks.map(t => t.value)).toEqual([1, 1.5, 2, 2.5, 3])
+ })
+
+ it('draws a bubble whole inside the plot, and thins the ticks the room crowds', async () => {
+ const html = await page({
+ data: {
+ title: 'T',
+ charts: [
+ {
+ type: 'bubble',
+ datasets: [
+ {
+ data: [
+ { x: 0, y: 0, r: 45 },
+ { x: 50, y: 50, r: 45 },
+ ],
+ },
+ ],
+ },
+ ],
+ },
+ })
+ const y = runPageScript(html, { vars: LIGHT }).charts.get('chart-0')!.config.options.scales!.y!
+ const ticks = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50].map(value => ({ value }))
+ const axis: FakeScale = { min: 0, max: 50, height: 200, isHorizontal: () => false, ticks }
+ y.afterDataLimits!(axis)
+ y.afterBuildTicks!(axis)
+ // 45 px, 4 more hovered and a 1 px border: capped at a quarter of the axis.
+ const px = (value: number) => ((value - axis.min) / (axis.max - axis.min)) * 200
+ expect(px(0)).toBeCloseTo(50, 3)
+ expect(axis.ticks.map(t => t.value)).toEqual([0, 10, 20, 30, 40, 50])
})
})
diff --git a/mcp-host/src/workflow/dashboardCharts.ts b/mcp-host/src/workflow/dashboardCharts.ts
index b58c3bae1..cc51e1192 100644
--- a/mcp-host/src/workflow/dashboardCharts.ts
+++ b/mcp-host/src/workflow/dashboardCharts.ts
@@ -50,6 +50,8 @@ export interface ClientChart {
xAxisLabel?: string
yAxisLabel?: string
gauge?: { value: string; max: string }
+ /** Radius in pixels of a scatter or bubble chart's largest mark, hovered. */
+ markRadius?: number
}
/** The number of series colors each theme defines (DashboardThemeColors.chart). */
@@ -349,14 +351,31 @@ function translate(
return { chart, warnings }
}
default:
- // A point or bubble at the end of an axis is drawn whole, past the plot edge.
- chart.datasets = datasets.map((ds, i) =>
- XY_TYPES.has(type) ? { ...styled(ds, i), clip: false } : styled(ds, i)
- )
+ chart.datasets = datasets.map((ds, i) => styled(ds, i))
+ if (XY_TYPES.has(type)) chart.markRadius = largestMark(type, datasets)
return { chart, warnings }
}
}
+// Chart.js draws a point 3 px across by default and grows a hovered mark by 4
+// px, with a border of 1 px.
+const POINT_RADIUS = 3
+const HOVER_GROWTH = 4
+const MARK_BORDER = 1
+
+/** Radius in pixels of the largest mark a scatter or bubble chart draws when hovered. */
+function largestMark(type: string, datasets: NormalizedDataset[]): number {
+ let largest = POINT_RADIUS
+ if (type === 'bubble') {
+ for (const ds of datasets) {
+ for (const point of ds.data as NormalizedPoint[]) {
+ if (point.r !== undefined && point.r > largest) largest = point.r
+ }
+ }
+ }
+ return largest + HOVER_GROWTH + MARK_BORDER
+}
+
function valueTable(type: string, labels: string[], datasets: NormalizedDataset[]): ValueTable {
if (XY_TYPES.has(type)) {
const points = Math.max(...datasets.map(ds => ds.data.length))
diff --git a/mcp-host/src/workflow/dashboardScript.ts b/mcp-host/src/workflow/dashboardScript.ts
index cb6897369..549b2f0f2 100644
--- a/mcp-host/src/workflow/dashboardScript.ts
+++ b/mcp-host/src/workflow/dashboardScript.ts
@@ -47,6 +47,52 @@ const PAGE_SCRIPT = String.raw`
});
}
+ // Room past the data for a scatter or bubble chart's marks: the axis runs on
+ // past its end ticks until a mark of the given radius on the outermost value
+ // is drawn whole inside the plot, clear of the tick labels, and the ticks stay
+ // where the data put them. Each axis takes its own pair of callbacks.
+ function markRoom(radius) {
+ var low = 0;
+ var high = 0;
+ return {
+ afterDataLimits: function (scale) {
+ low = scale.min;
+ high = scale.max;
+ },
+ afterBuildTicks: function (scale) {
+ var length = scale.isHorizontal() ? scale.width : scale.height;
+ var r = Math.min(radius, length / 4);
+ if (!(r > 0)) return;
+ var first = scale.min;
+ var last = scale.max;
+ var min = first;
+ var max = last;
+ for (var i = 0; i < 40; i++) {
+ var need = (r * (max - min)) / length;
+ var nextMin = Math.min(first, low - need);
+ var nextMax = Math.max(last, high + need);
+ if (nextMin === min && nextMax === max) break;
+ min = nextMin;
+ max = nextMax;
+ }
+ scale.min = min;
+ scale.max = max;
+ scale.ticks = spacedTicks(scale.ticks, (last - first) / (max - min));
+ },
+ };
+ }
+
+ // Ticks spaced for the whole axis, thinned to every other multiple of the
+ // doubled step when the room leaves them much less of it.
+ function spacedTicks(ticks, share) {
+ if (!(share < 0.75) || ticks.length < 3) return ticks;
+ var step = 2 * (ticks[1].value - ticks[0].value);
+ var kept = ticks.filter(function (t) {
+ return Math.abs(t.value / step - Math.round(t.value / step)) < 1e-6;
+ });
+ return kept.length >= 2 ? kept : ticks;
+ }
+
function options(spec) {
var text = cssVar('--text-muted');
var grid = cssVar('--border');
@@ -59,6 +105,7 @@ const PAGE_SCRIPT = String.raw`
};
// Set only where needed: an explicit false would override the bar charts' own zero baseline.
if (spec.stacked) out.beginAtZero = true;
+ if (spec.markRadius) Object.assign(out, markRoom(spec.markRadius));
return out;
}
var scales;
diff --git a/mcp-host/src/workflow/internalTools.ts b/mcp-host/src/workflow/internalTools.ts
index 1d9b2f82a..b6270e6be 100644
--- a/mcp-host/src/workflow/internalTools.ts
+++ b/mcp-host/src/workflow/internalTools.ts
@@ -417,6 +417,85 @@ function addHeadroom(scale: Scale): void {
if (set.min === undefined) scale.min = min >= 0 && min - room < 0 ? 0 : min - room
}
+/**
+ * Room past the data for the marks of a scatter or bubble chart, whose largest
+ * mark reaches `radius` pixels from its value. The axis runs on past its end
+ * ticks until a mark on the outermost value is drawn whole inside the plot,
+ * clear of the tick labels, while the ticks stay where the data put them, so
+ * an axis from 0 shows no -1. A bound set in the chart options stays as set.
+ * Each axis takes its own pair of callbacks.
+ */
+function markRoom(radius: number): {
+ afterDataLimits(scale: Scale): void
+ afterBuildTicks(scale: Scale): void
+} {
+ let low = 0
+ let high = 0
+ return {
+ afterDataLimits(scale) {
+ low = scale.min
+ high = scale.max
+ },
+ afterBuildTicks(scale) {
+ const length = scale.isHorizontal() ? scale.width : scale.height
+ // A mark wider than half the plot is not given room it cannot use.
+ const r = Math.min(radius, length / 4)
+ if (!(r > 0)) return
+ const set = scale.options as { min?: unknown; max?: unknown }
+ const first = scale.min
+ const last = scale.max
+ let min = first
+ let max = last
+ // The room a mark needs grows with the span it widens, so it is found by
+ // iteration; each step changes the span by at most half the last one.
+ for (let i = 0; i < 40; i++) {
+ const need = (r * (max - min)) / length
+ const nextMin = set.min === undefined ? Math.min(first, low - need) : first
+ const nextMax = set.max === undefined ? Math.max(last, high + need) : last
+ if (nextMin === min && nextMax === max) break
+ min = nextMin
+ max = nextMax
+ }
+ scale.min = min
+ scale.max = max
+ scale.ticks = spacedTicks(scale.ticks, (last - first) / (max - min))
+ },
+ }
+}
+
+/**
+ * `ticks` for an axis they now span only `share` of. Chart.js spaced them for
+ * the whole length, so when the room takes much of it every other one is
+ * dropped, keeping the multiples of the doubled step so the values stay round.
+ */
+function spacedTicks<T extends { value: number }>(ticks: T[], share: number): T[] {
+ if (!(share < 0.75) || ticks.length < 3) return ticks
+ const step = 2 * (ticks[1].value - ticks[0].value)
+ const kept = ticks.filter(t => Math.abs(t.value / step - Math.round(t.value / step)) < 1e-6)
+ return kept.length >= 2 ? kept : ticks
+}
+
+/** Radius in pixels of the largest mark a scatter or bubble chart draws, its border included. */
+function largestMark(chartType: string, datasets: ChartDataset[]): number {
+ const size = (value: unknown, fallback: number) =>
+ typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : fallback
+ let largest = 0
+ for (const ds of datasets) {
+ const d = ds as unknown as Record<string, unknown>
+ if (chartType === 'bubble') {
+ const border = size(d.borderWidth, 1) / 2
+ for (const point of ds.data as unknown[]) {
+ const r = (point as { r?: unknown } | null)?.r
+ largest = Math.max(largest, size(r, size(d.radius, 3)) + border)
+ }
+ } else {
+ const border = size(d.pointBorderWidth, 1) / 2
+ for (const r of [d.pointRadius ?? 3].flat()) largest = Math.max(largest, size(r, 0) + border)
+ }
+ }
+ return largest
+}
+
/**
* Default radius of a series' points: `full` up to `crowd` points, and smaller
* past that so a dense series keeps the ink of `crowd` points instead of
@@ -474,10 +553,6 @@ function applyThemePalette(
}
const point = out as unknown as Record<string, unknown>
const points = Array.isArray(ds.data) ? ds.data.length : 0
- // An axis ends at a round value or at zero, where a point or bubble would be cut.
- if ((chartType === 'scatter' || chartType === 'bubble') && point.clip === undefined) {
- point.clip = false
- }
if (chartType === 'scatter') {
if (point.pointRadius === undefined) point.pointRadius = pointRadius(6, 100, points)
} else if (chartType === 'line' || chartType === 'area' || chartType === 'stackedArea') {
@@ -1029,6 +1104,8 @@ const generateChart: InternalToolDefinition = {
},
}
const headroom = { afterDataLimits: addHeadroom }
+ const marks = chartTypeRaw === 'scatter' || chartTypeRaw === 'bubble'
+ const radius = marks ? largestMark(chartTypeRaw, themedDatasets) : 0
const config: ChartConfiguration = {
type: chartType,
@@ -1078,8 +1155,7 @@ const generateChart: InternalToolDefinition = {
...(indexAxis === 'y' ? valueTick : {}),
},
grid: { color: theme.gridColor },
- // A bubble reaches its radius past its value, on either axis.
- ...(indexAxis === 'y' || chartTypeRaw === 'bubble' ? headroom : {}),
+ ...(marks ? markRoom(radius) : indexAxis === 'y' ? headroom : {}),
title: xAxisLabel
? {
display: true,
@@ -1096,7 +1172,7 @@ const generateChart: InternalToolDefinition = {
...(indexAxis === 'x' ? valueTick : {}),
},
grid: { color: theme.gridColor },
- ...(indexAxis === 'x' ? headroom : {}),
+ ...(marks ? markRoom(radius) : indexAxis === 'x' ? headroom : {}),
title: yAxisLabel
? {
display: true,