【问题标题】:Python dash gauge - how can i use strings as values instead of numbers?Python dash gauge - 我如何使用字符串作为值而不是数字?
【发布时间】: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 是自定义字符串,即强卖、卖等。

标签: python-3.x plotly-dash dash-bootstrap-components


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    • 2018-12-07
    • 2015-04-05
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多