bito-code-review[bot] commented on code in PR #41490: URL: https://github.com/apache/superset/pull/41490#discussion_r3485858715
########## superset-frontend/plugins/preset-chart-deckgl/src/utils/addColor.ts: ########## @@ -0,0 +1,113 @@ +/** + * 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 { + CategoricalColorNamespace, + JsonObject, + QueryFormData, +} from '@superset-ui/core'; +import { hexToRGB } from './colors'; +import { ColorBreakpointType } from '../types'; +import { COLOR_SCHEME_TYPES, ColorSchemeType } from '../utilities/utils'; +import { DEFAULT_DECKGL_COLOR } from '../utilities/Shared_DeckGL'; + +const { getScale } = CategoricalColorNamespace; + +/** + * Resolve the per-feature color for a deck.gl layer based on the form data's + * color scheme configuration. This mirrors the categorical/fixed/breakpoint + * color logic that `CategoricalDeckGLContainer` applies when a layer is + * rendered on its own, so that it can be reused when layers are composed + * inside the deck.gl Multiple Layers chart. + * + * Features whose color scheme is not recognized are returned unchanged so the + * layer's own fallback color logic can take over. + */ +export function addColorToFeatures( + data: JsonObject[], + fd: QueryFormData, + selectedColorScheme: ColorSchemeType = fd.color_scheme_type, +): JsonObject[] { + const appliedScheme = fd.color_scheme; + const colorFn = getScale(appliedScheme); + + switch (selectedColorScheme) { + case COLOR_SCHEME_TYPES.fixed_color: { + const color = fd.color_picker || { r: 0, g: 0, b: 0, a: 1 }; + const colorArray = [color.r, color.g, color.b, color.a * 255]; + + return data.map(d => ({ ...d, color: colorArray })); + } + case COLOR_SCHEME_TYPES.categorical_palette: { + if (!fd.dimension) { + const fallbackColor = fd.color_picker || { r: 0, g: 0, b: 0, a: 1 }; + const colorArray = [ + fallbackColor.r, + fallbackColor.g, + fallbackColor.b, + fallbackColor.a * 255, + ]; + return data.map(d => ({ ...d, color: colorArray })); + } + + return data.map(d => ({ + ...d, + color: hexToRGB(colorFn(d.cat_color, fd.slice_id)), + })); Review Comment: <!-- Bito Reply --> The reviewer's suggestion to use `fd.sliceId` is based on the assumption that `QueryFormData` uses camelCase for this property. However, as you noted, the codebase consistently uses the snake_case `slice_id` key for form data across deck.gl layers. Given that `slice_id` is the established convention in this context, the suggestion to switch to `sliceId` is incorrect and should not be applied. **superset-frontend/plugins/preset-chart-deckgl/src/utils/addColor.ts** ``` return data.map(d => ({ ...d, color: hexToRGB(colorFn(d.cat_color, fd.slice_id)), })); ``` ########## superset-frontend/plugins/preset-chart-deckgl/src/utils/addColor.test.ts: ########## @@ -0,0 +1,83 @@ +/** + * 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 { QueryFormData } from '@superset-ui/core'; +import { addColorToFeatures } from './addColor'; +import { COLOR_SCHEME_TYPES } from '../utilities/utils'; + +const baseFormData = { + datasource: '1__table', + viz_type: 'deck_scatter', +} as unknown as QueryFormData; + +test('assigns distinct colors per category for a categorical palette', () => { + const features = [{ cat_color: 'A' }, { cat_color: 'B' }, { cat_color: 'A' }]; + const result = addColorToFeatures(features, { + ...baseFormData, + color_scheme_type: COLOR_SCHEME_TYPES.categorical_palette, + color_scheme: 'supersetColors', + dimension: 'category', + slice_id: 1, + } as unknown as QueryFormData); + + // Each feature gets a resolved RGBA color + result.forEach(d => { + expect(Array.isArray(d.color)).toBe(true); + expect(d.color).toHaveLength(4); + }); + // Same category resolves to the same color, different categories differ + expect(result[0].color).toEqual(result[2].color); + expect(result[0].color).not.toEqual(result[1].color); +}); + +test('falls back to the fixed color picker when no dimension is set', () => { + const features = [{ cat_color: 'A' }, { cat_color: 'B' }]; + const result = addColorToFeatures(features, { + ...baseFormData, + color_scheme_type: COLOR_SCHEME_TYPES.categorical_palette, + color_picker: { r: 10, g: 20, b: 30, a: 1 }, + } as unknown as QueryFormData); + + result.forEach(d => { + expect(d.color).toEqual([10, 20, 30, 255]); + }); +}); + +test('applies the fixed color scheme to every feature', () => { + const features = [{ cat_color: 'A' }, { cat_color: 'B' }]; + const result = addColorToFeatures(features, { + ...baseFormData, + color_scheme_type: COLOR_SCHEME_TYPES.fixed_color, + color_picker: { r: 1, g: 2, b: 3, a: 0.5 }, + } as unknown as QueryFormData); + + result.forEach(d => { + expect(d.color).toEqual([1, 2, 3, 127.5]); + }); +}); + +test('returns features unchanged for an unrecognized color scheme', () => { + const features = [{ cat_color: 'A' }]; + const result = addColorToFeatures(features, { + ...baseFormData, + color_scheme_type: 'something_else', + } as unknown as QueryFormData); + + expect(result).toEqual(features); + expect(result[0].color).toBeUndefined(); +}); Review Comment: <!-- Bito Reply --> The addition of the `color_breakpoints` test case effectively addresses the reviewer's concern regarding missing test coverage for the metric-based breakpoint logic. This update ensures that both in-range matches and the default fallback behavior are verified, which improves the robustness of the `addColorToFeatures` utility. -- 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]
