From 2f29f7b0fe5bc2620c1d3901573cd871908df688 Mon Sep 17 00:00:00 2001 From: Jason Smart Date: Fri, 3 Jan 2025 08:10:18 -0800 Subject: [PATCH] Adds support for boolean time series data, Signed-off-by: Jason Smart --- .../src/component/render-result-metadata.ts | 11 +++++++ packages/malloy-render/src/component/types.ts | 2 +- packages/malloy-render/src/component/util.ts | 4 +++ .../src/stories/line_charts.stories.malloy | 30 ++++++++++++++----- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/packages/malloy-render/src/component/render-result-metadata.ts b/packages/malloy-render/src/component/render-result-metadata.ts index 89b31ee9e..6d5628bb2 100644 --- a/packages/malloy-render/src/component/render-result-metadata.ts +++ b/packages/malloy-render/src/component/render-result-metadata.ts @@ -35,6 +35,7 @@ import { getFieldKey, valueIsDateTime, valueIsNumber, + valueIsBoolean, valueIsString, } from './util'; import { @@ -187,6 +188,16 @@ const populateFieldMeta = (data: DataArray, metadata: RenderResultMetadata) => { fieldMeta.values.add(n); fieldSet.add(n); } + } else if (valueIsBoolean(f, value)) { + const bool:boolean = value; + if (f.isAtomicField() && f.sourceWasDimension()) { + fieldMeta.values.add(bool); + fieldSet.add(bool); + } + if (!fieldMeta.minString || fieldMeta.minString.length > 4) + fieldMeta.minString = "true"; + if (!fieldMeta.maxString || fieldMeta.maxString.length < 5) + fieldMeta.maxString = "false"; } else if (valueIsString(f, value)) { const s = value; fieldMeta.values.add(s); diff --git a/packages/malloy-render/src/component/types.ts b/packages/malloy-render/src/component/types.ts index 6dedb560b..846e24421 100644 --- a/packages/malloy-render/src/component/types.ts +++ b/packages/malloy-render/src/component/types.ts @@ -52,7 +52,7 @@ export interface FieldRenderMetadata { max: number | null; minString: string | null; maxString: string | null; - values: Set; + values: Set; maxRecordCt: number | null; maxUniqueFieldValueCounts: Map; vegaChartProps?: VegaChartProps; diff --git a/packages/malloy-render/src/component/util.ts b/packages/malloy-render/src/component/util.ts index 54b61004c..63c487bca 100644 --- a/packages/malloy-render/src/component/util.ts +++ b/packages/malloy-render/src/component/util.ts @@ -41,6 +41,10 @@ export function valueIsNumber(f: Field, v: unknown): v is number { return f.isAtomicField() && f.isNumber() && v !== null; } +export function valueIsBoolean(f: Field, v: unknown): v is boolean { + return f.isAtomicField() && f.isBoolean() && v !== null; +} + export function valueIsString(f: Field, s: unknown): s is string { return f.isAtomicField() && f.isString() && s !== null; } diff --git a/packages/malloy-render/src/stories/line_charts.stories.malloy b/packages/malloy-render/src/stories/line_charts.stories.malloy index fba9ac23b..815ff3a36 100644 --- a/packages/malloy-render/src/stories/line_charts.stories.malloy +++ b/packages/malloy-render/src/stories/line_charts.stories.malloy @@ -242,7 +242,7 @@ source: products is duckdb.table("static/data/products.parquet") extend { product_count_by_gender is count(id) #(story) - view: line_chart_with_boolean is { + view: line_chart_boolean_xaxis is { nest: product_sales_by_gender is { group_by: is_female aggregate: product_count_by_gender @@ -255,7 +255,7 @@ source: products is duckdb.table("static/data/products.parquet") extend { } dimension: - `the_date` is @2001-02-03 + id day + `date_of_sale` is @2001-02-03 + id day bool_series is pick true when brand = 'Lucky Brand' pick false when brand = 'Calvin Klein' else true @@ -265,17 +265,33 @@ source: products is duckdb.table("static/data/products.parquet") extend { #(story) # line_chart - view: product_brand_broken is { + view: line_chart_time_series is { # x - group_by: the_date + group_by: date_of_sale # series - group_by: text_series_with_null + group_by: brand + + # y + aggregate: `Sales $` is retail_price.avg()*500 + where: date_of_sale >= @2001-02-01 and date_of_sale < @2001-04-01 + order_by: date_of_sale + limit: 20 + } + + #(story) + # line_chart + view: line_chart_bool_time_series is { + # x + group_by: date_of_sale + + # series + group_by: bool_series # y aggregate: `Sales $` is retail_price.avg()*500 - where: the_date >= @2001-02-01 and the_date < @2001-04-01 - order_by: the_date + where: date_of_sale >= @2001-02-01 and date_of_sale < @2001-04-01 + order_by: date_of_sale } }