I'm trying to add some elements on a chart, and I'm doing it in the onrendered
function passed to billboard.js
configuration.
In this function, I have to know if some series are or not on the chart, and this can be done by easily checking their opacities.
Now, in the example below, you can see that sometimes the opacity transitions are not done when the code in onrendered
fires.
How to make sure that what happens in onrendered
is after all the series are shown/hidden from the chart? So after all the transitions ended.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://d3js.org/d3.v4.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/billboard.js/dist/billboard.min.css" />
<script src="https://cdn.jsdelivr.net/npm/billboard.js/dist/billboard.pkgd.min.js"></script>
<title>JS Bin</title>
</head>
<body>
<div id="chart"></div>
<script>
console.log('should be d1=1,d2=1')
var c = bb.generate({
bindto: "#chart",
size: {
width: 500,
height: 250
},
transition: 0,
data: {
x: "x",
columns: [
["x", "2013-01-01", "2013-01-02", "2013-01-03", "2013-01-04", "2013-01-05", "2013-01-06"],
["data1", 30, 200, 100, 400, 150, 250],
["data2", 130, 340, 200, 500, 250, 350],
]
},
axis: {
x: {
type: "timeseries",
tick: {
format: "%Y-%m-%d"
}
}
},
onrendered: function() {
var data1op = d3.select(".bb-chart-line.bb-target.bb-target-data1").style("opacity");
var data2op = d3.select(".bb-chart-line.bb-target.bb-target-data2").style("opacity");
console.log(`data1 op: ${data1op} --- data2 op: ${data2op}`);
}
});
setTimeout(function(){console.log('should be d1=0,d2=1');c.hide('data1')},500);
setTimeout(function(){console.log('should be d1=0,d2=0');c.hide('data2')},1000);
setTimeout(function(){console.log('should be d1=1,d2=0');c.show('data1')},1500);
setTimeout(function(){console.log('should be d1=1,d2=1');c.show('data2')},2000);
setTimeout(function(){console.log('more waiting between hide/show')},3500);
setTimeout(function(){console.log('should be d1=0,d2=1');c.hide('data1')},5000);
setTimeout(function(){console.log('should be d1=0,d2=0');c.hide('data2')},6000);
setTimeout(function(){console.log('should be d1=1,d2=0');c.show('data1')},7000);
setTimeout(function(){console.log('should be d1=1,d2=1');c.show('data2')},8000);
</script>
</body>
</html>