Skip to content

Commit 4d8442b

Browse files
committed
Fix show_legend for SQLite boolean values
1 parent 375df59 commit 4d8442b

3 files changed

Lines changed: 46 additions & 1 deletion

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
SELECT
2+
'chart' AS component,
3+
'Legend Hidden' AS title,
4+
'line' AS type,
5+
FALSE AS show_legend;
6+
7+
SELECT 'Marketing' AS series, 2023 AS x, 30 AS y
8+
UNION ALL
9+
SELECT 'Marketing', 2024, 45
10+
UNION ALL
11+
SELECT 'Sales', 2023, 35
12+
UNION ALL
13+
SELECT 'Sales', 2024, 50;

sqlpage/apexcharts.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
/* !include https://cdn.jsdelivr.net/npm/apexcharts@5.3.6/dist/apexcharts.min.js */
22

33
sqlpage_chart = (() => {
4+
/**
5+
* Coerces SQL values to booleans.
6+
* SQLite often returns booleans as 0/1 integers.
7+
* @param {unknown} value
8+
* @param {boolean} defaultValue
9+
* @returns {boolean}
10+
*/
11+
function sqlBoolean(value, defaultValue = false) {
12+
if (value == null) return defaultValue;
13+
if (typeof value === "boolean") return value;
14+
if (typeof value === "number") return value !== 0;
15+
if (typeof value === "string") {
16+
const normalized = value.trim().toLowerCase();
17+
if (normalized === "0" || normalized === "false") return false;
18+
if (normalized === "1" || normalized === "true") return true;
19+
}
20+
return Boolean(value);
21+
}
22+
423
function sqlpage_chart() {
524
for (const c of document.querySelectorAll("[data-pre-init=chart]")) {
625
try {
@@ -156,7 +175,7 @@ sqlpage_chart = (() => {
156175
palette: "palette4",
157176
},
158177
legend: {
159-
show: data.show_legend !== false,
178+
show: sqlBoolean(data.show_legend, true),
160179
},
161180
dataLabels: {
162181
enabled: !!data.labels,

tests/end-to-end/official-site.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ test("chart supports hiding legend", async ({ page }) => {
3535
await expect(expensesChart.locator(".apexcharts-legend")).toBeHidden();
3636
});
3737

38+
test("chart supports hiding legend with SQL boolean false", async ({
39+
page,
40+
}) => {
41+
await page.goto(`${BASE}/examples/chart_hide_legend.sql`);
42+
43+
const chart = page.locator(".card", {
44+
has: page.getByRole("heading", { name: "Legend Hidden" }),
45+
});
46+
47+
await expect(chart.locator(".apexcharts-canvas")).toBeVisible();
48+
await expect(chart.locator(".apexcharts-legend")).toBeHidden();
49+
});
50+
3851
test("map", async ({ page }) => {
3952
await page.goto(`${BASE}/documentation.sql?component=map#component`);
4053
await expect(page.getByText("Loading...")).not.toBeVisible();

0 commit comments

Comments
 (0)