Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/viewer/src/charts/default_charts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface DefaultChartsConfig {
table?: boolean | Partial<InstancesSpec>;

/** Set to false to disable the embedding view, or an object to override spec properties */
embedding?: boolean | Partial<EmbeddingSpec>;
embedding?: boolean | (Partial<Omit<EmbeddingSpec, "data">> & { data?: Partial<EmbeddingSpec["data"]> });
}

/** Returns a list of default charts for a given data table. */
Expand Down Expand Up @@ -56,8 +56,15 @@ export async function defaultCharts(options: {
neighbors: projection.neighbors,
},
};
if (typeof config.embedding == "object") {
spec = { ...spec, ...config.embedding };
if (config.embedding != null && typeof config.embedding == "object") {
spec = {
...spec,
...config.embedding,
data: {
...spec.data,
...config.embedding.data,
},
};
}
charts.push(spec);
exclude.push(projection.x);
Expand Down
78 changes: 78 additions & 0 deletions packages/viewer/test/default_charts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) 2026 Apple Inc. Licensed under MIT License.

import type { Coordinator } from "@uwdata/mosaic-core";
import { describe, expect, test } from "vitest";

import { defaultCharts } from "../src/charts/default_charts.js";

const mockCoordinator = {
query: async () => [],
} as unknown as Coordinator;

describe("defaultCharts", () => {
test("preserves projection data fields when config.embedding overrides partial data properties", async () => {
const charts = await defaultCharts({
coordinator: mockCoordinator,
table: "items",
id: "id",
projection: {
x: "proj_x",
y: "proj_y",
text: "text_col",
image: "img_col",
importance: "imp_col",
neighbors: "nbr_col",
},
config: {
embedding: {
title: "Custom Embedding",
mode: "density",
data: {
category: "label",
text: "override_text",
},
},
},
});

const embedding = charts.find((c) => c.type === "embedding");
expect(embedding).toEqual({
type: "embedding",
title: "Custom Embedding",
mode: "density",
data: {
x: "proj_x",
y: "proj_y",
text: "override_text",
image: "img_col",
importance: "imp_col",
neighbors: "nbr_col",
category: "label",
},
});
});

test("ignores null embedding override at runtime and respects false", async () => {
const withNull = await defaultCharts({
coordinator: mockCoordinator,
table: "items",
id: "id",
projection: { x: "x", y: "y" },
config: {
embedding: null as unknown as undefined,
},
});
expect(withNull.some((c) => c.type === "embedding")).toBe(true);

const disabled = await defaultCharts({
coordinator: mockCoordinator,
table: "items",
id: "id",
projection: { x: "x", y: "y" },
config: {
embedding: false,
},
});
expect(disabled.some((c) => c.type === "embedding")).toBe(false);
});
});
Loading