Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 843b5a5

Browse files
dcc.Graph: Improve responsiveness, expose resize status, allow wrapper extension (#706)
1 parent 2a1f01e commit 843b5a5

File tree

11 files changed

+441
-54
lines changed

11 files changed

+441
-54
lines changed

CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## [Unreleased]
6+
### Added
7+
- [#706](https://github.com/plotly/dash-core-components/pull/706)
8+
- Added new `responsive` property that overrides the underlying Plotly.js graph responsiveness from Dash-land
9+
- Added responsiveness on graph parent element resize (previously only worked on window.resize)
10+
- Added new `dash-graph--pending` class to dcc.Graph, present while resizing, (re-)rendering, loading
11+
12+
### Updated
13+
- [#706](https://github.com/plotly/dash-core-components/pull/706)
14+
- Upgraded plotly.js to [1.51.3](https://github.com/plotly/plotly.js/releases/tag/v1.51.3)
15+
516
## [1.6.0] - 2019-11-27
617
### Updated
718
- Upgraded plotly.js to 1.51.2 [#708](https://github.com/plotly/dash-core-components/pull/708)

CONTRIBUTING.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ Locally](README.md#testing-locally) section of README.md.
3434
## Updating Plotly.js
3535

3636
1. Update the version of `plotly.js` in package.json. Always use an exact version without "^" or "~"
37-
2. Run `npm install` followed by `npm run build`, this will ensure the latest version of Plotly.js is in `node_modules` and copy
38-
that version over with the other build artifacts
39-
3. Update `dash_core_components_base/__init__.py` plotly.js `relative_package_path` and `external_url`
37+
2. Run `npm install` followed by `npm run build`, the Plotly.js artifact will be copied and bundled over as required
4038
4. Update `CHANGELOG.md` with links to the releases and a description of the changes. The message should state (see the existing `CHANGELOG.md` for examples):
4139
* If you're only bumping the patch level, the heading is "Fixed" and the text starts "Patched plotly.js". Otherwise the heading is "Updated" and the text starts "Upgraded plotly.js"
4240
* The new plotly.js version number, and the PR in which this was done

MANIFEST.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ include dash_core_components/async~*.js.map
55
include dash_core_components/highlight.pack.js
66
include dash_core_components/metadata.json
77
include dash_core_components/package-info.json
8-
include dash_core_components/plotly-*.min.js
8+
include dash_core_components/plotly.min.js
99
include README.md
1010
include LICENSE.txt
1111
include package.json

dash_core_components_base/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@
9292
'dynamic': True
9393
},
9494
{
95-
'relative_package_path': 'plotly-1.51.2.min.js',
95+
'relative_package_path': 'plotly.min.js',
9696
'external_url': (
9797
'https://unpkg.com/dash-core-components@{}'
98-
'/dash_core_components/plotly-1.51.2.min.js'
98+
'/dash_core_components/plotly.min.js'
9999
).format(__version__),
100100
'namespace': 'dash_core_components',
101101
'async': 'eager'

dash_core_components_base/plotly-1.51.2.min.js

-7
This file was deleted.

package-lock.json

+45-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"format": "prettier --config .prettierrc --write src/**/*.js tests/unit/*.js",
2525
"format:test": "prettier --config .prettierrc src/**/*.js tests/unit/*.js --list-different",
2626
"uninstall-local": "pip uninstall dash-core-components -y",
27+
"prebuild:js": "cp node_modules/plotly.js/dist/plotly.min.js dash_core_components_base/plotly.min.js",
2728
"build:js": "webpack --mode production",
2829
"build:py_and_r": "dash-generate-components ./src/components dash_core_components -p package-info.json && cp dash_core_components_base/** dash_core_components/ && dash-generate-components ./src/components dash_core_components -p package-info.json --r-prefix 'dcc'",
2930
"build": "run-s build:js generator build:py_and_r",
@@ -36,7 +37,7 @@
3637
"color": "^3.1.0",
3738
"fast-isnumeric": "^1.1.3",
3839
"moment": "^2.20.1",
39-
"plotly.js": "1.51.2",
40+
"plotly.js": "1.51.3",
4041
"prop-types": "^15.6.0",
4142
"ramda": "^0.26.1",
4243
"rc-slider": "^8.6.11",
@@ -78,6 +79,7 @@
7879
"prettier": "^1.14.2",
7980
"react": "^16.8.6",
8081
"react-dom": "^16.8.6",
82+
"react-resize-detector": "^4.2.1",
8183
"style-loader": "^0.23.1",
8284
"styled-jsx": "^3.1.1",
8385
"terser-webpack-plugin": "^2.3.0",

src/components/Graph.react.js

+54-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import React, {Component, PureComponent, Suspense} from 'react';
1+
import React, {Component, memo, Suspense} from 'react';
22
import PropTypes from 'prop-types';
33

44
import {asyncDecorator} from '@plotly/dash-component-plugins';
55
import LazyLoader from '../utils/LazyLoader';
6+
import {
7+
privatePropTypes,
8+
privateDefaultProps,
9+
} from '../fragments/Graph.privateprops';
610

711
const EMPTY_EXTEND_DATA = [];
812

@@ -76,11 +80,9 @@ class PlotlyGraph extends Component {
7680
render() {
7781
return (
7882
<ControlledPlotlyGraph
79-
{...{
80-
...this.props,
81-
extendData: this.state.extendData,
82-
clearExtendData: this.clearExtendData,
83-
}}
83+
{...this.props}
84+
extendData={this.state.extendData}
85+
clearExtendData={this.clearExtendData}
8486
/>
8587
);
8688
}
@@ -90,24 +92,58 @@ const RealPlotlyGraph = asyncDecorator(PlotlyGraph, () =>
9092
LazyLoader.plotly().then(LazyLoader.graph)
9193
);
9294

93-
class ControlledPlotlyGraph extends PureComponent {
94-
render() {
95-
return (
96-
<Suspense fallback={null}>
97-
<RealPlotlyGraph {...this.props} />
98-
</Suspense>
99-
);
100-
}
101-
}
95+
const ControlledPlotlyGraph = memo(props => {
96+
const {className, id} = props;
97+
98+
const extendedClassName = className
99+
? 'dash-graph ' + className
100+
: 'dash-graph';
101+
102+
return (
103+
<Suspense
104+
fallback={
105+
<div
106+
id={id}
107+
key={id}
108+
className={`${extendedClassName} dash-graph--pending`}
109+
/>
110+
}
111+
>
112+
<RealPlotlyGraph {...props} className={extendedClassName} />
113+
</Suspense>
114+
);
115+
});
116+
117+
ControlledPlotlyGraph.propTypes = PropTypes.any;
102118

103119
PlotlyGraph.propTypes = {
120+
...privatePropTypes,
121+
104122
/**
105123
* The ID of this component, used to identify dash components
106124
* in callbacks. The ID needs to be unique across all of the
107125
* components in an app.
108126
*/
109127
id: PropTypes.string,
110128

129+
/**
130+
* If True, the Plotly.js plot will be fully responsive to window resize
131+
* and parent element resize event. This is achieved by overriding
132+
* `config.responsive` to True, `figure.layout.autosize` to True and unsetting
133+
* `figure.layout.height` and `figure.layout.width`.
134+
* If False, the Plotly.js plot not be responsive to window resize and
135+
* parent element resize event. This is achieved by overriding `config.responsive`
136+
* to False and `figure.layout.autosize` to False.
137+
* If 'auto' (default), the Graph will determine if the Plotly.js plot can be made fully
138+
* responsive (True) or not (False) based on the values in `config.responsive`,
139+
* `figure.layout.autosize`, `figure.layout.height`, `figure.layout.width`.
140+
* This is the legacy behavior of the Graph component.
141+
*
142+
* Needs to be combined with appropriate dimension / styling through the `style` prop
143+
* to fully take effect.
144+
*/
145+
responsive: PropTypes.oneOf([true, false, 'auto']),
146+
111147
/**
112148
* Data from latest click event. Read-only.
113149
*/
@@ -479,6 +515,8 @@ PlotlyGraph.propTypes = {
479515
};
480516

481517
PlotlyGraph.defaultProps = {
518+
...privateDefaultProps,
519+
482520
clickData: null,
483521
clickAnnotationData: null,
484522
hoverData: null,
@@ -491,6 +529,7 @@ PlotlyGraph.defaultProps = {
491529
layout: {},
492530
frames: [],
493531
},
532+
responsive: 'auto',
494533
animate: false,
495534
animation_options: {
496535
frame: {

src/fragments/Graph.privateprops.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import PropTypes from 'prop-types';
2+
3+
export const privatePropTypes = {
4+
_dashprivate_transformConfig: PropTypes.func,
5+
_dashprivate_transformFigure: PropTypes.func,
6+
};
7+
8+
export const privateDefaultProps = {
9+
_dashprivate_transformConfig: c => c,
10+
_dashprivate_transformFigure: f => f,
11+
};

0 commit comments

Comments
 (0)