rusackas commented on code in PR #42910: URL: https://github.com/apache/superset/pull/42910#discussion_r4172454425
########## superset-frontend/src/explore/components/controls/IntervalColorsControl/index.tsx: ########## @@ -0,0 +1,100 @@ +/** + * 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 { t } from '@apache-superset/core/translation'; +import { getCategoricalSchemeRegistry } from '@superset-ui/core'; +import { isRangesInputComplete } from '@superset-ui/plugin-chart-echarts'; +import ControlHeader from '../../ControlHeader'; +import ColorPickerControl from '../ColorPickerControl'; +import type { ColorPickerValue } from '../ColorPickerControl'; +import { + RangeRow as IntervalRow, + RangeLabel as BoundLabel, + replaceColorAtIndex, +} from '../shared/RangeColorRow'; +import { IntervalColorsControlProps } from './types'; +import { + parseIntervalBounds as parseBounds, + resolveLegacyIntervalColors as resolveLegacyColors, +} from './legacyColors'; + +/** + * Per-interval color editor for the Gauge chart. Row *count* is driven by + * the sibling `intervals` control (one row per parsed upper bound) so bound + * values keep a single source of truth; this control only owns colors, + * stored as an array of hex strings positionally matched to those bounds. + */ +export default function IntervalColorsControl({ + value, + onChange, + intervals, + legacyIntervalColorIndices, + colorScheme, + ...headerProps +}: IntervalColorsControlProps) { + const bounds = parseBounds(intervals); + // `parseBounds` leniently drops blank tokens (e.g. mid-edit "20,,60"), + // which would otherwise shift colors to the wrong bound once the blank is + // filled back in -- same hazard `BulletRangeColorsControl` guards against. + const boundsComplete = isRangesInputComplete(intervals); + const legacyColors = resolveLegacyColors( + bounds, + legacyIntervalColorIndices, + colorScheme, + ); + const schemeColors = + getCategoricalSchemeRegistry().get(colorScheme)?.colors ?? []; + + const colorAt = (index: number): string => + value?.[index] || + legacyColors[index] || + schemeColors[index % (schemeColors.length || 1)] || + ''; + + const handleColorChange = (index: number) => (color: ColorPickerValue) => { + if (typeof color !== 'string' || !boundsComplete) return; + onChange?.(replaceColorAtIndex(bounds.length, index, color, colorAt)); + }; + + return ( + <div> + <ControlHeader {...headerProps} /> + {bounds.length === 0 ? ( + <BoundLabel> + {t('Add interval bounds above to configure colors here.')} + </BoundLabel> + ) : ( + bounds.map((bound, index) => ( + // eslint-disable-next-line react/no-array-index-key + <IntervalRow key={index}> Review Comment: Switched to `t("Up to %s", bound)` in 5295540. ########## superset-frontend/src/explore/components/controls/IntervalColorsControl/legacyColors.ts: ########## @@ -0,0 +1,71 @@ +/** + * 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 } 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, positionally matched to `bounds`. Shared by the control panel + * (display-only fallback while a legacy chart is being edited) and by + * `handleDeprecatedControls` (which migrates the value into `interval_colors` + * on load, since `interval_color_indices` is no longer a registered control + * and would otherwise be silently dropped on the next save). + */ +export const resolveLegacyIntervalColors = ( + bounds: number[], + legacyIntervalColorIndices: string | undefined, + colorScheme: string | undefined, +): string[] => { + // strict: an explicitly-named but unregistered scheme should resolve to + // "no colors" here, not silently fall back to the registry's default + // scheme -- that fallback belongs to the display layer (index.tsx), not + // to this migration helper. + const schemeColors = + getCategoricalSchemeRegistry().get(colorScheme, true)?.colors ?? []; + const indices = (legacyIntervalColorIndices ?? '') + .split(',') + .map(part => part.trim()) + .map(part => (part === '' ? NaN : Number(part))); + // Bounds without an explicit legacy index cycle through the scheme on + // their own counter, independent of their position among the bounds that + // *do* have one -- e.g. indices "2,3" for 3 bounds should leave the third + // (uncovered) bound on the first scheme color, not the third. + let missingCount = 0; + return bounds.map((_, index) => { + const legacyIndex = indices[index]; + if (schemeColors.length === 0) return ''; + if (Number.isFinite(legacyIndex)) { + return schemeColors[ + (((legacyIndex - 1) % schemeColors.length) + schemeColors.length) % + schemeColors.length + ]; + } + const fallbackColor = schemeColors[missingCount % schemeColors.length]; Review Comment: @sadpandajoe good catch. Bounds without a legacy index now keep their positional scheme color, same as the renderer, so `20,40,60` with `2,3` gives the third band the third color. Migration test updated to assert that. ########## superset-frontend/src/explore/components/controls/IntervalColorsControl/legacyColors.ts: ########## @@ -0,0 +1,71 @@ +/** + * 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 } 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, positionally matched to `bounds`. Shared by the control panel + * (display-only fallback while a legacy chart is being edited) and by + * `handleDeprecatedControls` (which migrates the value into `interval_colors` + * on load, since `interval_color_indices` is no longer a registered control + * and would otherwise be silently dropped on the next save). + */ +export const resolveLegacyIntervalColors = ( + bounds: number[], + legacyIntervalColorIndices: string | undefined, + colorScheme: string | undefined, +): string[] => { + // strict: an explicitly-named but unregistered scheme should resolve to + // "no colors" here, not silently fall back to the registry's default + // scheme -- that fallback belongs to the display layer (index.tsx), not + // to this migration helper. + const schemeColors = + getCategoricalSchemeRegistry().get(colorScheme, true)?.colors ?? []; Review Comment: @sadpandajoe agreed. The resolver now does a non-strict lookup, so an unregistered scheme falls back to the default palette like the renderer does. Test updated accordingly. -- 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]
