transformProps.test.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import { ChartProps, supersetTheme } from '@superset-ui/core';
  20. import { RadarSeriesOption } from 'echarts/charts';
  21. import transformProps from '../../src/Radar/transformProps';
  22. import {
  23. EchartsRadarChartProps,
  24. EchartsRadarFormData,
  25. } from '../../src/Radar/types';
  26. interface RadarIndicator {
  27. name: string;
  28. max: number;
  29. min: number;
  30. }
  31. type RadarShape = 'circle' | 'polygon';
  32. interface RadarChartConfig {
  33. shape: RadarShape;
  34. indicator: RadarIndicator[];
  35. }
  36. interface RadarSeriesData {
  37. value: number[];
  38. name: string;
  39. }
  40. const formData: Partial<EchartsRadarFormData> = {
  41. colorScheme: 'supersetColors',
  42. datasource: '3__table',
  43. granularity_sqla: 'ds',
  44. columnConfig: {
  45. 'MAX(na_sales)': {
  46. radarMetricMaxValue: null,
  47. radarMetricMinValue: 0,
  48. },
  49. 'SUM(eu_sales)': {
  50. radarMetricMaxValue: 5000,
  51. },
  52. },
  53. groupby: [],
  54. metrics: [
  55. 'MAX(na_sales)',
  56. 'SUM(jp_sales)',
  57. 'SUM(other_sales)',
  58. 'SUM(eu_sales)',
  59. ],
  60. viz_type: 'radar',
  61. numberFormat: 'SMART_NUMBER',
  62. dateFormat: 'smart_date',
  63. showLegend: true,
  64. showLabels: true,
  65. isCircle: false,
  66. };
  67. const queriesData = [
  68. {
  69. data: [
  70. {
  71. 'MAX(na_sales)': 41.49,
  72. 'SUM(jp_sales)': 1290.99,
  73. 'SUM(other_sales)': 797.73,
  74. 'SUM(eu_sales)': 2434.13,
  75. },
  76. ],
  77. },
  78. ];
  79. const chartProps = new ChartProps({
  80. formData,
  81. width: 800,
  82. height: 600,
  83. queriesData,
  84. theme: supersetTheme,
  85. });
  86. describe('Radar transformProps', () => {
  87. it('should transform chart props for normalized radar chart & normalize all metrics except the ones with custom min & max', () => {
  88. const transformedProps = transformProps(
  89. chartProps as EchartsRadarChartProps,
  90. );
  91. const series = transformedProps.echartOptions.series as RadarSeriesOption[];
  92. const radar = transformedProps.echartOptions.radar as RadarChartConfig;
  93. expect((series[0].data as RadarSeriesData[])[0].value).toEqual([
  94. 0.0170451044, 0.5303701939, 0.3277269497, 2434.13,
  95. ]);
  96. expect(radar.indicator).toEqual([
  97. {
  98. name: 'MAX(na_sales)',
  99. max: 1,
  100. min: 0,
  101. },
  102. {
  103. name: 'SUM(jp_sales)',
  104. max: 1,
  105. min: 0,
  106. },
  107. {
  108. name: 'SUM(other_sales)',
  109. max: 1,
  110. min: 0,
  111. },
  112. {
  113. name: 'SUM(eu_sales)',
  114. max: 5000,
  115. min: 0,
  116. },
  117. ]);
  118. });
  119. });
  120. describe('legend sorting', () => {
  121. const legendSortData = [
  122. {
  123. data: [
  124. {
  125. name: 'Sylvester sales',
  126. 'SUM(jp_sales)': 1290.99,
  127. 'SUM(other_sales)': 797.73,
  128. 'SUM(eu_sales)': 2434.13,
  129. },
  130. {
  131. name: 'Arnold sales',
  132. 'SUM(jp_sales)': 290.99,
  133. 'SUM(other_sales)': 627.73,
  134. 'SUM(eu_sales)': 434.13,
  135. },
  136. {
  137. name: 'Mark sales',
  138. 'SUM(jp_sales)': 2290.99,
  139. 'SUM(other_sales)': 1297.73,
  140. 'SUM(eu_sales)': 934.13,
  141. },
  142. ],
  143. },
  144. ];
  145. const createChartProps = (overrides = {}) =>
  146. new ChartProps({
  147. ...chartProps,
  148. formData: {
  149. ...formData,
  150. groupby: ['name'],
  151. metrics: ['SUM(jp_sales)', 'SUM(other_sales)', 'SUM(eu_sales)'],
  152. ...overrides,
  153. },
  154. queriesData: legendSortData,
  155. });
  156. it('preserves original data order when no sort specified', () => {
  157. const props = createChartProps({ legendSort: null });
  158. const result = transformProps(props as EchartsRadarChartProps);
  159. const legendData = (result.echartOptions.legend as any).data;
  160. expect(legendData).toEqual([
  161. 'Sylvester sales',
  162. 'Arnold sales',
  163. 'Mark sales',
  164. ]);
  165. });
  166. it('sorts alphabetically ascending when legendSort is "asc"', () => {
  167. const props = createChartProps({ legendSort: 'asc' });
  168. const result = transformProps(props as EchartsRadarChartProps);
  169. const legendData = (result.echartOptions.legend as any).data;
  170. expect(legendData).toEqual([
  171. 'Arnold sales',
  172. 'Mark sales',
  173. 'Sylvester sales',
  174. ]);
  175. });
  176. it('sorts alphabetically descending when legendSort is "desc"', () => {
  177. const props = createChartProps({ legendSort: 'desc' });
  178. const result = transformProps(props as EchartsRadarChartProps);
  179. const legendData = (result.echartOptions.legend as any).data;
  180. expect(legendData).toEqual([
  181. 'Sylvester sales',
  182. 'Mark sales',
  183. 'Arnold sales',
  184. ]);
  185. });
  186. });