Hello,
I want to use the ’ Displaying data bars’ on this page https://dash.plotly.com/datatable/conditional-formatting
The example data bar uses the max/min value for a given column. Now I have a df, and I want to create bars for column c, which shows the b/a% for each row.
e.g. the data bar in column c will be 44%, 75% and 40%.
a b c
0 9 4 44%
1 4 3 75%
2 5 2 40%
Could anyone tell me how to do that plz?
This is the official code to create a data bar
def data_bars(df, column):
n_bins = 100
bounds = [i * (1.0 / n_bins) for i in range(n_bins + 1)]
ranges = [
((df[column].max() - df[column].min()) * i) + df[column].min()
for i in bounds
]
styles = []
for i in range(1, len(bounds)):
min_bound = ranges[i - 1]
max_bound = ranges[i]
max_bound_percentage = bounds[i] * 100
styles.append({
'if': {
'filter_query': (
'{{{column}}} >= {min_bound}' +
(' && {{{column}}} < {max_bound}' if (i < len(bounds) - 1) else '')
).format(column=column, min_bound=min_bound, max_bound=max_bound),
'column_id': column
},
'background': (
"""
linear-gradient(90deg,
#0074D9 0%,
#0074D9 {max_bound_percentage}%,
white {max_bound_percentage}%,
white 100%)
""".format(max_bound_percentage=max_bound_percentage)
),
'paddingBottom': 2,
'paddingTop': 2
})
return styles