rusackas commented on code in PR #42910: URL: https://github.com/apache/superset/pull/42910#discussion_r4385135816
########## superset-frontend/src/explore/components/controls/IntervalColorsControl/legacyColors.ts: ########## @@ -0,0 +1,89 @@ +/** + * 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 { + getCategoricalSchemeRegistry, + validateNumber, +} from '@superset-ui/core'; + +export const parseIntervalBounds = (intervals?: string): number[] => + (intervals ?? '') + .split(',') + .map(part => part.trim()) + .filter(part => part !== '') + .map(Number) + .filter(bound => Number.isFinite(bound)); + +/** + * Resolves the legacy `interval_color_indices` control (comma-separated, + * 1-indexed positions into the chosen categorical color scheme) into real + * hex colors, one per interval band (`boundCount` entries). Shared by the + * control panel (display-only fallback while a legacy chart is being edited) + * and by `handleDeprecatedControls` (which, on request, migrates the value + * into `interval_colors`). + */ +export const resolveLegacyIntervalColors = ( + boundCount: number, + legacyIntervalColorIndices: string | undefined, + colorScheme: string | undefined, +): string[] => { + // Non-strict lookup mirrors the Gauge renderer: an unregistered scheme + // falls back to the registry's default scheme. + const schemeColors = + getCategoricalSchemeRegistry().get(colorScheme)?.colors ?? []; + // Parse with the exact same semantics `getIntervalBoundsAndColors` in + // Gauge/transformProps.ts uses for this same legacy fallback (via + // `parseNumbersList`, not reimported here to avoid pulling the whole + // `@superset-ui/plugin-chart-echarts` package -- and every chart plugin it + // eagerly registers -- into `explore/store.ts`'s import graph): one + // unparseable (or blank) token discards every legacy index for the whole + // list, rather than resolving the other tokens independently. A more + // lenient parser here would let this one-time migration (and this + // display-only preview) disagree with how an untouched legacy chart has + // always rendered -- see #42910 review thread on this file. + let indices: number[]; + try { + indices = (legacyIntervalColorIndices ?? '').split(',').map(part => { + if (validateNumber(part)) throw new Error('All values must be numeric'); + return Number(part); + }); + } catch { + indices = []; + } + // An index past the static palette can only be resolved against the + // renderer's render-time palette (which grows under USE_ANALOGOUS_COLORS), + // so wrapping it modulo the static scheme would show -- and, once a row is + // edited, persist -- a different color than the chart renders. Leave every + // band unresolved so such charts keep rendering through the renderer's own + // legacy path. + if (indices.some(index => index > schemeColors.length)) { Review Comment: Good catch, took your suggestion: only the first `boundCount` indices are checked now, with a test for `2,3,7` on a 3-color scheme. ########## superset-frontend/plugins/plugin-chart-echarts/test/Gauge/transformProps.test.ts: ########## @@ -687,41 +764,121 @@ describe('Min/Max calculation and axis labels', () => { }); describe('getIntervalBoundsAndColors', () => { - test('should generate correct interval bounds and colors', () => { + test('should generate correct interval bounds and colors from legacy indices', () => { const colorFn = CategoricalColorNamespace.getScale( 'supersetColors' as string, ); - expect(getIntervalBoundsAndColors('', '', colorFn, 0, 10)).toEqual([]); - expect(getIntervalBoundsAndColors('4, 10', '1, 2', colorFn, 0, 10)).toEqual( - [ - [0.4, '#1f77b4'], - [1, '#ff7f0e'], - ], - ); expect( - getIntervalBoundsAndColors('4, 8, 10', '9, 8, 7', colorFn, 0, 10), + getIntervalBoundsAndColors('', '', undefined, colorFn, 0, 10), + ).toEqual([]); + expect( + getIntervalBoundsAndColors('4, 10', '1, 2', undefined, colorFn, 0, 10), + ).toEqual([ + [0.4, '#1f77b4'], + [1, '#ff7f0e'], + ]); + expect( + getIntervalBoundsAndColors( + '4, 8, 10', + '9, 8, 7', + undefined, + colorFn, + 0, + 10, + ), ).toEqual([ [0.4, '#bcbd22'], [0.8, '#7f7f7f'], [1, '#e377c2'], ]); - expect(getIntervalBoundsAndColors('4, 10', '1, 2', colorFn, 2, 10)).toEqual( - [ - [0.25, '#1f77b4'], - [1, '#ff7f0e'], - ], - ); expect( - getIntervalBoundsAndColors('-4, 0', '1, 2', colorFn, -10, 0), + getIntervalBoundsAndColors('4, 10', '1, 2', undefined, colorFn, 2, 10), + ).toEqual([ + [0.25, '#1f77b4'], + [1, '#ff7f0e'], + ]); + expect( + getIntervalBoundsAndColors('-4, 0', '1, 2', undefined, colorFn, -10, 0), ).toEqual([ [0.6, '#1f77b4'], [1, '#ff7f0e'], ]); expect( - getIntervalBoundsAndColors('-4, -2', '1, 2', colorFn, -10, -2), + getIntervalBoundsAndColors('-4, -2', '1, 2', undefined, colorFn, -10, -2), ).toEqual([ [0.75, '#1f77b4'], [1, '#ff7f0e'], ]); }); + + test('should prefer interval_colors (real hex values) over legacy indices when present', () => { + const colorFn = CategoricalColorNamespace.getScale( + 'supersetColors' as string, + ); + expect( + getIntervalBoundsAndColors( + '4, 10', + '1, 2', + ['#ff0000', '#00ff00'], + colorFn, + 0, + 10, + ), + ).toEqual([ + [0.4, '#ff0000'], + [1, '#00ff00'], + ]); + }); + + test('should fall back to the color scheme for bounds missing an explicit interval_colors entry', () => { + const colorFn = CategoricalColorNamespace.getScale( + 'supersetColors' as string, + ); + expect( + getIntervalBoundsAndColors('4, 8, 10', '', ['#ff0000'], colorFn, 0, 10), + ).toEqual([ + [0.4, '#ff0000'], + [0.8, colorFn.colors[1]], + [1, colorFn.colors[2]], + ]); + }); + + test('wraps the interval_colors fallback around the scheme like the editor does', () => { + // With more bounds than colors in the scheme, a bound past the end of + // `intervalColors` must wrap around the scheme the same way + // `IntervalColorsControl`'s own display does (`index % schemeColors.length`), + // rather than indexing straight off the end into `undefined`. + const colorFn = new CategoricalColorScale([ + '#1f77b4', + '#ff7f0e', + '#2ca02c', + ]); + expect( + getIntervalBoundsAndColors( + '4, 6, 8, 10', + '', + ['#ff0000'], + colorFn, + 0, + 10, + ), + ).toEqual([ + [0.4, '#ff0000'], + [0.6, '#ff7f0e'], + [0.8, '#2ca02c'], + [1, '#1f77b4'], + ]); + }); + + test('an empty interval_colors array falls back to legacy indices', () => { + const colorFn = CategoricalColorNamespace.getScale( + 'supersetColors' as string, + ); + expect( + getIntervalBoundsAndColors('4, 10', '1, 2', [], colorFn, 0, 10), Review Comment: Switched the test to `2, 1` and asserted `[0.4, '#ff7f0e'], [1, '#1f77b4']`, so it now pins the legacy path rather than positional order. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
