Skip to content

Commit

Permalink
allows plotly plots to be placed in another div by specifying the div…
Browse files Browse the repository at this point in the history
… id as "div_id" in the MIME json package

implements jupyterlab#173
  • Loading branch information
hottwaj committed Feb 12, 2019
1 parent 6990c5f commit ff9bd14
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions packages/plotly-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,44 @@ interface IPlotlySpec {
}

export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
render_target: HTMLElement;
/**
* Create a new widget for rendering Plotly.
*/
constructor(options: IRenderMime.IRendererOptions) {
super();
this.addClass(CSS_CLASS);
this._mimeType = options.mimeType;
}

/**
* Render Plotly into this widget's node.
*/
renderModel(model: IRenderMime.IMimeModel): Promise<void> {
const { data, layout, frames, config } = model.data[this._mimeType] as
| any
| IPlotlySpec;
const { data, layout, frames, config, div_id } = model.data[
this._mimeType
] as any | IPlotlySpec;
// const metadata = model.metadata[this._mimeType] as any || {};
return Plotly.react(this.node, data, layout, config).then(plot => {
if (div_id == null) {
this.render_target = this.node;
this.addClass(CSS_CLASS);
} else {
this.render_target = document.getElementById(div_id);
}
return Plotly.react(this.render_target, data, layout, config).then(plot => {
this.update();
if (frames) {
Plotly.addFrames(this.node, frames).then(() => {
Plotly.animate(this.node);
Plotly.addFrames(this.render_target, frames).then(() => {
Plotly.animate(this.render_target);
});
}
if (this.node.offsetWidth > 0 && this.node.offsetHeight > 0) {
if (
this.render_target.offsetWidth > 0 &&
this.render_target.offsetHeight > 0
) {
Plotly.toImage(plot, {
format: 'png',
width: this.node.offsetWidth,
height: this.node.offsetHeight
width: this.render_target.offsetWidth,
height: this.render_target.offsetHeight
}).then((url: string) => {
const imageData = url.split(',')[1];
if (model.data['image/png'] !== imageData) {
Expand Down Expand Up @@ -97,8 +106,8 @@ export class RenderedPlotly extends Widget implements IRenderMime.IRenderer {
*/
protected onUpdateRequest(msg: Message): void {
if (this.isVisible) {
Plotly.redraw(this.node).then(() => {
Plotly.Plots.resize(this.node);
Plotly.redraw(this.render_target).then(() => {
Plotly.Plots.resize(this.render_target);
});
}
}
Expand Down

0 comments on commit ff9bd14

Please sign in to comment.