首页  >  问答  >  正文

实现动态使用div的全部高度/宽度的Plotly JS方法

<p>点击顶部按钮时,容器<code>#myDiv</code>的大小会发生变化。如何使得Plotly自动占用容器的所有可用空间,<strong>而不会有任何明显的延迟</strong>?</p> <p>这里存在一些较高的延迟:</p> <p> <pre class="brush:js;toolbar:false;">var z = [], steps = [], i; for (i = 0; i < 500; i++) z.push(Array.from({length: 600}, () => Math.floor(Math.random() * 100))); for (i = 0; i < 100; i++) steps.push({ label: i, method: 'restyle', args: ['line.color', 'red']}); var data = [{z: z, colorscale: 'YlGnBu', type: 'heatmap'}]; var layout = {title: '', sliders: [{ pad: {t: 5}, len: 1, x: 0, currentvalue: {xanchor: 'right', prefix: 'i: ', font: {color: '#888', size: 20}}, steps: steps }]}; Plotly.newPlot('myDiv', data, layout); document.getElementById('button').onclick = () => { document.getElementById('myDiv').classList.toggle('size2'); Plotly.Plots.resize('myDiv'); }</pre> <pre class="brush:css;toolbar:false;">#button { background-color: gray; } #myDiv { width: 500px; height: 300px; border: 2px solid black; } .size2 { width: 300px !important; height: 200px !important; }</pre> <pre class="brush:html;toolbar:false;"><script src="https://cdn.plot.ly/plotly-2.16.2.min.js"></script> <span id="button">点击这里改变大小</span> <div id="myDiv"></div> <div id="text">Hello world</div></pre> </p>
P粉012875927P粉012875927421 天前519

全部回复(1)我来回复

  • P粉046878197

    P粉0468781972023-08-26 11:23:06

    要使Plotly自动占用容器的所有可用空间,您可以在布局对象中使用 'autosize' 选项。

    var layout = {
        title: '',
        autosize: true,
        sliders: [{
            pad: {t: 5},
            len: 1,
            x: 0,
            currentvalue: {
                xanchor: 'right',
                prefix: 'i: ',
                font: {
                    color: '#888',
                    size: 20
                }
            },
            steps: steps
        }]
    };
    

    回复
    0
  • 取消回复