javascript - 无法让 Plotly.js 图形在 react-grid-layout 中调整大小

标签 javascript reactjs react-grid-layout

我一直在研究一个 react-grid-layout 来在屏幕上显示和移动图形。目前我可以将 plotly.js 图形添加到容器中。它是可移动的,但不随容器调整大小。我想知道是否需要异步函数来允许在调整容器框大小时重新渲染绘图。下面是网格布局和直方图的代码。

const ReactGridLayout = WidthProvider(Responsive);

const Dash = (props) => {
  const { value, addItem } = props
  const ref = useRef()

  return (
    <div>
      <button onClick={addItem}>Add Item</button>
        <ReactGridLayout
          className="layout"
          onLayoutChange={(e) => console.log(props.layout)}
          breakpoints={{ lg: 1200, md: 996, sm: 768, xs: 480, xxs: 0 }}
          cols={{ lg: 12, md: 12, sm: 6, xs: 4, xxs: 2 }}
          resizeHandles={['s', 'w', 'e', 'n', 'sw', 'nw','se', 'ne']}
          verticalCompact={false}
          // onDragStart={'.dragbackground'}
          // isDraggable={false}
          draggableHandle=".dragHandle"
               
      >        
        {_.map(value, (item, i) => (
          <div id = 'gridID' ref={ref} key={i} data-grid={props.layout[i]}  onClick={() => props.updateLayout(props.layout[i])}>
            <span className='dragHandle'>Drag From Here</span>
            <br/>
            <DashItem  key={i} >
              {item}
            </DashItem>
            <span className='dragHandle'>Drag From Here</span>
          </div>
        ))}
        </ReactGridLayout>
    </div>
  );
}


Dash.propTypes = {
  value: PropTypes.array,
  onIncreaseClick: PropTypes.func.isRequired
}


const mapStateToProps = (state) => {
  return {
    value: state.count,
    layout: state.layout,
    onLayoutChange: state.onLayoutChange,
  };
};

const mapDispatchToProps = dispatch => { 
  return {
    addItem: () => dispatch({type: actionTypes.ADD_GRID_ITEM}),
    updateLayout: (i) => dispatch({type: actionTypes.UPDATE_LAYOUT, layoutId: i}),
    removeItem: (i) => dispatch({type: actionTypes.REMOVE_ITEM, layoutElId: i})
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(Dash);
这是使用 plotly.js 的直方图代码:
export default function Histogram(props) {

  const { width, height, ref } = useResizeDetector({
    //
  })

  const layout = props.layout

  return(   
    <div style={{height: '100%', width: '100%'}}> 
      <Plot
      useResizeHandler = {true}
      ChartComponent = {Histogram}
      callback={(chart) => this.setChart(chart)}
      style={{width: "100%", height: "100%"}}
      config={{responsive: true}}
      data={[
        {
          x: d1,
          type: 'histogram',
          marker: {
            colour: 'red',
            opacity: 0.5
          },
        },
        {
          x: d2,
          type: 'histogram',
          marker: {
            colour: 'blue',
            opacity: 0.5
          }
        }
      ]}
      layout={{   
        ...layout,
        height: height,
        width: width,
        autosize:true,
        margin: {
          l: 50,
          r: 50,
          b: 100,
          t: 100,
          pad: 4
        },
        title: 'Histogram Title',
          barmode: 'stack',
          bargap: 0.05,
          bargroup: 2,
          xaxis: {
            title: 'X Axis Title'
          },
          yaxis: {
            title: 'Frequency',
            automargin:true
          }}}
      style= {{
        display: 'flex',
        alignItems: 'center',
      }}
      config= {{
      responsive: true 
      }}  
      />
    </div>   
  )
};
我用来将图形元素输入到我的网格中的 Redux reducer :
const reducer = (state=initialState, action) => {
    switch (action.type) {
        case actionTypes.ADD_GRID_ITEM:
            const id = uuid()
            console.log("adding: " + id)
            return{
                ...state,
                count: state.count.concat(<Histogram/>),
                layout: state.layout.concat({
                    i: `${id}`,
                    x: 2,
                    y: 0,
                    w: 4,
                    h: 5
                }),
            }

最佳答案

我刚刚构建了类似的东西,对我有用的是使用 react-resize-detector 将绘图包裹在带有 ref 的 div 中。以便它始终以响应窗口大小调整和拖动调整大小的方式填充其父项,但仍与 react 布局网格分离。这是我使用的绘图组件:

import { useResizeDetector } from 'react-resize-detector'
import Plot from 'react-plotly.js'
export default function ResponsivePlot(props) {
  const { width, height, ref } = useResizeDetector({ 
    // refreshMode: 'debounce', 
    // refreshRate: 1000
  })
  const { layout, layers } = props
  return (
    <div ref={ref} style={{ display: 'flex', height: '100%' }}>
      <Plot
        data={layers}
        layout={{
          ...layout, 
          ...{
            width: width, 
            height: height
          }
        }}
      />
    </div>
  )
};
用法:
import ResponsivePlot from './ResponsivePlot'

//...other stuff 

<ResponsivePlot layers={layers} layout={layout} />

关于javascript - 无法让 Plotly.js 图形在 react-grid-layout 中调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65565167/

相关文章:

javascript - 在将 Angular 5.2 升级到 Angular 6、Bootstrap 4、jquery 3.3.1 后,Bootstrap 4 下拉菜单和 Bootstrap-select 下拉菜单不再下拉

javascript - JS getters : does defineProperty replace or complement the older, inline 'get' 'set' 语法?

node.js - 尝试在服务器端捕获影响React中的错误处理

javascript - ReactJS react-grid-layout 切换静态属性

javascript - 如何使用 react-grid-layout 创建动态拖放布局

javascript将值分配给函数中的全局变量不起作用

javascript - 如何转置 HTML 表格?

reactjs - react-admin:如何在 Datagrid 的每一行上显示删除按钮

javascript - 函数在 React/Redux 中返回 [object Object] 而不是 html

javascript - React-Grid-Layout:如何在调整大小时重新渲染项目