Files
superset2/superset-frontend/plugins/plugin-chart-echarts/test/Timeseries/buildQuery.test.ts
Superset Dev 1fbde07e30 feat(scatter): add chart orientation and dot size metric controls
Adds two capabilities to the ECharts Scatter chart
(echarts_timeseries_scatter):

- A Vertical/Horizontal orientation control, reusing the shared
  Timeseries horizontal-orientation plumbing built for the Bar chart.
  Horizontal orientation places the dimension on the y-axis, enabling
  categorical (or time/numeric) y-axes with the metric on the x-axis.
  Axis-related controls swap between the X/Y sections based on
  orientation, mirroring the Bar chart panel.
- An optional "Dot size metric" with minimum/maximum dot size sliders.
  The size metric rides along in the query (deduped if it matches a
  value metric), its series are excluded from rendering, and each
  point's marker is sized so that marker *area* scales linearly with
  the metric between the configured bounds, avoiding the perceptual
  exaggeration of diameter-linear scaling.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 00:48:36 -07:00

154 lines
4.7 KiB
TypeScript

/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { SqlaFormData, VizType } from '@superset-ui/core';
import buildQuery from '../../src/Timeseries/buildQuery';
describe('Timeseries buildQuery', () => {
const formData = {
datasource: '5__table',
granularity_sqla: 'ds',
metrics: ['bar', 'baz'],
viz_type: 'my_chart',
};
test('should build groupby with series in form data', () => {
const queryContext = buildQuery(formData);
const [query] = queryContext.queries;
expect(query.metrics).toEqual(['bar', 'baz']);
});
test('should order by timeseries limit if orderby unspecified', () => {
const queryContext = buildQuery({
...formData,
timeseries_limit_metric: 'bar',
order_desc: true,
});
const [query] = queryContext.queries;
expect(query.metrics).toEqual(['bar', 'baz']);
expect(query.series_limit_metric).toEqual('bar');
expect(query.order_desc).toEqual(true);
expect(query.orderby).toEqual([['bar', false]]);
});
test('should include the scatter dot size metric in query metrics', () => {
const queryContext = buildQuery({
...formData,
size: 'qux',
});
const [query] = queryContext.queries;
expect(query.metrics).toEqual(['bar', 'baz', 'qux']);
});
test('should dedupe the dot size metric when it is also a value metric', () => {
const queryContext = buildQuery({
...formData,
size: 'bar',
});
const [query] = queryContext.queries;
expect(query.metrics).toEqual(['bar', 'baz']);
});
test('should not order by timeseries limit if orderby provided', () => {
const queryContext = buildQuery({
...formData,
timeseries_limit_metric: 'bar',
order_desc: true,
orderby: [['foo', true]],
});
const [query] = queryContext.queries;
expect(query.metrics).toEqual(['bar', 'baz']);
expect(query.series_limit_metric).toEqual('bar');
expect(query.order_desc).toEqual(true);
expect(query.orderby).toEqual([['foo', true]]);
});
});
describe('queryObject conversion', () => {
const formData: SqlaFormData = {
datasource: '5__table',
viz_type: VizType.Table,
granularity_sqla: 'time_column',
time_grain_sqla: 'P1Y',
time_range: '1 year ago : 2013',
groupby: ['col1'],
metrics: ['count(*)'],
};
test("shouldn't convert queryObject", () => {
const { queries } = buildQuery(formData);
expect(queries[0]).toEqual(
expect.objectContaining({
granularity: 'time_column',
time_range: '1 year ago : 2013',
extras: { time_grain_sqla: 'P1Y', having: '', where: '' },
columns: ['col1'],
series_columns: ['col1'],
metrics: ['count(*)'],
is_timeseries: true,
post_processing: [
{
operation: 'pivot',
options: {
aggregates: { 'count(*)': { operator: 'mean' } },
columns: ['col1'],
drop_missing_columns: true,
index: ['__timestamp'],
},
},
{ operation: 'flatten' },
],
}),
);
});
test('should convert queryObject', () => {
const { queries } = buildQuery({ ...formData, x_axis: 'time_column' });
expect(queries[0]).toMatchObject({
granularity: 'time_column',
time_range: '1 year ago : 2013',
extras: { having: '', where: '', time_grain_sqla: 'P1Y' },
columns: [
{
columnType: 'BASE_AXIS',
expressionType: 'SQL',
label: 'time_column',
sqlExpression: 'time_column',
timeGrain: 'P1Y',
isColumnReference: true,
},
'col1',
],
series_columns: ['col1'],
metrics: ['count(*)'],
post_processing: [
{
operation: 'pivot',
options: {
aggregates: { 'count(*)': { operator: 'mean' } },
columns: ['col1'],
drop_missing_columns: true,
index: ['time_column'],
},
},
{ operation: 'flatten' },
],
});
});
});