想要减少Plotly.JS热图和另一个div文本之间的填充/空间-需要CSS

问题描述

我有一个运行良好的Plotly.JS热图,我想在单独的div上放置一些描述性文本,但是差距看起来有些尴尬。我以某种方式努力减少填充量,以使我的文字(例如,我的描述)和热图更加接近。我尝试使用CSS,但是我显然不满意。我确信这是一个简单的答案,并且我忽略了一些简单的事情。任何帮助表示赞赏。

Codepen:https://codepen.io/tenebris_silentio/pen/eYzgZMw

<!DOCTYPE html>

  <head>
    <!-- Load plotly.js into the DOM -->
    <script src='https://cdn.plot.ly/plotly-latest.min.js'></script>
  </head>
<br>

My description.

    <div id='myDiv'><br> </div>
  </div>
</div><script>

var colorscaleValue = [
  [0,'#ADD8E6'],[1,'#000080']
];
  var data = [
    {

      z: [[41,60,25,24,28],[56,27,14,45,17],[47,17,12,47,17]],x: ['Geographical','Temporal','Cultural','Digital','Financial'],y: ['Category 1','Category 2','Category 3'],colorscale: colorscaleValue,type: 'heatmap',hovertemplate: '<b># of %{y} Projects with a %{x} classification</b>: %{z}' + '<extra></extra>',hoverongaps: true
    }
  ];

  var layout = {
    title: '',width: 900,height: 560,autosize: false,yaxis: {automargin: true}
  };

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

  </script><!-- Plotly chart will be drawn inside this DIV --></div></div></div>

  <style>

.myDiv{
    padding: -1px;
}

  </style>

解决方法

您可以在布局中使用margin configuration

var layout = {
  // ...
    margin: {
      t: 10
    },// ...

<!DOCTYPE html>

  <head>
    <!-- Load plotly.js into the DOM -->
    <script src='https://cdn.plot.ly/plotly-latest.min.js'></script>
  </head>
<br>

My description.

    <div id='myDiv'><br> </div>
  </div>
</div><script>

var colorscaleValue = [
  [0,'#ADD8E6'],[1,'#000080']
];
  var data = [
    {

      z: [[41,60,25,24,28],[56,27,14,45,17],[47,17,12,47,17]],x: ['Geographical','Temporal','Cultural','Digital','Financial'],y: ['Category 1','Category 2','Category 3'],colorscale: colorscaleValue,type: 'heatmap',hovertemplate: '<b># of %{y} Projects with a %{x} classification</b>: %{z}' + '<extra></extra>',hoverongaps: true
    }
  ];

  var layout = {
    title: '',width: 900,height: 560,autosize: false,margin: {
      t: 10
    },yaxis: {automargin: true}
  };

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

  </script><!-- Plotly chart will be drawn inside this DIV --></div></div></div>

  <style>

.myDiv{
    padding: -1px;
}

  </style>

,

因为Plotly.JS正在生成SVG,因此对其进行样式设置并不容易。一种解决方案是使用绝对位置来设置元素的样式,以便您可以将其与SVG“重叠”。

例如:

var colorscaleValue = [
  [0,layout);

  </script><!-- Plotly chart will be drawn inside this DIV --></div></div></div>
.myDiv{
    padding: -1px;
}

p {
  position: absolute;
  z-index: 10;
  margin-top: 45px;
  margin-left: 80px;
}
<p>My description.</p>
<div id='myDiv'></div>
<script src='https://cdn.plot.ly/plotly-latest.min.js'></script>

,

为什么不在布局本身中添加文本?​​

var layout = {
  "title": {"text": "My description."},...
};

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...