mirror of
https://github.com/lana-k/sqliteviz.git
synced 2025-12-06 18:18:53 +08:00
fix plot resize
This commit is contained in:
65
src/lib/ReactPlotlyEditorWithPlotRef.jsx
Normal file
65
src/lib/ReactPlotlyEditorWithPlotRef.jsx
Normal file
@@ -0,0 +1,65 @@
|
||||
import ReactPlotlyEditor from 'react-chart-editor'
|
||||
import React, { createRef } from 'react'
|
||||
import EditorControls from 'react-chart-editor/lib/EditorControls'
|
||||
|
||||
/**
|
||||
* This extended ReactPlotlyEditor has a reference to PlotComponent.
|
||||
* The reference makes it possible to call updatePlotly method of PlotComponent.
|
||||
* updatePlotly method allows smoothly resize the plot
|
||||
* when resize chart editor container.
|
||||
*/
|
||||
export default class ReactPlotlyEditorWithPlotRef extends ReactPlotlyEditor {
|
||||
constructor(props) {
|
||||
super(props)
|
||||
this.plotComponentRef = createRef()
|
||||
}
|
||||
render() {
|
||||
return (
|
||||
<div className="plotly_editor">
|
||||
{!this.props.hideControls && (
|
||||
<EditorControls
|
||||
graphDiv={this.state.graphDiv}
|
||||
dataSources={this.props.dataSources}
|
||||
dataSourceOptions={this.props.dataSourceOptions}
|
||||
plotly={this.props.plotly}
|
||||
onUpdate={this.props.onUpdate}
|
||||
advancedTraceTypeSelector={this.props.advancedTraceTypeSelector}
|
||||
locale={this.props.locale}
|
||||
traceTypesConfig={this.props.traceTypesConfig}
|
||||
dictionaries={this.props.dictionaries}
|
||||
showFieldTooltips={this.props.showFieldTooltips}
|
||||
srcConverters={this.props.srcConverters}
|
||||
makeDefaultTrace={this.props.makeDefaultTrace}
|
||||
glByDefault={this.props.glByDefault}
|
||||
mapBoxAccess={Boolean(
|
||||
this.props.config && this.props.config.mapboxAccessToken
|
||||
)}
|
||||
fontOptions={this.props.fontOptions}
|
||||
chartHelp={this.props.chartHelp}
|
||||
customConfig={this.props.customConfig}
|
||||
>
|
||||
{this.props.children}
|
||||
</EditorControls>
|
||||
)}
|
||||
<div
|
||||
className="plotly_editor_plot"
|
||||
style={{ width: '100%', height: '100%' }}
|
||||
>
|
||||
<this.PlotComponent
|
||||
ref={this.plotComponentRef}
|
||||
data={this.props.data}
|
||||
layout={this.props.layout}
|
||||
frames={this.props.frames}
|
||||
config={this.props.config}
|
||||
useResizeHandler={this.props.useResizeHandler}
|
||||
debug={this.props.debug}
|
||||
onInitialized={this.handleRender}
|
||||
onUpdate={this.handleRender}
|
||||
style={{ width: '100%', height: '100%' }}
|
||||
divId={this.props.divId}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
<template>
|
||||
<div v-show="visible" class="chart-container" ref="chartContainer">
|
||||
<div class="chart-container" ref="chartContainer">
|
||||
<div class="warning chart-warning" v-show="!dataSources && visible">
|
||||
There is no data to build a chart. Run your SQL query and make sure the
|
||||
result is not empty.
|
||||
@@ -13,11 +13,7 @@
|
||||
:data="state.data"
|
||||
:layout="state.layout"
|
||||
:frames="state.frames"
|
||||
:config="{
|
||||
editable: true,
|
||||
displaylogo: false,
|
||||
modeBarButtonsToRemove: ['toImage']
|
||||
}"
|
||||
:config="config"
|
||||
:dataSources="dataSources"
|
||||
:dataSourceOptions="dataSourceOptions"
|
||||
:plotly="plotly"
|
||||
@@ -36,8 +32,7 @@
|
||||
import { applyPureReactInVue } from 'veaury'
|
||||
import plotly from 'plotly.js'
|
||||
import 'react-chart-editor/lib/react-chart-editor.css'
|
||||
|
||||
import ReactPlotlyEditor from 'react-chart-editor'
|
||||
import ReactPlotlyEditorWithPlotRef from '@/lib/ReactPlotlyEditorWithPlotRef.jsx'
|
||||
import chartHelper from '@/lib/chartHelper'
|
||||
import * as dereference from 'react-chart-editor/lib/lib/dereference'
|
||||
import fIo from '@/lib/utils/fileIo'
|
||||
@@ -54,16 +49,21 @@ export default {
|
||||
],
|
||||
emits: ['update:importToSvgEnabled', 'update', 'loadingImageCompleted'],
|
||||
components: {
|
||||
PlotlyEditor: applyPureReactInVue(ReactPlotlyEditor)
|
||||
PlotlyEditor: applyPureReactInVue(ReactPlotlyEditorWithPlotRef)
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
plotly,
|
||||
state: this.initOptions || {
|
||||
data: [],
|
||||
layout: {},
|
||||
layout: { autosize: true },
|
||||
frames: []
|
||||
},
|
||||
config: {
|
||||
editable: true,
|
||||
displaylogo: false,
|
||||
modeBarButtonsToRemove: ['toImage']
|
||||
},
|
||||
visible: true,
|
||||
resizeObserver: null,
|
||||
useResizeHandler: this.$store.state.isWorkspaceVisible
|
||||
@@ -120,9 +120,12 @@ export default {
|
||||
},
|
||||
methods: {
|
||||
async handleResize() {
|
||||
this.visible = false
|
||||
await this.$nextTick()
|
||||
this.visible = true
|
||||
const plotComponent = this.$refs.plotlyEditor.plotComponentRef.current
|
||||
this.$refs.plotlyEditor.plotComponentRef.current.updatePlotly(
|
||||
false, // shouldInvokeResizeHandler
|
||||
plotComponent.props.onUpdate, // figureCallbackFunction
|
||||
false // shouldAttachUpdateEvents
|
||||
)
|
||||
},
|
||||
onRender(data, layout, frames) {
|
||||
// TODO: check changes and enable Save button if needed
|
||||
|
||||
Reference in New Issue
Block a user