transformers.test.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 { CategoricalColorScale } from '@superset-ui/core';
  20. import type { SeriesOption } from 'echarts';
  21. import { EchartsTimeseriesSeriesType } from '../../src';
  22. import {
  23. transformSeries,
  24. transformNegativeLabelsPosition,
  25. } from '../../src/Timeseries/transformers';
  26. // Mock the colorScale function
  27. const mockColorScale = jest.fn(
  28. (key: string, sliceId?: number) => `color-for-${key}-${sliceId}`,
  29. ) as unknown as CategoricalColorScale;
  30. describe('transformSeries', () => {
  31. const series = { name: 'test-series' };
  32. it('should use the colorScaleKey if timeShiftColor is enabled', () => {
  33. const opts = {
  34. timeShiftColor: true,
  35. colorScaleKey: 'test-key',
  36. sliceId: 1,
  37. };
  38. const result = transformSeries(series, mockColorScale, 'test-key', opts);
  39. expect((result as any)?.itemStyle.color).toBe('color-for-test-key-1');
  40. });
  41. it('should use seriesKey if timeShiftColor is not enabled', () => {
  42. const opts = {
  43. timeShiftColor: false,
  44. seriesKey: 'series-key',
  45. sliceId: 2,
  46. };
  47. const result = transformSeries(series, mockColorScale, 'test-key', opts);
  48. expect((result as any)?.itemStyle.color).toBe('color-for-series-key-2');
  49. });
  50. it('should apply border styles for bar series with connectNulls', () => {
  51. const opts = {
  52. seriesType: EchartsTimeseriesSeriesType.Bar,
  53. connectNulls: true,
  54. timeShiftColor: false,
  55. };
  56. const result = transformSeries(series, mockColorScale, 'test-key', opts);
  57. expect((result as any).itemStyle.borderWidth).toBe(1.5);
  58. expect((result as any).itemStyle.borderType).toBe('dotted');
  59. expect((result as any).itemStyle.borderColor).toBe(
  60. (result as any).itemStyle.color,
  61. );
  62. });
  63. it('should not apply border styles for non-bar series', () => {
  64. const opts = {
  65. seriesType: EchartsTimeseriesSeriesType.Line,
  66. connectNulls: true,
  67. timeShiftColor: false,
  68. };
  69. const result = transformSeries(series, mockColorScale, 'test-key', opts);
  70. expect((result as any).itemStyle.borderWidth).toBe(0);
  71. expect((result as any).itemStyle.borderType).toBeUndefined();
  72. expect((result as any).itemStyle.borderColor).toBeUndefined();
  73. });
  74. });
  75. describe('transformNegativeLabelsPosition', () => {
  76. it('label position bottom of negative value no Horizontal', () => {
  77. const isHorizontal = false;
  78. const series: SeriesOption = {
  79. data: [
  80. [2020, 1],
  81. [2021, 3],
  82. [2022, -2],
  83. [2023, -5],
  84. [2024, 4],
  85. ],
  86. type: EchartsTimeseriesSeriesType.Bar,
  87. stack: undefined,
  88. };
  89. const result =
  90. Array.isArray(series.data) && series.type === 'bar' && !series.stack
  91. ? transformNegativeLabelsPosition(series, isHorizontal)
  92. : series.data;
  93. expect((result as any)[0].label).toBe(undefined);
  94. expect((result as any)[1].label).toBe(undefined);
  95. expect((result as any)[2].label.position).toBe('outside');
  96. expect((result as any)[3].label.position).toBe('outside');
  97. expect((result as any)[4].label).toBe(undefined);
  98. });
  99. it('label position left of negative value is Horizontal', () => {
  100. const isHorizontal = true;
  101. const series: SeriesOption = {
  102. data: [
  103. [1, 2020],
  104. [-3, 2021],
  105. [2, 2022],
  106. [-4, 2023],
  107. [-6, 2024],
  108. ],
  109. type: EchartsTimeseriesSeriesType.Bar,
  110. stack: undefined,
  111. };
  112. const result =
  113. Array.isArray(series.data) && series.type === 'bar' && !series.stack
  114. ? transformNegativeLabelsPosition(series, isHorizontal)
  115. : series.data;
  116. expect((result as any)[0].label).toBe(undefined);
  117. expect((result as any)[1].label.position).toBe('outside');
  118. expect((result as any)[2].label).toBe(undefined);
  119. expect((result as any)[3].label.position).toBe('outside');
  120. expect((result as any)[4].label.position).toBe('outside');
  121. });
  122. it('label position to line type', () => {
  123. const isHorizontal = false;
  124. const series: SeriesOption = {
  125. data: [
  126. [2020, 1],
  127. [2021, 3],
  128. [2022, -2],
  129. [2023, -5],
  130. [2024, 4],
  131. ],
  132. type: EchartsTimeseriesSeriesType.Line,
  133. stack: undefined,
  134. };
  135. const result =
  136. Array.isArray(series.data) &&
  137. !series.stack &&
  138. series.type !== 'line' &&
  139. series.type === 'bar'
  140. ? transformNegativeLabelsPosition(series, isHorizontal)
  141. : series.data;
  142. expect((result as any)[0].label).toBe(undefined);
  143. expect((result as any)[1].label).toBe(undefined);
  144. expect((result as any)[2].label).toBe(undefined);
  145. expect((result as any)[3].label).toBe(undefined);
  146. expect((result as any)[4].label).toBe(undefined);
  147. });
  148. it('label position to bar type and stack', () => {
  149. const isHorizontal = false;
  150. const series: SeriesOption = {
  151. data: [
  152. [2020, 1],
  153. [2021, 3],
  154. [2022, -2],
  155. [2023, -5],
  156. [2024, 4],
  157. ],
  158. type: EchartsTimeseriesSeriesType.Bar,
  159. stack: 'obs',
  160. };
  161. const result =
  162. Array.isArray(series.data) && series.type === 'bar' && !series.stack
  163. ? transformNegativeLabelsPosition(series, isHorizontal)
  164. : series.data;
  165. expect((result as any)[0].label).toBe(undefined);
  166. expect((result as any)[1].label).toBe(undefined);
  167. expect((result as any)[2].label).toBe(undefined);
  168. expect((result as any)[3].label).toBe(undefined);
  169. expect((result as any)[4].label).toBe(undefined);
  170. });
  171. });