Looking for a way to toggle between two charts (created with charts.js) by clicking a button?
Initially, I had them in separate divs, one hidden and the other visible:
<div id="one">
<canvas id="myChart1" width="400" height="400"></canvas>
</div>
<div id="two" class="hidden">
<canvas id="myChart2" width="400" height="400"></canvas>
</div>
Removing the 'hidden' class resulted in the chart not being displayed.
You can find an example on JSFiddle: http://jsfiddle.net/2rzkpzy9/12/
Is there a workaround or another approach to switch between these charts?
var data1 = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.2)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: [65, 59, 80, 81, 56, 55, 40]
}]
};
var data2 = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
label: "My First dataset",
fillColor: "#FD6260",
strokeColor: "#FD6260",
pointColor: "#FD6260",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: [65, 59, 80, 81, 56, 55, 40]
}]
};
var options = {
scaleShowGridLines : false,
scaleShowHorizontalLines: false,
scaleShowVerticalLines: false,
}
var cxt = document.getElementById("myChart1").getContext("2d");
var chart = new Chart(cxt).Bar(data1,options);
var cxt2 = document.getElementById("myChart2").getContext("2d");
var chart2 = new Chart(cxt2).Bar(data2,options);
swap= function() {
console.log(document.getElementById("one").className);
document.getElementById("one").className = "hidden";
document.getElementById("two").className = '';
console.log(document.getElementById("one").className);
}
.hidden {
display: none;
}
<script src="http://www.chartjs.org/assets/Chart.js"></script>
<div id="one">
<canvas id="myChart1" width="400" height="400"></canvas>
</div>
<div id="two" class="hidden">
<canvas id="myChart2" width="400" height="400"></canvas>
</div>
<button type="button" class="btn" onClick="swap()">Swap!</button>
<div id="info"></div>