apache / superset

Apache Superset is a Data Visualization and Data Exploration Platform
https://superset.apache.org/
Apache License 2.0
61.6k stars 13.45k forks source link

Metric datatype incorrect when swap dataset #29227

Open rad-pat opened 2 months ago

rad-pat commented 2 months ago

Bug description

Swapping the dataset of a chart using the numeric metric of the original dataset to another dataset that has a numeric metric of the same name fails converts the metric datatype to text and loses the custom formatting.

How to reproduce the bug

Create dataset 1 with numeric metric "Metric1", sum(some numeric column), apply D3 formatting $,.2f Create dataset 2 with the same metric name and formatting. Create table chart1 from dataset1 add columns and metric1 Create chart and see formatting on metric column Save Chart Edit Chart Swap dataset to dataset2 Click continue on Continue on message about controls will be retained. View "Customize" tab, metric appears to be numeric Click "Update Chart" - metric now switches to text and formatting is lost.

Screenshots/recordings

No response

Superset version

3.1.3

Python version

3.10

Node version

I don't know

Browser

Chrome

Additional context

No response

Checklist

dosubot[bot] commented 2 months ago

Hey @rad-pat! I'm here to help you with the issue you've encountered. I'm a bot that can assist you with bug resolution, answer your questions, and guide you on becoming a contributor. Let's dive into solving this together!

To resolve the issue in Superset version 3.1.3 where swapping the dataset of a chart causes a numeric metric to incorrectly convert to text and lose its custom formatting, you can leverage the SHARED_COLUMN_CONFIG_PROPS and DEFAULT_CONFIG_FORM_LAYOUT constants from the superset-frontend/src/explore/components/controls/ColumnConfigControl/constants.tsx file. These constants define the configurable column formatting properties and their default layouts, ensuring that numeric metrics retain their formatting.

Here is how you can ensure the correct formatter is applied:

  1. Import the necessary constants:
import { SHARED_COLUMN_CONFIG_PROPS, DEFAULT_CONFIG_FORM_LAYOUT } from 'path-to-constants';
  1. Use the SHARED_COLUMN_CONFIG_PROPS to define your column formatting properties:
const columnConfigProps = {
  ...SHARED_COLUMN_CONFIG_PROPS,
  // Add any additional custom properties if needed
};
  1. Apply the DEFAULT_CONFIG_FORM_LAYOUT to ensure the correct layout and formatting:
const configFormLayout = {
  ...DEFAULT_CONFIG_FORM_LAYOUT,
  // Customize the layout if necessary
};
  1. Ensure the correct formatter is applied to your numeric metrics:
const formatNumericMetric = (metricValue) => {
  const formatter = columnConfigProps.d3NumberFormat;
  return formatter(metricValue);
};

// Example usage
const formattedValue = formatNumericMetric(yourMetricValue);

By using the SHARED_COLUMN_CONFIG_PROPS and DEFAULT_CONFIG_FORM_LAYOUT, you can ensure that numeric metrics retain their custom formatting when swapping datasets, thus resolving the issue [1][2].

To continue talking to Dosu, mention @dosu.