Scale max and min values of Metric chart #2819
-
|
Hello Taipy Community. I'm building a dashboard that has a scorecard of total sales. I wanted to use a Metric chart for the same total. When I select/de-select years in the dropdown, the total is updated correctly. In my function that I calculate the total, I also calc a new max and min for the Metric chart. The new values are not reflected with the updated total. Any ideas? Thanks in advance! Function code: Markdown Code: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
Hey! I think this is a state-binding issue. In Taipy, min/max/threshold need to be bound to state variables that actually change (same as value). A couple practical checks:\n\n- Make sure total_min, total_max, and total_threshold are declared in state and updated in your callback.\n- If you return multiple values, ensure your callback is wired to update all of them (e.g., on_change returns a tuple that maps to all three state vars).\n- Also, you’re using data when computing min_year/min_year_sales instead of filtered_data — if that’s intentional fine, but it might make the threshold look “stuck”.\n\nIf you can share the callback wiring (on_change / on_action), I can help spot it. |
Beta Was this translation helpful? Give feedback.
-
|
The The easiest solution might be to instead use a figure directly like so: import pandas as pd
import plotly.graph_objects as go
from taipy.gui import Gui, State
df = pd.DataFrame({"product": ["A", "B", "C"], "sales": [100, 200, 150]})
product_list = df["product"].tolist()
selected_product = None
sales_figure = None
def update_sales(state):
_total_sales = df[df["product"] == state.selected_product]["sales"].values[0]
_min_sales = _total_sales - 50
_max_sales = _total_sales + 50
_threshold_sales = _total_sales + 20
fig = go.Figure(
go.Indicator(
mode="gauge+number",
value=_total_sales,
gauge={
"axis": {"range": [_min_sales, _max_sales]},
"threshold": {"line": {"color": "red", "width": 4}, "thickness": 0.75, "value": _threshold_sales},
},
)
)
state.sales_figure = fig
def on_init(state: State):
state.selected_product = product_list[0]
update_sales(state)
page = """
<|{selected_product}|selector|lov={product_list}|mode=radio|on_change=update_sales|>
<|chart|figure={sales_figure}|>
"""
Gui(page).run()However, if you prefer to use the metric control, you can instead create the component in a partial. Let me know if you want an example of this |
Beta Was this translation helpful? Give feedback.
The
minandmaxproperties for the metric control are not dynamic, so they will retain the value at initialization.The easiest solution might be to instead use a figure directly like so: