PlotlyJS条形图X轴自动调整不能按预期工作?

问题描述

以下是我对PlotlyJS的一个问题的最小复制,我看不到所有栏。 我以为 X轴上的autorange可以解决问题。

那里发生了什么,我是否在配置图形?

var data = [ {
    "x" : [ "1.5","2.5","3.5","6.5","7.5","8.5","10.5","Not KNown" ],"y" : [ "1.0","1.0","2.0","1.0" ],"name" : "Double with string \"N/A\" Exposure","type" : "bar","showlegend" : false,"visible" : true
  } ];

var layout = {
    "xaxis" : {
      "type" : "category","showticklabels" : true,"zeroline" : false,"fixedrange" : false,"showgrid" : false,"autorange" : true,"autotick" : true,"showline" : false,"visible" : true,"automargin" : false
    },"toResize" : true,"showlegend" : true,};

Plotly.newPlot('myDiv',data,layout);
body {
  width: 100%;
  
  div {
    width: 100%;
  }
}
<head>
    <!-- Load plotly.js into the DOM -->
    <script src='https://cdn.plot.ly/plotly-latest.min.js'></script>
</head>

<body>
    <div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>
</body>

解决方法

这肯定很奇怪。 在我看来,尽管您为x轴设置了type: "category",但还是试图自行确定轴类型,或者以某种方式无法处理x值数组中同时使用的数字和字符串。特别是在这种情况下,7.5似乎冒犯了我们。

作为一种解决方法,您可以在数字类别中添加不间断空格以强制解释为字符串,例如7.5&nbsp;

您可能会为此问题向开发人员提交错误。

var data = [ {
    "x" : [ "1.5","2.5","3.5","6.5","7.5&nbsp;","8.5","10.5","Not Known" ],"y" : [ 1.0,1.0,2.0,1.0 ],"type" : "bar"
  } ];

var layout = {
    "xaxis" : {
      "type" : "category"
    }
};

Plotly.newPlot('myDiv',data,layout);
body {
  width: 100%;
  
  div {
    width: 100%;
  }
}
<head>
    <!-- Load plotly.js into the DOM -->
    <script src='https://cdn.plot.ly/plotly-latest.min.js'></script>
</head>

<body>
    <div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>
</body>