gengns ES

An entry from the gengns log, written by Génesis García Morilla · · CSS3, HTML5, JavaScript

Responsive Time Series with JavaScript and Plotly

This is a basic but really useful (less is more) example of how to use Plotly to show a measure varying over time (live). You can resize the window here or on JSFiddle to check how it fits to the render area.

JavaScript

// New line plot with random data between 0 and 1
Plotly.newPlot('chart', [{
  x: [new Date()],
  y: [Math.random()],
  mode: 'lines',
  line: { color: 'grey' }
}], { 
  title: 'Streaming simulation with random data',
  font: { size: 16, color: 'grey' }
}, { 
  responsive: true 
})

// Streaming simulation with setInterval
setInterval(() => {
  Plotly.extendTraces('chart', {
    x: [[new Date()]],
    y: [[Math.random()]]
  }, [0])
}, 1e3)

HTML

<div id="chart"></div>

CSS

body {
  margin: 0;
  padding: 0;
}

#chart {
  width: 100vw;
  height: 100vh;
}
Open in JSFiddle

Tags: line chart, plotly, time