transformProps.test.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 {
  20. CategoricalColorNamespace,
  21. ChartProps,
  22. SqlaFormData,
  23. supersetTheme,
  24. VizType,
  25. } from '@superset-ui/core';
  26. import transformProps, {
  27. getIntervalBoundsAndColors,
  28. } from '../../src/Gauge/transformProps';
  29. import { EchartsGaugeChartProps } from '../../src/Gauge/types';
  30. describe('Echarts Gauge transformProps', () => {
  31. const baseFormData: SqlaFormData = {
  32. datasource: '26__table',
  33. viz_type: VizType.Gauge,
  34. metric: 'count',
  35. adhocFilters: [],
  36. rowLimit: 10,
  37. minVal: 0,
  38. maxVal: 100,
  39. startAngle: 225,
  40. endAngle: -45,
  41. colorScheme: 'SUPERSET_DEFAULT',
  42. fontSize: 14,
  43. numberFormat: 'SMART_NUMBER',
  44. valueFormatter: '{value}',
  45. showPointer: true,
  46. animation: true,
  47. showAxisTick: false,
  48. showSplitLine: false,
  49. splitNumber: 10,
  50. showProgress: true,
  51. overlap: true,
  52. roundCap: false,
  53. };
  54. it('should transform chart props for no group by column', () => {
  55. const formData: SqlaFormData = { ...baseFormData, groupby: [] };
  56. const queriesData = [
  57. {
  58. colnames: ['count'],
  59. data: [
  60. {
  61. count: 16595,
  62. },
  63. ],
  64. },
  65. ];
  66. const chartPropsConfig = {
  67. formData,
  68. width: 800,
  69. height: 600,
  70. queriesData,
  71. theme: supersetTheme,
  72. };
  73. const chartProps = new ChartProps(chartPropsConfig);
  74. const result = transformProps(chartProps as EchartsGaugeChartProps);
  75. // Test core properties
  76. expect(result.width).toBe(800);
  77. expect(result.height).toBe(600);
  78. // Test series data
  79. const seriesData = (result.echartOptions as any).series[0].data;
  80. expect(seriesData).toHaveLength(1);
  81. expect(seriesData[0].value).toBe(16595);
  82. expect(seriesData[0].name).toBe('');
  83. expect(seriesData[0].itemStyle.color).toBe('#1f77b4');
  84. // Test detail and title positions
  85. expect(seriesData[0].title.offsetCenter).toEqual(['0%', '20%']);
  86. expect(seriesData[0].title.fontSize).toBe(14);
  87. expect(seriesData[0].detail.offsetCenter).toEqual(['0%', '32.6%']);
  88. expect(seriesData[0].detail.fontSize).toBe(16.8);
  89. });
  90. it('should transform chart props for single group by column', () => {
  91. const formData: SqlaFormData = {
  92. ...baseFormData,
  93. groupby: ['year'],
  94. };
  95. const queriesData = [
  96. {
  97. colnames: ['year', 'count'],
  98. data: [
  99. {
  100. year: 1988,
  101. count: 15,
  102. },
  103. {
  104. year: 1995,
  105. count: 219,
  106. },
  107. ],
  108. },
  109. ];
  110. const chartPropsConfig = {
  111. formData,
  112. width: 800,
  113. height: 600,
  114. queriesData,
  115. theme: supersetTheme,
  116. };
  117. const chartProps = new ChartProps(chartPropsConfig);
  118. const result = transformProps(chartProps as EchartsGaugeChartProps);
  119. // Test core properties
  120. expect(result.width).toBe(800);
  121. expect(result.height).toBe(600);
  122. // Test series data
  123. const seriesData = (result.echartOptions as any).series[0].data;
  124. expect(seriesData).toHaveLength(2);
  125. // First data point
  126. expect(seriesData[0].value).toBe(15);
  127. expect(seriesData[0].name).toBe('year: 1988');
  128. expect(seriesData[0].itemStyle.color).toBe('#1f77b4');
  129. expect(seriesData[0].title.offsetCenter).toEqual(['0%', '20%']);
  130. expect(seriesData[0].title.fontSize).toBe(14);
  131. expect(seriesData[0].detail.offsetCenter).toEqual(['0%', '32.6%']);
  132. expect(seriesData[0].detail.fontSize).toBe(16.8);
  133. // Second data point
  134. expect(seriesData[1].value).toBe(219);
  135. expect(seriesData[1].name).toBe('year: 1995');
  136. expect(seriesData[1].itemStyle.color).toBe('#ff7f0e');
  137. expect(seriesData[1].title.offsetCenter).toEqual(['0%', '48%']);
  138. expect(seriesData[1].title.fontSize).toBe(14);
  139. expect(seriesData[1].detail.offsetCenter).toEqual(['0%', '60.6%']);
  140. expect(seriesData[1].detail.fontSize).toBe(16.8);
  141. });
  142. it('should transform chart props for multiple group by columns', () => {
  143. const formData: SqlaFormData = {
  144. ...baseFormData,
  145. groupby: ['year', 'platform'],
  146. };
  147. const queriesData = [
  148. {
  149. colnames: ['year', 'platform', 'count'],
  150. data: [
  151. {
  152. year: 2011,
  153. platform: 'PC',
  154. count: 140,
  155. },
  156. {
  157. year: 2008,
  158. platform: 'PC',
  159. count: 76,
  160. },
  161. ],
  162. },
  163. ];
  164. const chartPropsConfig = {
  165. formData,
  166. width: 800,
  167. height: 600,
  168. queriesData,
  169. theme: supersetTheme,
  170. };
  171. const chartProps = new ChartProps(chartPropsConfig);
  172. const result = transformProps(chartProps as EchartsGaugeChartProps);
  173. // Test core properties
  174. expect(result.width).toBe(800);
  175. expect(result.height).toBe(600);
  176. // Test series data
  177. const seriesData = (result.echartOptions as any).series[0].data;
  178. expect(seriesData).toHaveLength(2);
  179. // First data point
  180. expect(seriesData[0].value).toBe(140);
  181. expect(seriesData[0].name).toBe('year: 2011, platform: PC');
  182. expect(seriesData[0].itemStyle.color).toBe('#1f77b4');
  183. expect(seriesData[0].title.offsetCenter).toEqual(['0%', '20%']);
  184. expect(seriesData[0].title.fontSize).toBe(14);
  185. expect(seriesData[0].detail.offsetCenter).toEqual(['0%', '32.6%']);
  186. expect(seriesData[0].detail.fontSize).toBe(16.8);
  187. // Second data point
  188. expect(seriesData[1].value).toBe(76);
  189. expect(seriesData[1].name).toBe('year: 2008, platform: PC');
  190. expect(seriesData[1].itemStyle.color).toBe('#ff7f0e');
  191. expect(seriesData[1].title.offsetCenter).toEqual(['0%', '48%']);
  192. expect(seriesData[1].title.fontSize).toBe(14);
  193. expect(seriesData[1].detail.offsetCenter).toEqual(['0%', '60.6%']);
  194. expect(seriesData[1].detail.fontSize).toBe(16.8);
  195. });
  196. it('should transform chart props for intervals', () => {
  197. const formData: SqlaFormData = {
  198. ...baseFormData,
  199. groupby: ['year', 'platform'],
  200. intervals: '60,100',
  201. intervalColorIndices: '1,2',
  202. minVal: 20,
  203. };
  204. const queriesData = [
  205. {
  206. colnames: ['year', 'platform', 'count'],
  207. data: [
  208. {
  209. year: 2011,
  210. platform: 'PC',
  211. count: 140,
  212. },
  213. {
  214. year: 2008,
  215. platform: 'PC',
  216. count: 76,
  217. },
  218. ],
  219. },
  220. ];
  221. const chartPropsConfig = {
  222. formData,
  223. width: 800,
  224. height: 600,
  225. queriesData,
  226. theme: supersetTheme,
  227. };
  228. const chartProps = new ChartProps(chartPropsConfig);
  229. const result = transformProps(chartProps as EchartsGaugeChartProps);
  230. // Test core properties
  231. expect(result.width).toBe(800);
  232. expect(result.height).toBe(600);
  233. // Test axisLine intervals
  234. const { axisLine } = (result.echartOptions as any).series[0];
  235. expect(axisLine.roundCap).toBe(false);
  236. expect(axisLine.lineStyle.width).toBe(14);
  237. expect(axisLine.lineStyle.color).toEqual([
  238. [0.5, '#1f77b4'],
  239. [1, '#ff7f0e'],
  240. ]);
  241. // Test series data
  242. const seriesData = (result.echartOptions.series as any)[0].data;
  243. expect(seriesData).toHaveLength(2);
  244. // First data point
  245. expect(seriesData[0].value).toBe(140);
  246. expect(seriesData[0].name).toBe('year: 2011, platform: PC');
  247. expect(seriesData[0].itemStyle.color).toBe('#1f77b4');
  248. // Second data point
  249. expect(seriesData[1].value).toBe(76);
  250. expect(seriesData[1].name).toBe('year: 2008, platform: PC');
  251. expect(seriesData[1].itemStyle.color).toBe('#ff7f0e');
  252. });
  253. });
  254. describe('getIntervalBoundsAndColors', () => {
  255. it('should generate correct interval bounds and colors', () => {
  256. const colorFn = CategoricalColorNamespace.getScale(
  257. 'supersetColors' as string,
  258. );
  259. expect(getIntervalBoundsAndColors('', '', colorFn, 0, 10)).toEqual([]);
  260. expect(getIntervalBoundsAndColors('4, 10', '1, 2', colorFn, 0, 10)).toEqual(
  261. [
  262. [0.4, '#1f77b4'],
  263. [1, '#ff7f0e'],
  264. ],
  265. );
  266. expect(
  267. getIntervalBoundsAndColors('4, 8, 10', '9, 8, 7', colorFn, 0, 10),
  268. ).toEqual([
  269. [0.4, '#bcbd22'],
  270. [0.8, '#7f7f7f'],
  271. [1, '#e377c2'],
  272. ]);
  273. expect(getIntervalBoundsAndColors('4, 10', '1, 2', colorFn, 2, 10)).toEqual(
  274. [
  275. [0.25, '#1f77b4'],
  276. [1, '#ff7f0e'],
  277. ],
  278. );
  279. expect(
  280. getIntervalBoundsAndColors('-4, 0', '1, 2', colorFn, -10, 0),
  281. ).toEqual([
  282. [0.6, '#1f77b4'],
  283. [1, '#ff7f0e'],
  284. ]);
  285. expect(
  286. getIntervalBoundsAndColors('-4, -2', '1, 2', colorFn, -10, -2),
  287. ).toEqual([
  288. [0.75, '#1f77b4'],
  289. [1, '#ff7f0e'],
  290. ]);
  291. });
  292. });