Skip to content

Commit

Permalink
#423: y-axis scale toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
aschonfeld committed Feb 8, 2021
1 parent ad5af5e commit ed43dc7
Showing 4 changed files with 90 additions and 19 deletions.
19 changes: 12 additions & 7 deletions dtale/dash_application/charts.py
Original file line number Diff line number Diff line change
@@ -186,6 +186,7 @@ def chart_url_querystring(params, data=None, group_filter=None):
"group_type",
"bins_val",
"bin_type",
"scale",
]
chart_type = params.get("chart_type")
if chart_type == "bar":
@@ -247,7 +248,9 @@ def build_colorscale(colorscale):
return [[i / (len(colorscale) - 1), rgb] for i, rgb in enumerate(colorscale)]


def build_axes(data_id, x, axis_inputs, mins, maxs, z=None, agg=None, data=None):
def build_axes(
data_id, x, axis_inputs, mins, maxs, z=None, agg=None, data=None, scale="linear"
):
"""
Returns helper function for building axis configurations against a specific y-axis.
@@ -286,7 +289,9 @@ def _build_axes(y):
for i, y2 in enumerate(y, 0):
right = i % 2 == 1
axis_ct = int(i / 2)
value = dict(title=_add_agg_label(update_label_for_freq(y2)))
value = dict(
title=_add_agg_label(update_label_for_freq(y2)), type=scale
)
if i == 0:
key = "yaxis"
else:
@@ -313,7 +318,7 @@ def _build_axes(y):
value["tickformat"] = ".0f"
axes[key] = value
elif axis_type == "single":
yaxis_cfg = dict(title=_add_agg_label(update_label_for_freq(y)))
yaxis_cfg = dict(title=_add_agg_label(update_label_for_freq(y)), type=scale)
all_range = axis_data.get("all") or {}
all_range = [
all_range.get(p) for p in ["min", "max"] if all_range.get(p) is not None
@@ -326,7 +331,7 @@ def _build_axes(y):
yaxis_cfg["tickformat"] = ".0f"
axes["yaxis"] = yaxis_cfg
else:
yaxis_cfg = dict(title=_add_agg_label(update_label_for_freq(y)))
yaxis_cfg = dict(title=_add_agg_label(update_label_for_freq(y)), type=scale)
if classify_type(dtypes.get(y[0])) == "I":
yaxis_cfg["tickformat"] = ".0f"
axes["yaxis"] = yaxis_cfg
@@ -2800,9 +2805,9 @@ def build_chart(data_id=None, data=None, **inputs):
range_data = dict(min=data["min"], max=data["max"])
axis_inputs = inputs.get("yaxis") or {}
chart_builder = chart_wrapper(data_id, data, inputs)
x, y, z, agg, group, animate_by, trendline = (
x, y, z, agg, group, animate_by, trendline, scale = (
inputs.get(p)
for p in ["x", "y", "z", "agg", "group", "animate_by", "trendline"]
for p in ["x", "y", "z", "agg", "group", "animate_by", "trendline", "scale"]
)
x = str("x") if x is None else x
z = z if chart_type in ZAXIS_CHARTS else None
@@ -2832,7 +2837,7 @@ def build_chart(data_id=None, data=None, **inputs):
)

axes_builder = build_axes(
data_id, x, axis_inputs, data["min"], data["max"], z=z, agg=agg
data_id, x, axis_inputs, data["min"], data["max"], z=z, agg=agg, scale=scale
)
if chart_type in ["scatter", "3d_scatter"]:
kwargs = dict(agg=agg)
25 changes: 20 additions & 5 deletions dtale/dash_application/layout/layout.py
Original file line number Diff line number Diff line change
@@ -781,6 +781,13 @@ def get_yaxis_type_tabs(y):
return tabs + [build_tab("Multi", "multi", {"padding": "2px", "minWidth": "4em"})]


def get_yaxis_scale_tabs():
return [
build_tab("Linear", "linear", {"padding": "2px", "minWidth": "4em"}),
build_tab("Log", "log", {"padding": "2px", "minWidth": "4em"}),
]


def build_group_val_options(df, group_cols):
group_vals = find_group_vals(df, group_cols)
return [
@@ -1763,11 +1770,19 @@ def show_map_style(show):
className="input-group-addon",
),
html.Div(
dcc.Tabs(
id="yaxis-type",
value=yaxis_type,
children=get_yaxis_type_tabs(y),
),
[
dcc.Tabs(
id="yaxis-scale",
value=inputs.get("scale") or "linear",
children=get_yaxis_scale_tabs(),
className="pr-5",
),
dcc.Tabs(
id="yaxis-type",
value=yaxis_type,
children=get_yaxis_type_tabs(y),
),
],
id="yaxis-type-div",
className="form-control col-auto pt-3",
style=yaxis_type_style,
12 changes: 11 additions & 1 deletion dtale/dash_application/views.py
Original file line number Diff line number Diff line change
@@ -594,10 +594,19 @@ def input_toggles(_ts, inputs, pathname):
Input("animate-toggle", "on"),
Input("animate-by-dropdown", "value"),
Input("trendline-dropdown", "value"),
Input("yaxis-scale", "value"),
],
)
def chart_input_data(
cpg, barmode, barsort, top_bars, colorscale, animate, animate_by, trendline
cpg,
barmode,
barsort,
top_bars,
colorscale,
animate,
animate_by,
trendline,
scale,
):
"""
dash callback for maintaining selections in chart-formatting inputs
@@ -614,6 +623,7 @@ def chart_input_data(
animate=animate,
animate_by=animate_by,
trendline=trendline,
scale=scale,
)

@dash_app.callback(
53 changes: 47 additions & 6 deletions tests/dtale/dash/test_dash.py
Original file line number Diff line number Diff line change
@@ -782,6 +782,7 @@ def test_chart_input_updates(unittest):
{"id": "animate-toggle", "property": "on"},
{"id": "animate-by-dropdown", "property": "value"},
{"id": "trendline-dropdown", "property": "value"},
{"id": "yaxis-scale", "property": "value"},
],
}

@@ -798,6 +799,7 @@ def test_chart_input_updates(unittest):
"animate": None,
"animate_by": None,
"trendline": None,
"scale": None,
},
)

@@ -2314,14 +2316,20 @@ def test_build_axes(unittest):
axes,
(
{
"yaxis": {"title": "b", "range": [1, 4], "tickformat": ".0f"},
"yaxis": {
"title": "b",
"range": [1, 4],
"tickformat": ".0f",
"type": "linear",
},
"yaxis2": {
"title": "c",
"overlaying": "y",
"side": "right",
"anchor": "x",
"range": [5, 7],
"tickformat": ".0f",
"type": "linear",
},
"yaxis3": {
"title": "d",
@@ -2330,6 +2338,7 @@ def test_build_axes(unittest):
"anchor": "free",
"position": 0.05,
"tickformat": ".0f",
"type": "linear",
},
"xaxis": {"domain": [0.1, 1], "tickformat": ".0f", "title": "a"},
},
@@ -2346,14 +2355,20 @@ def test_build_axes(unittest):
axes,
(
{
"yaxis": {"title": "b", "range": [1, 4], "tickformat": ".0f"},
"yaxis": {
"title": "b",
"range": [1, 4],
"tickformat": ".0f",
"type": "linear",
},
"yaxis2": {
"title": "c",
"overlaying": "y",
"side": "right",
"anchor": "x",
"range": [5, 7],
"tickformat": ".0f",
"type": "linear",
},
"yaxis3": {
"title": "d",
@@ -2362,6 +2377,7 @@ def test_build_axes(unittest):
"anchor": "free",
"position": 0.05,
"tickformat": ".0f",
"type": "linear",
},
"yaxis4": {
"title": "e",
@@ -2370,6 +2386,7 @@ def test_build_axes(unittest):
"anchor": "free",
"position": 0.95,
"tickformat": ".0f",
"type": "linear",
},
"xaxis": {
"domain": [0.1, 0.8999999999999999],
@@ -2390,14 +2407,20 @@ def test_build_axes(unittest):
axes,
(
{
"yaxis": {"title": "b", "range": [1, 4], "tickformat": ".0f"},
"yaxis": {
"title": "b",
"range": [1, 4],
"tickformat": ".0f",
"type": "linear",
},
"yaxis2": {
"title": "c",
"overlaying": "y",
"side": "right",
"anchor": "x",
"range": [5, 7],
"tickformat": ".0f",
"type": "linear",
},
"yaxis3": {
"title": "d",
@@ -2406,6 +2429,7 @@ def test_build_axes(unittest):
"anchor": "free",
"position": 0.05,
"tickformat": ".0f",
"type": "linear",
},
"yaxis4": {
"title": "e",
@@ -2414,6 +2438,7 @@ def test_build_axes(unittest):
"anchor": "free",
"position": 0.95,
"tickformat": ".0f",
"type": "linear",
},
"yaxis5": {
"title": "f",
@@ -2422,6 +2447,7 @@ def test_build_axes(unittest):
"anchor": "free",
"position": 0.1,
"tickformat": ".0f",
"type": "linear",
},
"xaxis": {
"domain": [0.15000000000000002, 0.8999999999999999],
@@ -2450,7 +2476,12 @@ def test_build_axes(unittest):
(
{
"xaxis": {"title": "a", "tickformat": ".0f"},
"yaxis": {"title": "b", "range": [1, 4], "tickformat": ".0f"},
"yaxis": {
"title": "b",
"range": [1, 4],
"tickformat": ".0f",
"type": "linear",
},
"zaxis": {"title": "c", "tickformat": ".0f"},
},
False,
@@ -2462,7 +2493,12 @@ def test_build_axes(unittest):
(
{
"xaxis": {"title": "a", "tickformat": ".0f"},
"yaxis": {"title": "b", "range": [1, 4], "tickformat": ".0f"},
"yaxis": {
"title": "b",
"range": [1, 4],
"tickformat": ".0f",
"type": "linear",
},
"zaxis": {"title": "c (Correlation)", "tickformat": ".0f"},
},
False,
@@ -2478,6 +2514,7 @@ def test_build_axes(unittest):
"title": "b (Correlation)",
"range": [1, 4],
"tickformat": ".0f",
"type": "linear",
},
},
False,
@@ -2490,7 +2527,11 @@ def test_build_axes(unittest):
(
{
"xaxis": {"title": "a", "tickformat": ".0f"},
"yaxis": {"tickformat": ".0f", "title": "b (Correlation)"},
"yaxis": {
"tickformat": ".0f",
"title": "b (Correlation)",
"type": "linear",
},
},
False,
),

0 comments on commit ed43dc7

Please sign in to comment.