Forum Discussion

jameshaid's avatar
jameshaid
Cloud Apps
08-22-2025
Solved

Column chart with full range x-axis

I'd like to create a column chart for which the x-axis always has values from 0 to 52 in increments of 1. The purpose of the chart is to show how many weeks of inventory remains at each distribution ...
  • TriAnthony's avatar
    TriAnthony
    09-01-2025

    Hi jameshaid​,

    I just sent the file to your registered email. If you're still unable to import the notebook, you can create a new one instead. In the "Create a new Notebook" window, give it a name and add two additional input parameters (located at the bottom of the window). All the other fields are optional.

    Here are the Additional Input Parameters configurations:

    WeekNumberFieldName parameter:

    BreakByFieldName parameter:

    Next, open the code editor (Jupyter Notebook) and delete all existing cells.

    Paste the below code into the first empty cell:

    # Test Cell
    # When the notebook is executed by the widget, this cell is ignored. 
    # See the `Test Cell` section below for further details.
    from init_sisense import sisense_conn
    
    df_input = sisense_conn.get_debug_data()
    additional_parameters = '{\"WeekNumberFieldName\":\"Week Number\", \"BreakByFieldName\":\"Age Range\"}'

    Then paste the below into the next cell:

    import json
    import pandas as pd
    
    # Load the additional_parameters JSON string
    
    try:
        data = json.loads(additional_parameters)
    except json.JSONDecodeError as e:
        print(f"Failed to parse outer JSON: {e}")
        raise
    week_number_field_name = data["WeekNumberFieldName"]
    break_by_field_name = data["BreakByFieldName"]
    
    week_numbers = range(1, 53)
    
    # Get unique values for break_by_field_name
    break_by_field = df_input[break_by_field_name].unique()
    
    # Build full cartesian product of week_number_field_name and break_by_field_name
    full_index = pd.MultiIndex.from_product(
        [week_numbers, break_by_field],
        names=[week_number_field_name, break_by_field_name]
    )
    
    # Rebuild the data frame
    df_result = (
        df_input.set_index([week_number_field_name, break_by_field_name])
                .reindex(full_index, fill_value=0)
                .reset_index()
    )
    
    df_result

    Save the code and switch back to the widget tab. Click Next, enter the names of your Week Number and Break By columns in the input fields. Click Done to close the window, then save the widget.