PlotlyJS bar chart X axis autorange not working as expected?

895 views Asked by At

Here a minimal reproduction of an issue I have with PlotlyJS, I can not see all bars. I thought autorange on X axis would do the trick.

What is happening there, am I misconfiguring the graph ?

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", "1.0", "2.0", "1.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>

1

There are 1 answers

1
mit On BEST ANSWER

This sure is weird. It seem to me that although you set type: "category" for the x axis, plotly attempts to determine the axis type by itself or somehow cannot cope with numbers and strings being used at the same time in the x value array. In particular, it seems to take offense with 7.5 in this case.

As a workaround, you could add non breaking spaces to your number categories to force interpretation as strings, as in 7.5&nbsp;.

You could probably file a bug with the plotly developers for this problem.

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, 1.0, 2.0, 1.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>