JSFiddle - React, Tailwind, and code Playground
by Ashish Rajak
HTML
<div id="chartContainer0" style="height: 200px; width: 30%;"></div>
<div id="chartContainer1" style="height: 200px; width: 30%;"></div>
<div id="chartContainer2" style="height: 200px; width: 30%;"></div>
<div id="chartContainer3" style="height: 200px; width: 30%;"></div>
<div id="chartContainer4" style="height: 200px; width: 30%;"></div>
<div id="chartContainer5" style="height: 200px; width: 30%;"></div>
<script type="text/javascript" src="http://canvasjs.com//assets/script/canvasjs.min.js"></script>
JavaScript
var chartOptions = [
{
theme: "theme2",
title:{
text: "Gaming Consoles Sold in 2012"
},
data: [
{
type: "pie",
dataPoints: [
{ y: 4181563, indexLabel: "PlayStation 3" },
{ y: 2175498, indexLabel: "Wii" },
{ y: 3125844, indexLabel: "Xbox 360" },
{ y: 1176121, indexLabel: "Nintendo DS"},
{ y: 1727161, indexLabel: "PSP" },
{ y: 4303364, indexLabel: "Nintendo 3DS"},
{ y: 1717786, indexLabel: "PS Vita"}
]
}
]
},{
theme: "theme1",
title:{
text: "Gaming Consoles Sold in 2013"
},
data: [
{
type: "pie",
dataPoints: [
{ y: 4181563, indexLabel: "PlayStation 3" },
{ y: 2175498, indexLabel: "Wii" },
{ y: 3125844, indexLabel: "Xbox 360" },
{ y: 1176121, indexLabel: "Nintendo DS"},
{ y: 1727161, indexLabel: "PSP" },
{ y: 4303364, indexLabel: "Nintendo 3DS"},
{ y: 1717786, indexLabel: "PS Vita"}
]
}
]
},{
theme: "theme3",
title:{
text: "Gaming Consoles Sold in 2015"
},
data: [
{
type: "pie",
dataPoints: [
{ y: 4181563, indexLabel: "PlayStation 3" },
{ y: 2175498, indexLabel: "Wii" },
{ y: 3125844, indexLabel: "Xbox 360" },
{ y: 1176121, indexLabel: "Nintendo DS"},
{ y: 1727161, indexLabel: "PSP" },
{ y: 4303364, indexLabel: "Nintendo 3DS"},
{ y: 1717786, indexLabel: "PS Vita"}
]
}
]
}
];
var containerName,
randomNumber = Math.random()*4; // generates random number which can be your condition
var numberOfPie;
if(randomNumber < 2) // condition for the number of piecharts you want to render
numberOfPie= 5;
else numberOfPie= 4;
for(var i=0; i< numberOfPie ; i++) // can either generate 4 or 5 pie charts based on condition
{
containerName = "chartContainer"+ i;
var chart = new CanvasJS.Chart(containerName,chartOptions[i%chartOptions.length]);
chart.render();
}