I found a way to resolve my list issue. Here is the code:
<script>
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: [{% for Date in Date %}"{{ Date }}",{%endfor%}],
datasets: [{
label: 'SAFRAN',
data: {{ Close }},
borderColor: 'rgba(255, 99, 132, 1)',
}]
},
});
</script>
I am trying to plot a line chart using Django and ChartJs.
I created a list of dates in my view file that I would like to use as x-axis data in my chart.
I rendered my list of dates in my template through built-in template tags {{ date }}. When I inspect the HTML file and see the content of my labels, the list has ' prior each date element in my list (see capture).
HTML inspection capture
I think this is why the chart would not display but I can’t figure out why I have this added in my list. Do you guys have any idea ?
Thank you for your help.
[from django.shortcuts import render
import yfinance as yf
import pandas as pd
from datetime import datetime as dt
def HomeView(request, *args, **kwargs):
data = yf.download(tickers='SAF.PA')
data['Date'] = data.index
data['Date'] = data['Date'].dt.strftime("%Y-%m-%d")
context = {
"Date" : data.Date[0:5].tolist(),
"Close" : data.Close[0:5].tolist()
}
return render(request, "home.html", context)][1]