My chart data is based on a list of dictionaries. It's a Django project, so my data is defined in views.py
: I count the number of records with Mike, Jane and Jack.
mylist=
[{'Date': '2021-10-02', 'ID': 11773, 'Receiver': Mike},
{'Date': '2021-10-02', 'ID': 15673, 'Receiver': Jane},
{'Date': '2021-10-03', 'ID': 11773, 'Receiver': Mike},
...
{'Date': '2021-12-25', 'ID': 34653, 'Receiver': Jack}]
mike=len(tuple(d for d in mylist if d['Receiver'] == 'Mike'))
jane=len(tuple(d for d in mylist if d['Receiver'] == 'Jane'))
jack=len(tuple(d for d in mylist if d['Receiver'] == 'Jack'))
count = [mike, jane, jack]
I have already drawn a pie chart based on my total counted data in my chart.HTML
file :
<!-- pie Chart -->
<div class="col-xl-4 col-lg-4">
<div class="card shadow mb-4">
<div
class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
<h6 class="m-0 font-weight-bold">Team Chart</h6>
</div>
<!-- Card Body -->
<div class="card-body">
<div class="chart-area">
<canvas id="myPieChart"></canvas>
<script>
var ctx = document.getElementById("myPieChart");
var myPieChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: ["Mike", "Jane", "Jack"],
datasets: [{
data: {{count}} ,
backgroundColor: ['#4e73df', '#1cc88a', '#36b9cc'],
hoverBackgroundColor: ['#2e59d9', '#17a673', '#2c9faf'],
hoverBorderColor: "rgba(234, 236, 244, 1)",
}],
},
});
</script>
</div>
</div>
</div>
</div>
Instead of getting the total count, I want to draw the chart based on a selected time range.
I want to input a date range and draw the same pie chart based on that date range. I tried the following codes to add the input dates in my chart.HTML
file:
<div class="d-sm-flex align-items-center justify-content-between mb-4">
<form method="get" action="chart/">
<div class="form-row">
<label for="start">Start Date:</label>
<div class="col">
<input type="date" class="form-control" name="start" min="2020-01-03" required>
</div>
<label for="end">End Date:</label>
<div class="col">
<input type="date" class="form-control" name="end" min="2020-01-03" required>
</div>
<button type="submit" class="btn btn-primary"><i class="fas fa-download fa-sm text-white-50"></i> Generate Report</button>
</div>
</form>
The output of this date selector on my HTML page is:
This step will allow me to append the input date value to URL when I click "generate report". For example: it will append: ?start=2022-02-02&end=2022-02-24
in the URL link.
I don't know how to link these input dates(URL appended with input dates) with the first field of my list(Date) in my pie chart. I think I should add date data in my javascript file. Maybe I should add something here data: {{count}}
.