Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shapes are misplaced when using numbers as categoryarray values #5767

Open
perdodi opened this issue Jun 25, 2021 · 6 comments
Open

Shapes are misplaced when using numbers as categoryarray values #5767

perdodi opened this issue Jun 25, 2021 · 6 comments

Comments

@perdodi
Copy link

perdodi commented Jun 25, 2021

If X axis type is category, and categoryarray contains (string type) numbers, then shapes are horizontally misplaced, as x0 and x1 values in the shape are not considered as items, but as indices of the categoryarray.

Eg.

This one works perfectly:

<div id="tester"></div>
<script>
    var layout = {
      "xaxis": {
        "type": "category",
        "categoryarray": [
          "red", "green", "blue", "purple", "cyan", "yellow", "magenta"
        ],
      },
      "shapes": [
        {
          "type": "rect",
          "xref": "x",
          "yref": "y",
          "x0": "purple",
          "x1": "yellow",
          "y0": 10,
          "y1": 20,
          "fillcolor": "rgba(45,180,135,0.3)",
        }
      ]
    };

    var data = [
      {
        "x": [ "red", "blue", "cyan", "magenta" ],
        "y": [ 10, 20, 30, 40],
      },
    ];
    
    TESTER = document.getElementById('tester');
    Plotly.newPlot( TESTER, data, layout );
</script>

image

But if I replace the color names to numbers:

  // in "categoryarray":
      "10", "20", "30", "40", "50", "60", "70"
...
  // in "shapes" this won't work:
      "x0": "40",
      "x1": "60",
...
 // in data:
    "x": [ "10", "30", "50", "70" ],

image

It will work only if I replace the category values to their indices in the shapes:

      "x0": 3,
      "x1": 5,

image

Note: if this is not intended to be a bug, but an undocumented feature, please document it, so I can rely on it later. 😃

@nicholas-esterer
Copy link
Contributor

nicholas-esterer commented Aug 3, 2021

This seems to be due to the behaviour of the package fast-isnumeric, which is used in

return isNumeric(v) ? +v : getCategoryIndex(v);

Indeed, in a nodejs console:

> var isNumeric = require('fast-isnumeric');
> isNumeric(40)
true
> isNumeric("40")
true

This makes it so that getCategoryIndex is not called even when the value is a string that looks like a number (e.g., "40").
@alexcjohnson is this behaviour desired?

@nicholas-esterer
Copy link
Contributor

also related: #5741

@alexcjohnson
Copy link
Collaborator

See also autotypenumbers: 'strict' #5240

@alexcjohnson
Copy link
Collaborator

(which is now the default for graphs made with plotly.py)

@nicholas-esterer
Copy link
Contributor

Ok I see. Setting autotypenumbers: "strict" in layout or layout.xaxis (relevant to this use case) doesn't fix the bug, but to be fair the docs just mention "trace data" and not the coordinates of shapes (or images or annotations for that matter). But the expected behaviour is most likely to follow what the axis' autotypesnumbers setting says to do for shapes as well.

@alexcjohnson
Copy link
Collaborator

Yep, that sounds right - if an axis is in strict mode, that should apply to shapes, annotations, and images as well as traces. So that part of this issue can be considered a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants