I've created 2 custom buttons (icons) for my Plotly.js modeBar: play and pause.
I want to switch/toggle between this icons and I'm able to do it, however I'm not happy with the result and would like to know if there is a better way without calling Plotly again.
const paused = false
// Custom icons
const play = {
name: 'Pause',
icon: {
width: 500,
height: 500,
path: 'M424.4 214.7L72.4 6.6C43.8-10.3 0 6.1 0 47.9V464c0 37.5 40.7 60.1 72.4 41.3l352-208c31.4-18.5 31.5-64.1 0-82.6z',
},
click: () => Plotly.plot(chart, [], {}, { modeBarButtonsToAdd: [pause] })
}
const pause = {
name: 'Play',
icon: {
width: 500,
height: 500,
path: 'M144 479H48c-26.5 0-48-21.5-48-48V79c0-26.5 21.5-48 48-48h96c26.5 0 48 21.5 48 48v352c0 26.5-21.5 48-48 48zm304-48V79c0-26.5-21.5-48-48-48h-96c-26.5 0-48 21.5-48 48v352c0 26.5 21.5 48 48 48h96c26.5 0 48-21.5 48-48z',
},
click: () => Plotly.plot(chart, [], {}, { modeBarButtonsToAdd: [play] })
}
// Chart
const plot = Plotly.plot(chart, [{
x: [1, 2, 3, 4],
y: [10, 15, 13, 17],
type: 'scatter'
}], {}, {
displaylogo: false,
displayModeBar: true,
modeBarButtonsToAdd: [paused ? pause : play]
})
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<div id="chart"></div>
Run the above snippet and click on the last icon.
Also, I implemented another version using the .active class and setting the icons with CSS but again would like to figure out how Plotly.js experts solve this.