div 中的 Plotly js 响应图

Plotly js responsive graph in div

我正在制作一个动态创建的 div 页面,使用简单的 css class 在鼠标悬停时调整大小。我使用它是为了在加载页面时这些 div 很小,然后如果用户想要仔细查看,他们只需将鼠标悬停在上面。

CSS 基本上是这样的

.resize {
    width: 400px;
    height: 300px;
    transition: all 1s;
}

.resize:hover {
    width: 800px;
    height: 600px;
}

我正在尝试让我在 div 中使用 Plotly.js 创建的绘图在用户鼠标悬停时自动调整大小。

JS代码

function doGraph() {
    for(index = 0; index < divArr.length; ++index) {
        (function() {
            var d3 = Plotly.d3;

            var gd3 = d3.select("div[id='"+divArr[index]+"']");
                //.append('div')
                //.style({
                //    width: "95%", "margin-left": "2.5%",

                //    height: "95%", "margin-top": "2.5%"
                //});

            var gd = gd3.node();

            Plotly.newPlot(gd, [{
                mode:'lines',
                x: xArr[index],
                y: yArr[index], }], 
                        layout , {scrollZoom:true,modeBarButtonsToRemove:['sendDataToCloud'],showLink:false,displaylogo:false});

            //gd.onresize = function() {
            //    Plotly.Plots.resize(gd);
            //};
            window.addEventListener('resize', function() { Plotly.Plots.relayout(gd); });

        })();
    }
}

注释掉的代码表明我不确定我需要做什么才能完成这项工作。到目前为止,我所做的所有修补工作都没有结果。

页面上的所有内容都是根据我的用户生成的 txt 文件在 c# 代码隐藏中动态创建的。

我找到了一个似乎相关的 q/a Here 但老实说,我不确定它如何适用于我的代码。

看看 plotly 的 resize 函数。

下面代码段中的图表会在鼠标悬停时随机调整大小。希望大家可以轻松修改使用。

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

  var WIDTH_IN_PERCENT_OF_PARENT = 60,
    HEIGHT_IN_PERCENT_OF_PARENT = 80;

  var gd3 = d3.select('#myDiv')
    .style({
      width: (Math.random() + Math.random()) * WIDTH_IN_PERCENT_OF_PARENT + '%',
      'margin-left': (100 - WIDTH_IN_PERCENT_OF_PARENT) / 2 + '%',

      height: (Math.random() + Math.random()) * HEIGHT_IN_PERCENT_OF_PARENT + 'vh',
      'margin-top': (100 - HEIGHT_IN_PERCENT_OF_PARENT) / 2 + 'vh'
    });

  var gd = gd3.node();

  a = Plotly.plot(gd, [{
    type: 'bar',
    x: [1, 2, 3, 4],
    y: [5, 10, 2, 8],

  }], {
    title: 'Random resize on hover',
    font: {
      size: 16
    }
  });

  document.getElementById("myDiv").on('plotly_hover', function(data) {
    window.alert("I'm resizing now");
    gd3 = d3.select('#myDiv')
      .style({
        width: (0.5 + Math.random() + Math.random()) * WIDTH_IN_PERCENT_OF_PARENT + '%',
        height: (0.5 + Math.random() + Math.random()) * HEIGHT_IN_PERCENT_OF_PARENT + 'vh',
      });
    gd = gd3.node()
    Plotly.Plots.resize(gd);;
  });


})();
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<div id='myDiv'></div>


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

  var WIDTH_IN_PERCENT_OF_PARENT = 60,
    HEIGHT_IN_PERCENT_OF_PARENT = 80;

  var gd3 = d3.select('#myDiv')
    .style({
      width: WIDTH_IN_PERCENT_OF_PARENT + '%',
      'margin-left': (100 - WIDTH_IN_PERCENT_OF_PARENT) / 2 + '%',

      height: HEIGHT_IN_PERCENT_OF_PARENT + 'vh',
      'margin-top': (100 - HEIGHT_IN_PERCENT_OF_PARENT) / 2 + 'vh'
    });

  var gd = gd3.node();

  a = Plotly.plot(gd, [{
    type: 'bar',
    x: [1, 2, 3, 4],
    y: [5, 10, 2, 8],

  }], {
    title: 'Double on hover, normal on unhover',
    font: {
      size: 16
    }
  });

  document.getElementById("myDiv").on('plotly_hover', function(data) {
    gd3.style({
        width: 2 * WIDTH_IN_PERCENT_OF_PARENT + '%',
        height: 2 * HEIGHT_IN_PERCENT_OF_PARENT + 'vh',
      });
    gd = gd3.node()
    Plotly.Plots.resize(gd);;
  });

  document.getElementById("myDiv").on('plotly_unhover', function(data) {
    gd3.style({
        width: WIDTH_IN_PERCENT_OF_PARENT + '%',
        height: HEIGHT_IN_PERCENT_OF_PARENT + 'vh',
      });
    gd = gd3.node()
    Plotly.Plots.resize(gd);;
  });


})();
<script src="https://cdn.plot.ly/plotly-1.2.0.min.js"></script>
<div id='myDiv'></div>

根据resizeresponsive 作为配置传递:

var config = {responsive: true}

Plotly.newPlot('myDiv', data, layout, config );

添加样式属性 resize:both; overflow: auto 时,可以调整 div 的大小。 由于 plotly 似乎只对 window 调整大小做出反应,因此当配置 responsive: true 被传递时,可以在 div 调整大小时触发这样的 window 调整大小。 div 调整大小的检测在 . Putting all together (https://jsfiddle.net/3cvnua0q/)

中进行了解释

<html>
<head>
  <script type="text/javascript" src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>

<div id='myDiv' style="resize:both; overflow: auto; border: 1px solid; height:250px">
</div>

<script>

let observer = new MutationObserver(function(mutations) {
  window.dispatchEvent(new Event('resize'));
});

let child = document.getElementById('myDiv');
observer.observe(child, {attributes: true})

var trace1 = {
  type: 'bar',
  x: [1, 2, 3, 4],
  y: [5, 10, 2, 8],
}

var data = [trace1]
var layout = {
  margin: {l: 20, r: 10, b: 20, t: 10}
}
var config = {responsive: true}

Plotly.newPlot('myDiv', data, layout, config);
</script>

</body>
</html>