mirror of
https://github.com/lana-k/sqliteviz.git
synced 2025-12-06 10:08:52 +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>
|
<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">
|
<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
|
There is no data to build a chart. Run your SQL query and make sure the
|
||||||
result is not empty.
|
result is not empty.
|
||||||
@@ -13,11 +13,7 @@
|
|||||||
:data="state.data"
|
:data="state.data"
|
||||||
:layout="state.layout"
|
:layout="state.layout"
|
||||||
:frames="state.frames"
|
:frames="state.frames"
|
||||||
:config="{
|
:config="config"
|
||||||
editable: true,
|
|
||||||
displaylogo: false,
|
|
||||||
modeBarButtonsToRemove: ['toImage']
|
|
||||||
}"
|
|
||||||
:dataSources="dataSources"
|
:dataSources="dataSources"
|
||||||
:dataSourceOptions="dataSourceOptions"
|
:dataSourceOptions="dataSourceOptions"
|
||||||
:plotly="plotly"
|
:plotly="plotly"
|
||||||
@@ -36,8 +32,7 @@
|
|||||||
import { applyPureReactInVue } from 'veaury'
|
import { applyPureReactInVue } from 'veaury'
|
||||||
import plotly from 'plotly.js'
|
import plotly from 'plotly.js'
|
||||||
import 'react-chart-editor/lib/react-chart-editor.css'
|
import 'react-chart-editor/lib/react-chart-editor.css'
|
||||||
|
import ReactPlotlyEditorWithPlotRef from '@/lib/ReactPlotlyEditorWithPlotRef.jsx'
|
||||||
import ReactPlotlyEditor from 'react-chart-editor'
|
|
||||||
import chartHelper from '@/lib/chartHelper'
|
import chartHelper from '@/lib/chartHelper'
|
||||||
import * as dereference from 'react-chart-editor/lib/lib/dereference'
|
import * as dereference from 'react-chart-editor/lib/lib/dereference'
|
||||||
import fIo from '@/lib/utils/fileIo'
|
import fIo from '@/lib/utils/fileIo'
|
||||||
@@ -54,16 +49,21 @@ export default {
|
|||||||
],
|
],
|
||||||
emits: ['update:importToSvgEnabled', 'update', 'loadingImageCompleted'],
|
emits: ['update:importToSvgEnabled', 'update', 'loadingImageCompleted'],
|
||||||
components: {
|
components: {
|
||||||
PlotlyEditor: applyPureReactInVue(ReactPlotlyEditor)
|
PlotlyEditor: applyPureReactInVue(ReactPlotlyEditorWithPlotRef)
|
||||||
},
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
plotly,
|
plotly,
|
||||||
state: this.initOptions || {
|
state: this.initOptions || {
|
||||||
data: [],
|
data: [],
|
||||||
layout: {},
|
layout: { autosize: true },
|
||||||
frames: []
|
frames: []
|
||||||
},
|
},
|
||||||
|
config: {
|
||||||
|
editable: true,
|
||||||
|
displaylogo: false,
|
||||||
|
modeBarButtonsToRemove: ['toImage']
|
||||||
|
},
|
||||||
visible: true,
|
visible: true,
|
||||||
resizeObserver: null,
|
resizeObserver: null,
|
||||||
useResizeHandler: this.$store.state.isWorkspaceVisible
|
useResizeHandler: this.$store.state.isWorkspaceVisible
|
||||||
@@ -120,9 +120,12 @@ export default {
|
|||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
async handleResize() {
|
async handleResize() {
|
||||||
this.visible = false
|
const plotComponent = this.$refs.plotlyEditor.plotComponentRef.current
|
||||||
await this.$nextTick()
|
this.$refs.plotlyEditor.plotComponentRef.current.updatePlotly(
|
||||||
this.visible = true
|
false, // shouldInvokeResizeHandler
|
||||||
|
plotComponent.props.onUpdate, // figureCallbackFunction
|
||||||
|
false // shouldAttachUpdateEvents
|
||||||
|
)
|
||||||
},
|
},
|
||||||
onRender(data, layout, frames) {
|
onRender(data, layout, frames) {
|
||||||
// TODO: check changes and enable Save button if needed
|
// TODO: check changes and enable Save button if needed
|
||||||
|
|||||||
Reference in New Issue
Block a user