File size: 7,105 Bytes
4cfea5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"""AG Grid table configurations for the Superstore BI dashboard."""

from dash_ag_grid import AgGrid
from data_processing import make_customer_sales_pareto_df
from vizro.models.types import capture
from vizro.tables import dash_ag_grid

# TODO: check and refactor
CELL_STYLE_PRODUCT = {
    "styleConditions": [
        {
            "condition": "params.value < -0.5",
            "style": {"backgroundColor": "#e33b3b"},
        },
        {
            "condition": "params.value >= -0.5 && params.value <= 0",
            "style": {"backgroundColor": "#f19791"},
        },
        {
            "condition": "params.value > 0 && params.value <= 0.30",
            "style": {"backgroundColor": "#728aff"},
        },
        {
            "condition": "params.value > 0.30",
            "style": {"backgroundColor": "#2251ff"},
        },
    ]
}


COLUMN_DEFS_PRODUCT = [
    {"field": "Sub-Category", "cellDataType": "text", "headerName": "Sub-Category", "flex": 3},
    {
        "field": "Profit",
        "cellDataType": "number",
        "flex": 2,
        "valueFormatter": {"function": "d3.format('$,.2f')(params.value)"},
    },
    {
        "field": "Sales",
        "cellDataType": "number",
        "flex": 2,
        "valueFormatter": {"function": "d3.format('$,.2f')(params.value)"},
    },
    {
        "field": "Profit Margin",
        "flex": 2,
        "cellDataType": "number",
        "valueFormatter": {"function": "d3.format('.0%')(params.value)"},
        "cellStyle": CELL_STYLE_PRODUCT,
    },
]


@capture("ag_grid")
def orders_ag_grid(data_frame):
    """Create custom AG Grid table for orders with conditional formatting.

    Args:
        data_frame: Source dataframe containing order data.

    Returns:
        AgGrid: Configured AG Grid component with custom column definitions and styling.
    """
    data_frame["Profit Ratio"] = (data_frame["Profit"] / data_frame["Sales"]).round(3)
    column_defs_orders = [
        {"headerName": "Order ID", "field": "Order ID", "minWidth": 150},
        {"headerName": "Status", "field": "Order Status", "minWidth": 150, "cellRenderer": "statusCellRenderer"},
        {
            "headerName": "Segment",
            "field": "Segment",
            "minWidth": 140,
        },
        {"headerName": "Customer", "field": "Customer Name", "minWidth": 170},
        {"headerName": "State", "field": "State", "minWidth": 150},
        {"headerName": "City", "field": "City", "minWidth": 150},
        {"headerName": "Category", "field": "Category", "minWidth": 150},
        {"headerName": "Sub-Category", "field": "Sub-Category", "minWidth": 150},
        {"headerName": "Sales", "field": "Sales", "valueFormatter": {"function": "d3.format('$,.2f')(params.value)"}},
        {
            "headerName": "Profit",
            "field": "Profit",
            "valueFormatter": {"function": "d3.format('$,.2f')(params.value)"},
        },
        {
            "headerName": "Profit Ratio",
            "field": "Profit Ratio",
            "minWidth": 140,
            "valueFormatter": {"function": "d3.format('.1%')(params.value)"},
            "cellStyle": {
                "styleConditions": [
                    {
                        "condition": "Number(params.value) < -0.5",
                        "style": {
                            "backgroundColor": "#e33b3b",
                            "color": "white",
                            "borderRadius": "18px",
                            "padding": "4px",
                            "fontWeight": "600",
                            "justifyContent": "center",
                            "alignItems": "center",
                            "display": "flex",
                            "marginTop": "8px",
                            "height": "30px",
                        },
                    },
                    {
                        "condition": "Number(params.value) >= -0.5 && Number(params.value) < 0",
                        "style": {
                            "backgroundColor": "#f19791",
                            "color": "white",
                            "borderRadius": "18px",
                            "padding": "4px",
                            "fontWeight": "600",
                            "justifyContent": "center",
                            "alignItems": "center",
                            "display": "flex",
                            "marginTop": "8px",
                            "height": "30px",
                        },
                    },
                    {
                        "condition": "Number(params.value) >= 0 && Number(params.value) < 0.30",
                        "style": {
                            "backgroundColor": "#728aff",
                            "color": "white",
                            "borderRadius": "18px",
                            "padding": "4px",
                            "fontWeight": "600",
                            "justifyContent": "center",
                            "alignItems": "center",
                            "display": "flex",
                            "marginTop": "8px",
                            "height": "30px",
                        },
                    },
                    {
                        "condition": "Number(params.value) >= 0.30",
                        "style": {
                            "backgroundColor": "#2251ff",
                            "color": "white",
                            "borderRadius": "18px",
                            "padding": "4px",
                            "fontWeight": "600",
                            "justifyContent": "center",
                            "alignItems": "center",
                            "display": "flex",
                            "marginTop": "8px",
                            "height": "30px",
                        },
                    },
                ]
            },
        },
    ]

    aggrid = AgGrid(
        columnDefs=column_defs_orders,
        defaultColDef={"resizable": True, "sortable": True, "filter": True, "minWidth": 30, "flex": 1},
        style={"height": "800px", "width": "100%"},
        rowData=data_frame.to_dict("records"),
        dashGridOptions={
            "rowHeight": 55,
            "animateRows": True,
            "suppressMovableColumns": True,
            "pagination": True,
            "paginationPageSize": 20,
        },
        dangerously_allow_code=True,
    )
    return aggrid


@capture("ag_grid")
def customers_ag_grid(data_frame):
    data_frame = make_customer_sales_pareto_df(data_frame)
    # Convert back to fraction since percentage is handled by cellDataType=percent.
    data_frame["Cumulative %"] = data_frame["Cumulative % of Sales"] / 100

    return dash_ag_grid(
        data_frame,
        columnDefs=[
            {"field": "Rank", "width": 50},
            {"field": "Customer Name"},
            {"field": "Sales", "cellDataType": "dollar"},
            {"field": "Cumulative %", "cellDataType": "percent", "width": 130},
        ],
    )()