| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- /**
- * 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 { ChartProps, supersetTheme, VizType } from '@superset-ui/core';
- import {
- LegendOrientation,
- LegendType,
- EchartsTimeseriesSeriesType,
- } from '../../src';
- import transformProps from '../../src/MixedTimeseries/transformProps';
- import {
- EchartsMixedTimeseriesFormData,
- EchartsMixedTimeseriesProps,
- } from '../../src/MixedTimeseries/types';
- const formData: EchartsMixedTimeseriesFormData = {
- annotationLayers: [],
- area: false,
- areaB: false,
- legendMargin: null,
- logAxis: false,
- logAxisSecondary: false,
- markerEnabled: false,
- markerEnabledB: false,
- markerSize: 0,
- markerSizeB: 0,
- minorSplitLine: false,
- minorTicks: false,
- opacity: 0,
- opacityB: 0,
- orderDesc: false,
- orderDescB: false,
- richTooltip: false,
- rowLimit: 0,
- rowLimitB: 0,
- legendOrientation: LegendOrientation.Top,
- legendType: LegendType.Scroll,
- showLegend: false,
- showValue: false,
- showValueB: false,
- stack: true,
- stackB: true,
- truncateYAxis: false,
- truncateYAxisSecondary: false,
- xAxisLabelRotation: 0,
- xAxisTitle: '',
- xAxisTitleMargin: 0,
- yAxisBounds: [undefined, undefined],
- yAxisBoundsSecondary: [undefined, undefined],
- yAxisTitle: '',
- yAxisTitleMargin: 0,
- yAxisTitlePosition: '',
- yAxisTitleSecondary: '',
- zoomable: false,
- colorScheme: 'bnbColors',
- datasource: '3__table',
- x_axis: 'ds',
- metrics: ['sum__num'],
- metricsB: ['sum__num'],
- groupby: ['gender'],
- groupbyB: ['gender'],
- seriesType: EchartsTimeseriesSeriesType.Line,
- seriesTypeB: EchartsTimeseriesSeriesType.Bar,
- viz_type: VizType.MixedTimeseries,
- forecastEnabled: false,
- forecastPeriods: [],
- forecastInterval: 0,
- forecastSeasonalityDaily: 0,
- legendSort: null,
- };
- const queriesData = [
- {
- data: [
- { boy: 1, girl: 2, ds: 599616000000 },
- { boy: 3, girl: 4, ds: 599916000000 },
- ],
- label_map: {
- ds: ['ds'],
- boy: ['boy'],
- girl: ['girl'],
- },
- },
- {
- data: [
- { boy: 1, girl: 2, ds: 599616000000 },
- { boy: 3, girl: 4, ds: 599916000000 },
- ],
- label_map: {
- ds: ['ds'],
- boy: ['boy'],
- girl: ['girl'],
- },
- },
- ];
- const chartPropsConfig = {
- formData,
- width: 800,
- height: 600,
- queriesData,
- theme: supersetTheme,
- };
- test('should transform chart props for viz with showQueryIdentifiers=false', () => {
- const chartPropsConfigWithoutIdentifiers = {
- ...chartPropsConfig,
- formData: {
- ...formData,
- showQueryIdentifiers: false,
- },
- };
- const chartProps = new ChartProps(chartPropsConfigWithoutIdentifiers);
- const transformed = transformProps(chartProps as EchartsMixedTimeseriesProps);
- // Check that series IDs don't include query identifiers
- const seriesIds = (transformed.echartOptions.series as any[]).map(
- (s: any) => s.id,
- );
- expect(seriesIds).toContain('sum__num, girl');
- expect(seriesIds).toContain('sum__num, boy');
- expect(seriesIds).not.toContain('sum__num (Query A), girl');
- expect(seriesIds).not.toContain('sum__num (Query A), boy');
- expect(seriesIds).not.toContain('sum__num (Query B), girl');
- expect(seriesIds).not.toContain('sum__num (Query B), boy');
- // Check that series name include query identifiers
- const seriesName = (transformed.echartOptions.series as any[]).map(
- (s: any) => s.name,
- );
- expect(seriesName).toContain('sum__num, girl');
- expect(seriesName).toContain('sum__num, boy');
- expect(seriesName).not.toContain('sum__num (Query A), girl');
- expect(seriesName).not.toContain('sum__num (Query A), boy');
- expect(seriesName).not.toContain('sum__num (Query B), girl');
- expect(seriesName).not.toContain('sum__num (Query B), boy');
- expect((transformed.echartOptions.legend as any).data).toEqual([
- 'sum__num, girl',
- 'sum__num, boy',
- 'sum__num, girl',
- 'sum__num, boy',
- ]);
- });
- test('should transform chart props for viz with showQueryIdentifiers=true', () => {
- const chartPropsConfigWithIdentifiers = {
- ...chartPropsConfig,
- formData: {
- ...formData,
- showQueryIdentifiers: true,
- },
- };
- const chartProps = new ChartProps(chartPropsConfigWithIdentifiers);
- const transformed = transformProps(chartProps as EchartsMixedTimeseriesProps);
- // Check that series IDs include query identifiers
- const seriesIds = (transformed.echartOptions.series as any[]).map(
- (s: any) => s.id,
- );
- expect(seriesIds).toContain('sum__num (Query A), girl');
- expect(seriesIds).toContain('sum__num (Query A), boy');
- expect(seriesIds).toContain('sum__num (Query B), girl');
- expect(seriesIds).toContain('sum__num (Query B), boy');
- expect(seriesIds).not.toContain('sum__num, girl');
- expect(seriesIds).not.toContain('sum__num, boy');
- // Check that series name include query identifiers
- const seriesName = (transformed.echartOptions.series as any[]).map(
- (s: any) => s.name,
- );
- expect(seriesName).toContain('sum__num (Query A), girl');
- expect(seriesName).toContain('sum__num (Query A), boy');
- expect(seriesName).toContain('sum__num (Query B), girl');
- expect(seriesName).toContain('sum__num (Query B), boy');
- expect(seriesName).not.toContain('sum__num, girl');
- expect(seriesName).not.toContain('sum__num, boy');
- expect((transformed.echartOptions.legend as any).data).toEqual([
- 'sum__num (Query A), girl',
- 'sum__num (Query A), boy',
- 'sum__num (Query B), girl',
- 'sum__num (Query B), boy',
- ]);
- });
- describe('legend sorting', () => {
- const getChartProps = (overrides = {}) =>
- new ChartProps({
- ...chartPropsConfig,
- formData: {
- ...formData,
- ...overrides,
- showQueryIdentifiers: true,
- },
- });
- it('sort legend by data', () => {
- const chartProps = getChartProps({
- legendSort: null,
- });
- const transformed = transformProps(
- chartProps as EchartsMixedTimeseriesProps,
- );
- expect((transformed.echartOptions.legend as any).data).toEqual([
- 'sum__num (Query A), girl',
- 'sum__num (Query A), boy',
- 'sum__num (Query B), girl',
- 'sum__num (Query B), boy',
- ]);
- });
- it('sort legend by label ascending', () => {
- const chartProps = getChartProps({
- legendSort: 'asc',
- });
- const transformed = transformProps(
- chartProps as EchartsMixedTimeseriesProps,
- );
- expect((transformed.echartOptions.legend as any).data).toEqual([
- 'sum__num (Query A), boy',
- 'sum__num (Query A), girl',
- 'sum__num (Query B), boy',
- 'sum__num (Query B), girl',
- ]);
- });
- it('sort legend by label descending', () => {
- const chartProps = getChartProps({
- legendSort: 'desc',
- });
- const transformed = transformProps(
- chartProps as EchartsMixedTimeseriesProps,
- );
- expect((transformed.echartOptions.legend as any).data).toEqual([
- 'sum__num (Query B), girl',
- 'sum__num (Query B), boy',
- 'sum__num (Query A), girl',
- 'sum__num (Query A), boy',
- ]);
- });
- });
- test('legend margin: top orientation sets grid.top correctly', () => {
- const chartPropsConfigWithoutIdentifiers = {
- ...chartPropsConfig,
- formData: {
- ...formData,
- legendMargin: 250,
- showLegend: true,
- },
- };
- const chartProps = new ChartProps(chartPropsConfigWithoutIdentifiers);
- const transformed = transformProps(chartProps as EchartsMixedTimeseriesProps);
- expect((transformed.echartOptions.grid as any).top).toEqual(270);
- });
- test('legend margin: bottom orientation sets grid.bottom correctly', () => {
- const chartPropsConfigWithoutIdentifiers = {
- ...chartPropsConfig,
- formData: {
- ...formData,
- legendMargin: 250,
- showLegend: true,
- legendOrientation: LegendOrientation.Bottom,
- },
- };
- const chartProps = new ChartProps(chartPropsConfigWithoutIdentifiers);
- const transformed = transformProps(chartProps as EchartsMixedTimeseriesProps);
- expect((transformed.echartOptions.grid as any).bottom).toEqual(270);
- });
- test('legend margin: left orientation sets grid.left correctly', () => {
- const chartPropsConfigWithoutIdentifiers = {
- ...chartPropsConfig,
- formData: {
- ...formData,
- legendMargin: 250,
- showLegend: true,
- legendOrientation: LegendOrientation.Left,
- },
- };
- const chartProps = new ChartProps(chartPropsConfigWithoutIdentifiers);
- const transformed = transformProps(chartProps as EchartsMixedTimeseriesProps);
- expect((transformed.echartOptions.grid as any).left).toEqual(270);
- });
- test('legend margin: right orientation sets grid.right correctly', () => {
- const chartPropsConfigWithoutIdentifiers = {
- ...chartPropsConfig,
- formData: {
- ...formData,
- legendMargin: 270,
- showLegend: true,
- legendOrientation: LegendOrientation.Right,
- },
- };
- const chartProps = new ChartProps(chartPropsConfigWithoutIdentifiers);
- const transformed = transformProps(chartProps as EchartsMixedTimeseriesProps);
- expect((transformed.echartOptions.grid as any).right).toEqual(270);
- });
|