Python dash gauge - 我如何使用字符串作为值而不是数字?

Posted

技术标签:

【中文标题】Python dash gauge - 我如何使用字符串作为值而不是数字?【英文标题】:Python dash gauge - how can i use strings as values instead of numbers? 【发布时间】:2021-11-15 09:46:36 【问题描述】:

这是我的代码(基本上你可以在 html dash 文档中找到相同的内容) 我想做类似的东西: 如您所见,有不同的段被命名,并且在仪表下方还有一个段名称。

import dash_daq as daq

daq.Gauge(
    color="gradient":True,"ranges":"green":[0,6],"yellow":[6,8],"red":[8,10],
    value=2,
    label='Default',
    max=10,
    min=0,
)

目前我有这个

【问题讨论】:

我不确定你能做到这一点。假设您有小型、中型和大型。如果你给 1 到小,2 到中,3 到大,这是有道理的。如果你有苹果、梨和南瓜怎么办?如果你给苹果 1,梨 2,香蕉 3,这是否意味着香蕉比苹果大 3 倍,或者比苹果好 3 倍,对吗?不。我很想听听其他人对此的看法。 您是否尝试过使用自定义 dict 定义比例,其中键是 hauge 值,vales 是自定义字符串,即强卖、卖等。 【参考方案1】:

bpgergo's suggestion of using the scale property的基本实现。

from dash import Dash, html, dcc
import dash_bootstrap_components as dbc
from dash.dependencies import Output, Input
import dash_daq as daq

app = Dash(__name__)
app.layout = daq.Gauge(
    color=
        "ranges": 
            "red": [0, 2],
            "pink": [2, 4],
            "#ADD8E6": [4, 6],
            "#4169E1": [6, 8],
            "blue": [8, 10],
        ,
    ,
    scale=
        "custom": 
            1: "label": "Strong Sell",
            3: "label": "Sell",
            5: "label": "Neutral",
            7: "label": "Buy",
            9: "label": "Strong Buy",
        
    ,
    value=2,
    max=10,
    min=0,
)

if __name__ == "__main__":
    app.run_server()

结果

你也可以用 css 隐藏刻度

.tick 
  display: none;

【讨论】:

以上是关于Python dash gauge - 我如何使用字符串作为值而不是数字?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Dash Web 应用程序中使用“dash.properties.Synced”? | Python

如何使用 Python3 opencv 捕获 mpeg-dash 流?

与dash应用程序集成后如何刷新python folium(map.html文件)?

如何通过 Python 中的 Plotly 从 Dash 的下拉列表中选择数据集列?

如何将布局更新到 Python Dash 应用程序中

如何在 python(dash)仪表板中显示 png 文件和 csv 表