Skip to content

[9.4] [Lens as code] Fix temporal scale on x axis and histogram brush (#264134)#264482

Merged
kibanamachine merged 1 commit intoelastic:9.4from
kibanamachine:backport/9.4/pr-264134
Apr 20, 2026
Merged

[9.4] [Lens as code] Fix temporal scale on x axis and histogram brush (#264134)#264482
kibanamachine merged 1 commit intoelastic:9.4from
kibanamachine:backport/9.4/pr-264134

Conversation

@kibanamachine
Copy link
Copy Markdown
Contributor

Backport

This will backport the following commits from main to 9.4:

Questions ?

Please refer to the Backport tool documentation

…tic#264134)

## Summary

API generated charts currently doesn't contain information if a XY chart
is actually a time based chart or a categorical chart.
We have left this information to be computed/decided at runtime (as it's
currently done when you create a chart from the Lens UI).
But information about the current timeField is required:
- to filter the chart in the configured time range
- to property assign the X axis scale type
- to enable the timeseries brushing

This PR fixes this problem by correctly picking the timeField when
necessary and clear the dataview cache that was polluted with the
initial adhoc dataview without timefield.

Right after ESQL query is executed, I'm adjusting the x scale to time if
the table column layer type is date.

For ESQL folks, I've just extracted the getESQLTimeField into its own
file, so it can be easily reused without the need to touch the DataViews
as it was initially used.

fix elastic#262425
fix elastic#262181

(cherry picked from commit fb3adea)
Comment on lines +44 to +47
const xScaleType =
table.columns.find((column) => column.id === accessors.xAccessor)?.meta.type === 'date'
? XScaleTypes.TIME
: args.xScaleType;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium expression_functions/extended_data_layer_fn.ts:44

The comparison column.id === accessors.xAccessor on line 45 assumes accessors.xAccessor is a string, but getAccessors can return it as an ExpressionValueVisDimension object. When it's an object, the comparison always fails, so date columns are never detected as XScaleTypes.TIME and isHistogram is calculated incorrectly. Use getColumnByAccessor to handle both accessor types.

-  const xScaleType =
-    table.columns.find((column) => column.id === accessors.xAccessor)?.meta.type === 'date'
-      ? XScaleTypes.TIME
-      : args.xScaleType;
🤖 Copy this AI Prompt to have your agent fix this:
In file src/platform/plugins/shared/chart_expressions/expression_xy/common/expression_functions/extended_data_layer_fn.ts around lines 44-47:

The comparison `column.id === accessors.xAccessor` on line 45 assumes `accessors.xAccessor` is a string, but `getAccessors` can return it as an `ExpressionValueVisDimension` object. When it's an object, the comparison always fails, so date columns are never detected as `XScaleTypes.TIME` and `isHistogram` is calculated incorrectly. Use `getColumnByAccessor` to handle both accessor types.

Evidence trail:
1. `extended_data_layer_fn.ts` lines 25-27: `getAccessors<string | ExpressionValueVisDimension, ExtendedDataLayerArgs>()` shows `xAccessor` can be either type
2. `extended_data_layer_fn.ts` lines 43-46: Uses `column.id === accessors.xAccessor` directly
3. `expression_value_dimension.ts` lines 12-19: Shows `ExpressionValueVisDimension` is an object type with nested accessor
4. `accessors.ts` lines 113-125: `getColumnByAccessor` handles both types correctly
5. `visualization.ts` line 18: Same package uses the correct pattern `getColumnByAccessor(l.xAccessor, l.table.columns)?.meta.type === 'date'`

@kibanamachine kibanamachine merged commit be85827 into elastic:9.4 Apr 20, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport This PR is a backport of another PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants