I am facing this challenge of creating multiple pieChart plot in my html page.
I have a for loop in the page and I want to plot the data.
I just insert the script inside the loop but it doesn't work.
the code is:
{% for welTsts in WellTsTProDChart %}
<div id='{{welTsts.TestDate}}' class="container">
<div class="row">
<div class="col-lg-6">
</div></div>
<div class="col-lg-6">
<div class="card card-success">
<div class="card-header">
<h3 class="card-title">Production Tests: {{ welTsts.TestDate }} Oil {{ welTsts.TestOIL}} (m3/D)</h3>
</div>
<div class="card-body w3-light-grey solid">
<canvas id='{{welTsts.TestDate}}' style="min-height: 230px; height: 230px; max-height: 245px; max-width: 100%;"</canvas>
{% block custom_js %}
<script>
$(document).ready(function(){
//Data Set for PIE CHart
var pieData = {
labels: [
'OIl',
'Water'
],
datasets: [
{
data: [{{ welTsts.TestOIL}} , {{ welTsts.TestWtr}}],
backgroundColor : ['#00a65a', '#5088f1'],
}
]
}
var pieChartCanvas = $('#{{welTsts.TestDate}}').get(0).getContext('2d')
var pieOptions = {
maintainAspectRatio : false,
responsive : true,
}
//Create pie or douhnut chart
// You can switch between pie and douhnut using the method below.
var pieChart = new Chart(pieChartCanvas, {
type: 'pie',
data: pieData,
options: pieOptions
})
})
</script>
{% endblock custom_js %}
</div></div></div></div></div>
{% endfor %}
when I have just one data it doesn't know the variables (data: [{{ welTsts.TestOIL}} , {{ welTsts.TestWtr}}]) and if I set fixed number like data: [70, 30] it plot it but just one plot even if there is many plot to show as the images below. it take the last var inside the script and put it in the first element in for loop!
and after adding unique Id