sadpandajoe commented on code in PR #38028:
URL: https://github.com/apache/superset/pull/38028#discussion_r4151427876


##########
superset-frontend/plugins/legacy-preset-chart-deckgl/src/layers/H3Hexagon/buildQuery.ts:
##########
@@ -0,0 +1,93 @@
+/**
+ * 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,
+  ensureIsArray,
+  getColumnLabel,
+  normalizeOrderBy,
+  QueryFormColumn,
+  QueryFormData,
+} from '@superset-ui/core';
+import { addTooltipColumnsToQuery } from '../buildQueryUtils';
+
+export interface H3FormData extends QueryFormData {
+  h3_index: QueryFormColumn | QueryFormColumn[];
+  metric?: string;
+  js_columns?: string[];
+  tooltip_contents?: unknown[];
+}
+
+export default function buildQuery(formData: H3FormData) {
+  const {
+    h3_index: h3IndexRaw,
+    metric,
+    js_columns: jsColumns,
+    tooltip_contents: tooltipContents,
+  } = formData;
+
+  const h3Index: QueryFormColumn | undefined = Array.isArray(h3IndexRaw)
+    ? h3IndexRaw[0]
+    : h3IndexRaw;
+
+  if (!h3Index) {
+    throw new Error('H3 index is required');
+  }
+
+  const h3IndexLabel = getColumnLabel(h3Index);
+
+  return buildQueryContext(formData, {
+    buildQuery: baseQueryObject => {
+      let columns: QueryFormColumn[] = [h3Index];
+      const metrics = metric ? [metric] : [];
+
+      if (jsColumns?.length) {
+        const existingLabels = new Set(columns.map(getColumnLabel));
+        jsColumns.forEach((col: string) => {
+          if (!existingLabels.has(col)) {
+            columns.push(col);
+            existingLabels.add(col);
+          }
+        });
+      }
+
+      columns = addTooltipColumnsToQuery(columns, tooltipContents);
+
+      const filters = ensureIsArray(baseQueryObject.filters || []);
+      filters.push({
+        col: h3IndexLabel,

Review Comment:
   For an ad-hoc H3 expression, this label is not a physical dataset column, so 
the backend rejects the null filter instead of applying it. A NULL bucket can 
then consume the row limit ahead of valid cells; could we pass the ad-hoc 
column object here and update the test to assert that payload?



##########
superset-frontend/plugins/legacy-preset-chart-deckgl/src/layers/H3Hexagon/H3Hexagon.tsx:
##########
@@ -0,0 +1,208 @@
+/**
+ * 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 { H3HexagonLayer } from '@deck.gl/geo-layers';
+import { cellToBoundary } from 'h3-js';
+import { JsonObject, QueryFormData } from '@superset-ui/core';
+import { t } from '@apache-superset/core';
+import { Color } from '@deck.gl/core';
+import { commonLayerProps, getColorForBreakpoints } from '../common';
+import { createDeckGLComponent, GetLayerType } from '../../factory';
+import TooltipRow from '../../TooltipRow';
+import { createTooltipContent } from '../../utilities/tooltipUtils';
+import sandboxedEval from '../../utils/sandbox';
+import {
+  BucketsWithColorScale,
+  getBreakPointColorScaler,
+  TRANSPARENT_COLOR_ARRAY,
+} from '../../utils';
+import { COLOR_SCHEME_TYPES } from '../../utilities/utils';
+import { DEFAULT_DECKGL_COLOR } from '../../utilities/Shared_DeckGL';
+import { H3Feature } from './transformProps';
+
+function defaultTooltipContent(_formData: QueryFormData) {
+  const TooltipContent = (o: JsonObject) => {
+    const obj = o.object as H3Feature | undefined;
+    return (
+      <div className="deckgl-tooltip">
+        <TooltipRow label={`${t('H3 Index')}: `} value={obj?.hexagon ?? ''} />
+        {obj?.elevation !== undefined && (
+          <TooltipRow label={`${t('Value')}: `} value={`${obj.elevation}`} />
+        )}
+      </div>
+    );
+  };
+  TooltipContent.displayName = 'H3HexagonTooltipContent';
+  return TooltipContent;
+}
+
+export function getPoints(data: H3Feature[]) {
+  const points: [number, number][] = [];
+
+  data.forEach(d => {
+    const { hexagon } = d;
+    if (!hexagon) {
+      return;
+    }
+
+    try {
+      const boundary = cellToBoundary(hexagon);
+      if (boundary && boundary.length > 0) {
+        const point: [number, number] = [boundary[0][1], boundary[0][0]];
+        points.push(point);
+      }
+    } catch {
+      // Skip entries with invalid H3 indices that cause cellToBoundary to 
throw
+    }
+  });
+
+  return points;
+}
+
+export const getLayer: GetLayerType<H3HexagonLayer> = function ({
+  formData,
+  payload,
+  setTooltip,
+  setDataMask,
+  filterState,
+  onContextMenu,
+  emitCrossFilters,
+}) {
+  const fd = formData;
+  const {
+    extruded = true,
+    coverage = 1,
+    elevation_scale: elevationScale = 1,
+  } = fd;
+
+  let data = [...payload.data.features];
+
+  if (fd.js_data_mutator) {
+    const jsFnMutator = sandboxedEval(fd.js_data_mutator);
+    data = jsFnMutator(data);
+  }
+
+  const fc = fd.fill_color_picker as
+    | { r: number; g: number; b: number; a: number }
+    | undefined;
+  const defaultBreakpointColor = fd.default_breakpoint_color as
+    | { r: number; g: number; b: number; a: number }
+    | undefined;
+  const fixedColor: Color = fc
+    ? [fc.r, fc.g, fc.b, 255 * fc.a]
+    : [
+        DEFAULT_DECKGL_COLOR.r,
+        DEFAULT_DECKGL_COLOR.g,
+        DEFAULT_DECKGL_COLOR.b,
+        DEFAULT_DECKGL_COLOR.a * 255,
+      ];
+
+  const colorSchemeType = fd.color_scheme_type;
+  const accessor = (d: JsonObject) =>
+    typeof d?.elevation === 'number' ? d.elevation : undefined;
+
+  let getFillColor: (d: JsonObject) => Color;
+  switch (colorSchemeType) {
+    case COLOR_SCHEME_TYPES.fixed_color: {
+      getFillColor = () => fixedColor;
+      break;
+    }
+    case COLOR_SCHEME_TYPES.linear_palette: {
+      // Without a metric there is nothing to scale by, fall back to the fixed 
color
+      getFillColor = fd.metric

Review Comment:
   With the default linear palette, a valid cell whose metric is 0 becomes 
transparent because `getBreakPointColorScaler` treats a falsy accessor value as 
missing. Could we preserve zero-valued cells and cover that case in the layer 
tests?



-- 
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]

Reply via email to