Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

237
Vistas
Plotly JS - replace Y data dynamically and redraw chart

I have a Plotly.JS chart with altitude chart in meters.

What I want to do is a button, where the user can click to change the units from meters to feet, or back from feets to meters. The chart of course should update.

HTML part:

<head>
  <!-- Plotly.js -->
    <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>

<body>
<button id="btn" onclick="myFunction()">meters/feet</button>
<div id="altitudeDiv" class="plot_synchro" style="width: 99%"></div>
</body>

JS part:

var data_alt = [{
  x: ['2022-03-01 10:50', '2022-03-01 11:50', '2022-03-01 12:50'],
  y: [100, 120, 110],
  type: 'scatter',
  line: {
    color: '#347C2C'
  },
  fill: 'tozeroy',
}];
var layout = {
  height: 200,
  margin: {
    l: 35,
    r: 20,
    b: 50,
    t: 10,
    pad: 5
  },
  automargin: true,

};

var options = {
  displayModeBar: false,
  responsive: true
};

Plotly.newPlot('altitudeDiv', data_alt, layout, options);

function myFunction() {
    alert('test');
  // here we should change the X values from meters to feet
  // feet = meters * 3.2
  // or from feet back to meters
  
  // I can add new data but not sure how to iterate over existing data and replace the values
  data_alt[0]['x'][4] = '2022-03-01 13:50';
  data_alt[0]['y'][4] = '115';
  Plotly.redraw('altitudeDiv');
  
}

The JS Fiddle: https://jsfiddle.net/a2m68nzu/

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda