Skip to content

Commit 98d30e1

Browse files
authored
Merge pull request #1906 from nickmelnikov82/make-graph-height-more-responsive
Make graph height more responsive
2 parents bd06cc8 + 05f39bd commit 98d30e1

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

components/dash-core-components/src/fragments/Graph.react.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,27 @@ class PlotlyGraph extends Component {
458458
});
459459
}
460460

461+
getStyle() {
462+
const {responsive} = this.props;
463+
let {style} = this.props;
464+
465+
// When there is no forced responsive style, return the original style property
466+
if (!responsive) {
467+
return style;
468+
}
469+
470+
// Otherwise, if the height is not set, we make the graph size equal to the parent one
471+
if (!style) {
472+
style = {};
473+
}
474+
475+
if (!style.height) {
476+
return Object.assign({height: '100%'}, style);
477+
}
478+
479+
return style;
480+
}
481+
461482
componentDidMount() {
462483
const p = this.plot(this.props);
463484
this._queue = this.amendTraces(p, {}, this.props);
@@ -516,7 +537,8 @@ class PlotlyGraph extends Component {
516537
}
517538

518539
render() {
519-
const {className, id, style} = this.props;
540+
const {className, id} = this.props;
541+
const style = this.getStyle();
520542

521543
return (
522544
<LoadingElement

components/dash-core-components/tests/integration/graph/test_graph_responsive.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import numpy as np
2+
import plotly.graph_objects as go
13
import pytest
24
import flaky
35

@@ -138,8 +140,38 @@ def resize(n_clicks, style):
138140
assert dash_dcc.get_logs() == []
139141

140142

143+
def test_grrs002_responsive_parent_height(dash_dcc):
144+
app = Dash(__name__, eager_loading=True)
145+
146+
x, y = np.random.uniform(size=50), np.random.uniform(size=50)
147+
148+
fig = go.Figure(
149+
data=[go.Scattergl(x=x, y=y, mode="markers")],
150+
layout=dict(margin=dict(l=0, r=0, t=0, b=0), height=600, width=600),
151+
)
152+
153+
app.layout = html.Div(
154+
dcc.Graph(
155+
id="graph",
156+
figure=fig,
157+
responsive=True,
158+
),
159+
style={"borderStyle": "solid", "height": 300, "width": 100},
160+
)
161+
162+
dash_dcc.start_server(app)
163+
164+
wait.until(
165+
lambda: dash_dcc.wait_for_element("#graph svg.main-svg").size.get("height", -1)
166+
== 300,
167+
3,
168+
)
169+
170+
assert dash_dcc.get_logs() == []
171+
172+
141173
@flaky.flaky(max_runs=3)
142-
def test_grrs002_graph(dash_dcc):
174+
def test_grrs003_graph(dash_dcc):
143175
app = Dash(__name__)
144176

145177
app.layout = html.Div(

0 commit comments

Comments
 (0)