plotly - 响应宽度,固定高度

plotly - responsive width, fixed height

我试图制作一个适合可滚动父项的图表 div:

data = {
          Name: {0:'name1', 1:'name2', 2: 'name3', 
                 3:'n4',4:'ewf',5:'dgag', 6:'faf', 7:'dfss',
                 8:'345',9:'9',10:'23435', 11:'2e345',12:'3345', 13:'a345', 
                 14:'34g5', 15:'3f45'},
          Count: {0:1023, 1:2345, 3:3875,4:234,5:3456, 6:84, 7:7763,
                  8:345,9:2345,10:2345, 11:2345,12:345, 13:345, 14:345, 
                  15:345},
          Index: {0:35, 1:200, 2:160, 3:24,4:234,5:356, 6:84, 7:73,
                  8:345,9:2345,10:2345, 11:2345,12:345, 13:345, 14:345, 
                  15:345}
       }
$('#p1').html("");
console.log(data)
Plotly.newPlot('p1',
    [{
        type: 'bar',
        x: Object.values(data.Count),
        y: Object.values(data.Name),
        base: 0,
        orientation: 'h'
    },{
        type: 'bar',
        x: Object.values(data.Index).map(function (e) {
            e = e-100
            return e
        }),
        y: Object.values(data.Name),
        base: 100,
        orientation: 'h',
        xaxis: 'x2',
        yaxis: 'y'
        
    },],
    {
        height: 10*2.3*Object.keys(data.Index).length,
        yaxis: {
            automargin: true,
            tickangle: 35
        },
        grid: {
            rows: 1,
            columns: 2,
            subplots: [['xy', 'x2y']]
        }
    },
    {
        responsive: true
    })
.col.tpcs{
    overflow-y: scroll;
    overflow-x: hidden;
    display: inline-block;
    height: 400px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">

<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row loading pt-5 mt-md-3 mb-5">
   <div class="col tpcs" id='p1'></div>
</div>

根据此处的文档https://plot.ly/javascript/responsive-fluid-layout/,我创建了一个响应图:

我想让剧情向下溢出,这样我就可以向下滚动,但是通过设置responsive: true,高度默认为#p1的大小,即400px。

有没有简单的方法让宽度响应?我想将手动高度保持在 10*2.3*Object.keys(data.Index).length

plotly 似乎还没有这个功能。为了解决这个问题,我必须做的是制作一个完全响应的图并将其包装到一个额外的嵌套 div 中,如下所示:

    <div class="row loading pt-5 mt-md-3 mb-5">
        <div class="col tpcs" id='p1wrap'>
            <div class="ph" id=p1></div>
        </div>
    </div>

然后我需要使 div 宽度响应其父 div:

   function resP(id){
    var d3 = Plotly.d3;

    var parent_width = $("#"+id).parent().width()
    var gd3 = d3.select(`div[id=${id}]`)
        .style({
            width: parent_width - 10,
            //'margin-right': (100 - WIDTH_IN_PERCENT_OF_PARENT) / 2 + 'vh',
            //'margin-top': (100 - HEIGHT_IN_PERCENT_OF_PARENT) / 2 + 'vh'
        });
    return gd3.node();
   }

   window.addEventListener('resize', function(){
    Plotly.Plots.resize( resP('p1') );
   })

将您的绘图设置为响应式,然后在您调整父级大小时使用此函数(在浏览器中触发 window 调整大小事件):

window.dispatchEvent(new Event('resize'));