| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- /**
- * 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, SqlaFormData, supersetTheme } from '@superset-ui/core';
- import transformProps from '../../src/Graph/transformProps';
- import { DEFAULT_GRAPH_SERIES_OPTION } from '../../src/Graph/constants';
- import { EchartsGraphChartProps } from '../../src/Graph/types';
- const formData: SqlaFormData = {
- colorScheme: 'bnbColors',
- datasource: '3__table',
- granularity_sqla: 'ds',
- metric: 'count',
- source: 'source_column',
- target: 'target_column',
- category: null,
- viz_type: 'graph',
- };
- const queriesData = [
- {
- colnames: ['source_column', 'target_column', 'count'],
- data: [
- {
- source_column: 'source_value_1',
- target_column: 'target_value_1',
- count: 6,
- },
- {
- source_column: 'source_value_2',
- target_column: 'target_value_2',
- count: 5,
- },
- ],
- },
- ];
- const chartPropsConfig = {
- formData,
- width: 800,
- height: 600,
- queriesData,
- theme: supersetTheme,
- };
- describe('EchartsGraph transformProps', () => {
- it('should transform chart props for viz without category', () => {
- const chartProps = new ChartProps(chartPropsConfig);
- expect(transformProps(chartProps as EchartsGraphChartProps)).toEqual(
- expect.objectContaining({
- width: 800,
- height: 600,
- echartOptions: expect.objectContaining({
- legend: expect.objectContaining({
- data: [],
- }),
- series: expect.arrayContaining([
- expect.objectContaining({
- data: [
- {
- col: 'source_column',
- category: undefined,
- id: '0',
- itemStyle: {
- color: '#1f77b4',
- },
- label: { show: true },
- name: 'source_value_1',
- select: {
- itemStyle: { borderWidth: 3, opacity: 1 },
- label: { fontWeight: 'bolder' },
- },
- symbolSize: 50,
- tooltip: expect.anything(),
- value: 6,
- },
- {
- col: 'target_column',
- category: undefined,
- id: '1',
- itemStyle: {
- color: '#1f77b4',
- },
- label: { show: true },
- name: 'target_value_1',
- select: {
- itemStyle: { borderWidth: 3, opacity: 1 },
- label: { fontWeight: 'bolder' },
- },
- symbolSize: 50,
- tooltip: expect.anything(),
- value: 6,
- },
- {
- col: 'source_column',
- category: undefined,
- id: '2',
- itemStyle: {
- color: '#1f77b4',
- },
- label: { show: true },
- name: 'source_value_2',
- select: {
- itemStyle: { borderWidth: 3, opacity: 1 },
- label: { fontWeight: 'bolder' },
- },
- symbolSize: 10,
- tooltip: expect.anything(),
- value: 5,
- },
- {
- col: 'target_column',
- category: undefined,
- id: '3',
- itemStyle: {
- color: '#1f77b4',
- },
- label: { show: true },
- name: 'target_value_2',
- select: {
- itemStyle: { borderWidth: 3, opacity: 1 },
- label: { fontWeight: 'bolder' },
- },
- symbolSize: 10,
- tooltip: expect.anything(),
- value: 5,
- },
- ],
- }),
- expect.objectContaining({
- links: [
- {
- emphasis: { lineStyle: { width: 12 } },
- lineStyle: { width: 6, color: '#1f77b4' },
- select: {
- lineStyle: { opacity: 1, width: 9.600000000000001 },
- },
- source: '0',
- target: '1',
- value: 6,
- },
- {
- emphasis: { lineStyle: { width: 5 } },
- lineStyle: { width: 1.5, color: '#1f77b4' },
- select: { lineStyle: { opacity: 1, width: 5 } },
- source: '2',
- target: '3',
- value: 5,
- },
- ],
- }),
- ]),
- }),
- }),
- );
- });
- it('should transform chart props for viz with category and falsy normalization', () => {
- const formData: SqlaFormData = {
- colorScheme: 'bnbColors',
- datasource: '3__table',
- granularity_sqla: 'ds',
- metric: 'count',
- source: 'source_column',
- target: 'target_column',
- sourceCategory: 'source_category_column',
- targetCategory: 'target_category_column',
- viz_type: 'graph',
- };
- const queriesData = [
- {
- colnames: [
- 'source_column',
- 'target_column',
- 'source_category_column',
- 'target_category_column',
- 'count',
- ],
- data: [
- {
- source_column: 'source_value',
- target_column: 'target_value',
- source_category_column: 'category_value_1',
- target_category_column: 'category_value_2',
- count: 6,
- },
- {
- source_column: 'source_value',
- target_column: 'target_value',
- source_category_column: 'category_value_1',
- target_category_column: 'category_value_2',
- count: 5,
- },
- ],
- },
- ];
- const chartPropsConfig = {
- formData,
- width: 800,
- height: 600,
- queriesData,
- theme: supersetTheme,
- };
- const chartProps = new ChartProps(chartPropsConfig);
- expect(transformProps(chartProps as EchartsGraphChartProps)).toEqual(
- expect.objectContaining({
- width: 800,
- height: 600,
- echartOptions: expect.objectContaining({
- legend: expect.objectContaining({
- data: ['category_value_1', 'category_value_2'],
- }),
- series: expect.arrayContaining([
- expect.objectContaining({
- data: [
- {
- id: '0',
- itemStyle: {
- color: '#1f77b4',
- },
- col: 'source_column',
- name: 'source_value',
- value: 11,
- symbolSize: 10,
- category: 'category_value_1',
- select: DEFAULT_GRAPH_SERIES_OPTION.select,
- tooltip: expect.anything(),
- label: { show: true },
- },
- {
- id: '1',
- itemStyle: {
- color: '#ff7f0e',
- },
- col: 'target_column',
- name: 'target_value',
- value: 11,
- symbolSize: 10,
- category: 'category_value_2',
- select: DEFAULT_GRAPH_SERIES_OPTION.select,
- tooltip: expect.anything(),
- label: { show: true },
- },
- ],
- }),
- ]),
- }),
- }),
- );
- });
- });
- describe('legend sorting', () => {
- const queriesData = [
- {
- colnames: [
- 'source_column',
- 'target_column',
- 'source_category_column',
- 'target_category_column',
- 'count',
- ],
- data: [
- {
- source_column: 'source_value',
- target_column: 'target_value',
- source_category_column: 'category_value_1',
- target_category_column: 'category_value_3',
- count: 6,
- },
- {
- source_column: 'source_value',
- target_column: 'target_value',
- source_category_column: 'category_value_3',
- target_category_column: 'category_value_2',
- count: 5,
- },
- {
- source_column: 'source_value',
- target_column: 'target_value',
- source_category_column: 'category_value_2',
- target_category_column: 'category_value_1',
- count: 4,
- },
- ],
- },
- ];
- const getChartProps = (overrides = {}) =>
- new ChartProps({
- ...chartPropsConfig,
- formData: {
- ...formData,
- ...overrides,
- sourceCategory: 'source_category_column',
- targetCategory: 'target_category_column',
- },
- queriesData,
- });
- it('sort legend by data', () => {
- const chartProps = getChartProps({
- legendSort: null,
- });
- const transformed = transformProps(chartProps as EchartsGraphChartProps);
- expect((transformed.echartOptions.legend as any).data).toEqual([
- 'category_value_1',
- 'category_value_3',
- 'category_value_2',
- ]);
- });
- it('sort legend by label ascending', () => {
- const chartProps = getChartProps({
- legendSort: 'asc',
- });
- const transformed = transformProps(chartProps as EchartsGraphChartProps);
- expect((transformed.echartOptions.legend as any).data).toEqual([
- 'category_value_1',
- 'category_value_2',
- 'category_value_3',
- ]);
- });
- it('sort legend by label descending', () => {
- const chartProps = getChartProps({
- legendSort: 'desc',
- });
- const transformed = transformProps(chartProps as EchartsGraphChartProps);
- expect((transformed.echartOptions.legend as any).data).toEqual([
- 'category_value_3',
- 'category_value_2',
- 'category_value_1',
- ]);
- });
- });
|