skip to Main Content

I am suing Apache superset. I want to add custom Y Axis Axis Format in number format. I have tried DEFAULT_METRIC_FORMATS and D3_FORMATS in superset config.Custom number format is not showing in Y Axis Axis Format in number format. Here is the image:

enter image description here

Here is my superset_config.py:

        ROW_LIMIT = 5000

    ENABLE_CORS = True
    CORS_OPTIONS = {
        'origins': ['http://localhost:3000', 'http://mywebsite.com'],
    }

    # ---------------------------------------------------
    # Database config
    # ---------------------------------------------------
    SQLALCHEMY_DATABASE_URI = 'postgresql+psycopg2://superset:superset@postgres:5432/superset'

    # Redis configuration for caching
    CACHE_CONFIG = {
        'CACHE_TYPE': 'RedisCache',
        'CACHE_DEFAULT_TIMEOUT': 300,
        'CACHE_KEY_PREFIX': 'superset_',
        'CACHE_REDIS_URL': 'redis://redis:6379/0',
    }



    FEATURE_FLAGS = {
        "ALERT_REPORTS": True,               # Enable alerts & reports
        "DASHBOARD_NATIVE_FILTERS": True,   # Native filters for dashboards
        "ENABLE_TEMPLATE_PROCESSING": True, # Template processing for queries
        "ENABLE_D3_FORMATS": True,          # Enable D3 number formatting dropdown
        "UX_BETA": True,                    # Optional: Enable beta UI features
    }

    # ---------------------------------------------------
    # Security / Auth configuration
    # ---------------------------------------------------
    SECRET_KEY = '4rA5fxt3Y58tk30FsJ2XpEbg3xD4uPuOYBo5qJ9QlU0'

    AUTH_USER_REGISTRATION = True
    AUTH_USER_REGISTRATION_ROLE = "Gamma"  # Default role for newly registered users

    # ---------------------------------------------------
    # Other configs
    # ---------------------------------------------------
    SUPERSET_WEBSERVER_PORT = 8088

    # Celery configuration for task queuing
    CELERY_CONFIG = {
        'broker_url': 'redis://redis:6379/0',
        'result_backend': 'redis://redis:6379/0',
    }

    # ---------------------------------------------------
    # Logging config
    # ---------------------------------------------------
    LOG_LEVEL = 'INFO'

    # ---------------------------------------------------
    # Customization for branding (optional)
    # ---------------------------------------------------
    APP_NAME = "My Superset"



    DEFAULT_METRIC_FORMATS = {
        "crore": "TK ,.2f Cr",         # Format for Crores (Bangladeshi currency)
        "lakh": "TK ,.2f Lakh",        # Format for Lakhs (Bangladeshi currency)
        "tk_crore": "TK ,.2f Cr",      # Format for Crores with TK prefix
        "tk_lakh": "TK ,.2f Lakh",     # Format for Lakhs with TK prefix
        "usd": "$,.2f",                # Format for US Dollars
        "euro": "€,.2f",               # Format for Euros
        "billion": ",.2f Billion",     # Format for billions
        "default": ",.2f",             # Default fallback format
    }


    EXTRA_CATEGORICAL_COLOR_SCHEMES = [
        {
            "id": "world_population_reporting_colors",
            "label": "World Population Reporting Colors",
            "description": "Custom color scheme for world population reporting",
            "colors": [
                "#004369", "#65D0E4", "#50BEF3", "#65D0E4", "#7D82EA", 
                "#AA5ECB", "#CE42A1", "#EC487D", "#FA6E67", "#FFA064", 
                "#EEDD55", "#9977BB", "#BBAA44", "#DDCCDD",
            ]
        },
        {
            "id": "crore_lakh_colors",
            "label": "Crore and Lakh  test5",
            "description": "Custom colors for crore and lakh number formats",
            "colors": [
                "#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", 
                "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf"
            ],
        },
         {
            "id": "crore_city_bank_colors",
            "label": "City Bank Color",
            "description": "Custom colors for crore and lakh number formats",
            "colors": [
                "#E20001", "#28a745", "#6dbe44", "#6610f2", "#fd7e14", 
                "#17a2b8", "#e377c2", "#7f7f7f", "#bcbd22", "#20c997 "
            ],
        }
          
    ]

My EXTRA_CATEGORICAL_COLOR_SCHEMES is working fine. But DEFAULT_METRIC_FORMATS is not showing in my drop-drown.

What is wrong of my code?

2

Answers


  1. Ok I just checked how we did it and checked the Apache Superset Slack.

    As far as I am aware you can only change the default locals easily in the supserset-config.py.

    We did this like this:

    D3_FORMAT = {
    "decimal": ",",           # - decimal place string (e.g., ".").
    "thousands": ".",         # - group separator string (e.g., ",").
    "grouping": [3],          # - array of group sizes (e.g., [3]), cycled as needed.
    "currency": ["", "€"]     # - currency prefix/suffix strings (e.g., ["$", ""])
    

    }

    I checked on the Superset Slack if you can add number formats and I could not find a way to do it in the superset_config.py. It appears that you can only do this with changes to the codebase, because the number formats are hard coded. This is an answer I found from September of this year:

    To add a custom number format to the Apache Superset UI, you would
    need to modify the source code of Superset and rebuild the project.
    This is because the number formats available in the UI are currently
    hardcoded in the source code. Here are the general steps you would
    need to follow:

    1. Clone the Apache Superset repository to your local machine.
    2. Navigate to the file where the number formats are defined. This is typically in a file named D3Formatting.ts or similar.
    3. Add your custom number format to the list of formats in this file.
    4. Rebuild the project. Please note that this requires a good understanding of JavaScript/TypeScript and the structure of the
      Superset project. You would also need to set up a development
      environment for Superset. Here is an example of how you might add a
      custom number format: js // This is an example and the actual code may
      look different const NUMBER_FORMAT_OPTIONS: { [format: string]: string
      } = { ‘SMART_NUMBER’: t(‘Adaptive Formatting’),
      ‘SMART_NUMBER_SHORT’: t(‘Adaptive Formatting (short)’), ‘.3s’:
      t(‘Compact Number’), // Add your custom format here ‘my_format’:
      t(‘My Custom Format’), // … }; After making these changes and
      rebuilding the project, your custom number format should be available
      in the UI. Please note that this is a simplified example and the
      actual process may be more complex. You should refer to the
      Contributor Guide for detailed instructions on how to set up a
      development environment and contribute to Superset.

    I also found this. So it appears you have to do changes in the codebase.

    Login or Signup to reply.
  2. In my case, I done this:

     D3_FORMAT = {
     "decimal": ",",           # - decimal place string (e.g., ".").
     "thousands": ".",         # - group separator string (e.g., ",").
     "grouping": [3],          # - array of group sizes (e.g., [3]), cycled as needed.
     "currency": ["R$", ""]     # - currency prefix/suffix strings (e.g., ["$", ""]) }
    

    Just paste as indicated and it should work

    Where to place your code

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search