Copilot commented on code in PR #39990: URL: https://github.com/apache/superset/pull/39990#discussion_r3236434657
########## superset-frontend/plugins/plugin-chart-echarts/src/Candlestick/transformProps.ts: ########## @@ -0,0 +1,194 @@ +/** + * 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 { + getMetricLabel, + getNumberFormatter, + getTimeFormatter, + getXAxisLabel, + isPhysicalColumn, + isDefined, + getXAxisColumn, +} from '@superset-ui/core'; +import type { EChartsCoreOption, CandlestickSeriesOption } from 'echarts'; +import type { CallbackDataParams } from 'echarts/types/src/util/types'; +import { + CandlestickChartTransformedProps, + EchartsCandlestickChartProps, +} from './types'; +import { defaultGrid, defaultYAxis } from '../defaults'; +import { getColtypesMapping, sanitizeHtml } from '../utils/series'; +import { getDefaultTooltip } from '../utils/tooltip'; +import { Refs } from '../types'; + +export default function transformProps( + chartProps: EchartsCandlestickChartProps, +): CandlestickChartTransformedProps { + const { width, height, formData, queriesData, hooks, filterState } = chartProps; + const { data = [], verboseMap = {} } = queriesData[0]; + const { setDataMask = () => {}, emitCrossFilters, onContextMenu } = hooks; + const coltypeMapping = getColtypesMapping(queriesData[0]); + const refs: Refs = {} as Refs; + + const { + open: openMetric, + close: closeMetric, + low: lowMetric, + high: highMetric, + zoomable, + } = formData; + + const openLabel = getMetricLabel(openMetric); + const closeLabel = getMetricLabel(closeMetric); + const lowLabel = getMetricLabel(lowMetric); + const highLabel = getMetricLabel(highMetric); + + let xAxisLabel = getXAxisLabel(chartProps.rawFormData) || '__timestamp'; + if ( + isPhysicalColumn(chartProps.rawFormData?.x_axis) && + isDefined(verboseMap[xAxisLabel]) + ) { + xAxisLabel = verboseMap[xAxisLabel]; + } + + const timeFormatter = getTimeFormatter('%Y-%m-%d %H:%M:%S'); + const numberFormatter = getNumberFormatter(); + + const transformedData = data + .map(datum => { + const time = datum[xAxisLabel]; + const o = datum[openLabel]; + const c = datum[closeLabel]; + const l = datum[lowLabel]; + const h = datum[highLabel]; + + if (o == null || c == null || l == null || h == null) { + return null; + } + Review Comment: The code overwrites `xAxisLabel` with `verboseMap[xAxisLabel]` (a display label) and then uses it to index into `datum` (`datum[xAxisLabel]`). `queriesData[0].data` is keyed by the underlying column name, so switching to the verbose label can make `time` undefined and drop all rows. Fix by keeping a stable x-axis *key* for data access (e.g., from `getXAxisColumn(formData)` or the raw `x_axis` field) and using the verbose label only for display (axis naming / tooltip). ########## superset-frontend/plugins/plugin-chart-echarts/src/Candlestick/transformProps.ts: ########## @@ -0,0 +1,194 @@ +/** + * 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 { + getMetricLabel, + getNumberFormatter, + getTimeFormatter, + getXAxisLabel, + isPhysicalColumn, + isDefined, + getXAxisColumn, +} from '@superset-ui/core'; +import type { EChartsCoreOption, CandlestickSeriesOption } from 'echarts'; +import type { CallbackDataParams } from 'echarts/types/src/util/types'; +import { + CandlestickChartTransformedProps, + EchartsCandlestickChartProps, +} from './types'; +import { defaultGrid, defaultYAxis } from '../defaults'; +import { getColtypesMapping, sanitizeHtml } from '../utils/series'; +import { getDefaultTooltip } from '../utils/tooltip'; +import { Refs } from '../types'; + +export default function transformProps( + chartProps: EchartsCandlestickChartProps, +): CandlestickChartTransformedProps { + const { width, height, formData, queriesData, hooks, filterState } = chartProps; + const { data = [], verboseMap = {} } = queriesData[0]; + const { setDataMask = () => {}, emitCrossFilters, onContextMenu } = hooks; + const coltypeMapping = getColtypesMapping(queriesData[0]); + const refs: Refs = {} as Refs; + + const { + open: openMetric, + close: closeMetric, + low: lowMetric, + high: highMetric, + zoomable, + } = formData; + + const openLabel = getMetricLabel(openMetric); + const closeLabel = getMetricLabel(closeMetric); + const lowLabel = getMetricLabel(lowMetric); + const highLabel = getMetricLabel(highMetric); + + let xAxisLabel = getXAxisLabel(chartProps.rawFormData) || '__timestamp'; + if ( + isPhysicalColumn(chartProps.rawFormData?.x_axis) && + isDefined(verboseMap[xAxisLabel]) + ) { + xAxisLabel = verboseMap[xAxisLabel]; + } + + const timeFormatter = getTimeFormatter('%Y-%m-%d %H:%M:%S'); + const numberFormatter = getNumberFormatter(); + + const transformedData = data + .map(datum => { + const time = datum[xAxisLabel]; + const o = datum[openLabel]; + const c = datum[closeLabel]; + const l = datum[lowLabel]; + const h = datum[highLabel]; + + if (o == null || c == null || l == null || h == null) { + return null; + } + + return [time, o, c, l, h]; + }) + .filter(Boolean) as [any, number, number, number, number][]; + + const series: CandlestickSeriesOption[] = [ + { + name: 'OHLC', + type: 'candlestick', + data: transformedData.map(row => row.slice(1)), + itemStyle: { + // International standard: Green for Up, Red for Down + color: '#14b143', + color0: '#ef232a', + borderColor: '#14b143', + borderColor0: '#ef232a', + }, + }, Review Comment: The control panel exposes `color_scheme`, but the series colors are hard-coded here, so user-selected color schemes will have no effect. Either wire `formData.colorScheme` into the candlestick up/down colors (preferred), or remove `color_scheme` from the control panel to avoid a non-functional control. ########## superset-frontend/plugins/plugin-chart-echarts/src/Candlestick/transformProps.ts: ########## @@ -0,0 +1,194 @@ +/** + * 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 { + getMetricLabel, + getNumberFormatter, + getTimeFormatter, + getXAxisLabel, + isPhysicalColumn, + isDefined, + getXAxisColumn, +} from '@superset-ui/core'; +import type { EChartsCoreOption, CandlestickSeriesOption } from 'echarts'; +import type { CallbackDataParams } from 'echarts/types/src/util/types'; +import { + CandlestickChartTransformedProps, + EchartsCandlestickChartProps, +} from './types'; +import { defaultGrid, defaultYAxis } from '../defaults'; +import { getColtypesMapping, sanitizeHtml } from '../utils/series'; +import { getDefaultTooltip } from '../utils/tooltip'; +import { Refs } from '../types'; + +export default function transformProps( + chartProps: EchartsCandlestickChartProps, +): CandlestickChartTransformedProps { + const { width, height, formData, queriesData, hooks, filterState } = chartProps; + const { data = [], verboseMap = {} } = queriesData[0]; + const { setDataMask = () => {}, emitCrossFilters, onContextMenu } = hooks; + const coltypeMapping = getColtypesMapping(queriesData[0]); + const refs: Refs = {} as Refs; + + const { + open: openMetric, + close: closeMetric, + low: lowMetric, + high: highMetric, + zoomable, + } = formData; + + const openLabel = getMetricLabel(openMetric); + const closeLabel = getMetricLabel(closeMetric); + const lowLabel = getMetricLabel(lowMetric); + const highLabel = getMetricLabel(highMetric); + + let xAxisLabel = getXAxisLabel(chartProps.rawFormData) || '__timestamp'; + if ( + isPhysicalColumn(chartProps.rawFormData?.x_axis) && + isDefined(verboseMap[xAxisLabel]) + ) { + xAxisLabel = verboseMap[xAxisLabel]; + } + + const timeFormatter = getTimeFormatter('%Y-%m-%d %H:%M:%S'); + const numberFormatter = getNumberFormatter(); + + const transformedData = data + .map(datum => { + const time = datum[xAxisLabel]; + const o = datum[openLabel]; + const c = datum[closeLabel]; + const l = datum[lowLabel]; + const h = datum[highLabel]; + + if (o == null || c == null || l == null || h == null) { + return null; + } + + return [time, o, c, l, h]; + }) + .filter(Boolean) as [any, number, number, number, number][]; Review Comment: This introduces non-trivial data shaping (x-axis extraction + OHLC mapping + null-row dropping) that is easy to break (especially around the x-axis key/verbose label handling). Add unit tests for `transformProps` covering: (1) correct x-axis value extraction when `verboseMap` is present, (2) correct OHLC ordering `[open, close, low, high]` in series data, and (3) dropping rows with any missing OHLC value. ########## superset-frontend/plugins/plugin-chart-echarts/src/Candlestick/buildQuery.ts: ########## @@ -0,0 +1,42 @@ +/** + * 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 { buildQueryContext, getXAxisColumn } from '@superset-ui/core'; +import { CandlestickQueryFormData } from './types'; + +export default function buildQuery(formData: CandlestickQueryFormData) { + return buildQueryContext(formData, baseQueryObject => { + // Collect all OHLC metrics and remove duplicates + const metrics = Array.from( + new Set( + [formData.open, formData.close, formData.high, formData.low].filter( + Boolean, + ), + ), + ); Review Comment: Deduping metrics via `new Set(...)` only works reliably for primitive metrics (strings). For ad-hoc metrics (objects), identical metrics can still appear multiple times because Set uses reference equality. This can lead to duplicate columns/labels in query results and ambiguous `getMetricLabel` lookups. Prefer deduping by a stable identity (e.g., `getMetricLabel(metric)`), keeping the first occurrence. -- 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]
