Skip to content
Open
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
22 changes: 11 additions & 11 deletions frontend/src/apexcharts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,27 @@ const sqlpage_chart = (() => {
["black", "#000000", "#000000"],
["white", "#ffffff", "#f8f9fa"],
];
const colorNames = Object.fromEntries(
tblrColors.flatMap(([name, dark, light]) => [
const colorNames = new Map(
tblrColors.flatMap(([name, dark, light]): [string, string][] => [
[name, dark],
[`${name}-lt`, light],
]),
);
const isDarkTheme = document.body?.dataset?.bsTheme === "dark";

const STACKABLE_CHART_TYPES = ["line", "area", "bar"];
const APEXCHARTS_TYPE_ALIASES = { column: "bar" };
const STROKE_WIDTHS = new Map([
["area", 3],
["line", 2],
]);
const APEXCHARTS_TYPE_ALIASES = new Map([["column", "bar"]]);

const referenceColor = colorNames[isDarkTheme ? "gray-lt" : "gray"];
const referenceColor = colorNames.get(isDarkTheme ? "gray-lt" : "gray");

type ReferenceLine = { [property: string]: string | number | null };

const named_color = (name: unknown): string | undefined =>
typeof name === "string" ? colorNames[name] : undefined;
typeof name === "string" ? colorNames.get(name) : undefined;

const reference_color = (name: string | number | null) =>
named_color(name) || referenceColor;
Expand Down Expand Up @@ -100,7 +104,7 @@ const sqlpage_chart = (() => {
chartContainer.innerHTML = "";
const is_timeseries = !!data.time;
const chart_type =
APEXCHARTS_TYPE_ALIASES[data.type] || data.type || "line";
APEXCHARTS_TYPE_ALIASES.get(data.type) || data.type || "line";
const is_stacked =
!!data.stacked && STACKABLE_CHART_TYPES.includes(chart_type);
const points = data.points.filter(Array.isArray);
Expand Down Expand Up @@ -219,11 +223,7 @@ const sqlpage_chart = (() => {
type: chart_type === "area" ? "gradient" : "solid",
},
stroke: {
width:
{
area: 3,
line: 2,
}[chart_type] || 0,
width: STROKE_WIDTHS.get(chart_type) ?? 0,
lineCap: "round",
curve: "smooth",
},
Expand Down
21 changes: 11 additions & 10 deletions frontend/src/chart_series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ export type Series = Map<string, ChartSeries>;

const NUMERIC_X_CHART_TYPES = ["line", "area", "bar", "scatter", "bubble"];

const Y_WHEN_A_SERIES_SKIPS_A_LABEL: Record<string, number | null> = {
bar: 0,
line: null,
area: null,
scatter: null,
bubble: null,
heatmap: null,
};
const Y_WHEN_A_SERIES_SKIPS_A_LABEL = new Map<string, number | null>([
["bar", 0],
["line", null],
["area", null],
["scatter", null],
["bubble", null],
["heatmap", null],
]);

/** equal x values share a key */
const x_key = (x: XValue): number | string =>
Expand Down Expand Up @@ -91,7 +91,8 @@ export function align_series_for(
is_stacked: boolean,
): ChartSeries[] {
if (is_stacked) return align_series(series, 0);
if (x_is_text(series) && chart_type in Y_WHEN_A_SERIES_SKIPS_A_LABEL)
return align_series(series, Y_WHEN_A_SERIES_SKIPS_A_LABEL[chart_type]);
const y_when_missing = Y_WHEN_A_SERIES_SKIPS_A_LABEL.get(chart_type);
if (x_is_text(series) && y_when_missing !== undefined)
return align_series(series, y_when_missing);
return series;
}
2 changes: 2 additions & 0 deletions tests/end-to-end/fixtures/chart/builtin-chart-color.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT 'chart' AS component, 'test-chart' AS id, 'Chart test fixture' AS title, 'bar' AS type, 'toString' AS color, 4 AS marker;
WITH points(series, x, y) AS (VALUES ('A', 'Q1', 1), ('A', 'Q2', 2)) SELECT * FROM points;
10 changes: 10 additions & 0 deletions tests/end-to-end/fixtures/chart/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,16 @@ test("keeps the default palette when the chart names a color SQLPage does not kn
expect(fills(unknown)).toEqual(fills(plain));
});

test("draws a chart whose color names a built-in JavaScript property", async ({
page,
}) => {
const plain = await renderChart(page, "uncolored-bar");
const chart = await renderChart(page, "builtin-chart-color");

expect(chart.failures).toEqual([]);
expect(fills(chart)).toEqual(fills(plain));
});

test("renders series named after built-in JavaScript properties", async ({
page,
}) => {
Expand Down
Loading