Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React-plotly set default coordinates which the plot is center about

How can I set a default zoom level and center my plot around the origin (0,0)? in react-plotly
I am not sure what options are responsible for this in the layout object.

import React from 'react';
import Plot from 'react-plotly.js';

export default function App() {

  var layout = {
    shapes: [
      {
        type: 'circle',
        xref: 'x',
        yref: 'y',
        x0: -2,
        y0: -1.56,
        x1: 2,
        y1: 2.09,
        opacity: 0.2,
        fillcolor: 'red',
        line: {
          color: 'red'
        }
      }
    ],
    height: 400,
    width: 480,
    showlegend: false,
    dragmode: 'pan'
  }

  return (
    <Plot
      layout={layout}
    />
  );
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Define the x and y axis and use constraintoward: "center". Note you may need to extrapolate the max and min values inside each range from your data if its not static:

import React from "react";
import Plot from "react-plotly.js";

export default function App() {
  var layout = {
    shapes: [
      {
        type: "circle",
        xref: "x",
        yref: "y",
        x0: -2,
        y0: -1.56,
        x1: 2,
        y1: 2.09,
        opacity: 0.2,
        fillcolor: "red",
        line: {
          color: "red",
        },
      },
    ],
    yaxis: {
      visible: true,
      constraintoward: "center",
      showticklabels: false,
      range: [-10, 10],
    },
    xaxis: {
      visible: true,
      constraintoward: "center",
      showticklabels: false,
      range: [-10, 10],
    },
    height: 400,
    width: 480,
    showlegend: false,
    dragmode: "pan",
  };

  return <Plot layout={layout} />;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading