我正在使用烧瓶建立一个网站。在app.py文件中,我计算了一些传递给html页面的统计信息。
@app.route('/')
def index():
values = [10, 11, 7, 15, 19, 5, 7.5, 10.9]
days= ["12/18/18","12/25/18","1/1/19","1/8/19","1/15/19","1/22/19","1/29/19","2/5/19"]
return render_template('index.html', days = days, values=values)
index.html中的javascript如下
var ctx = document.getElementById("lineChart");
var lineChart = new Chart(ctx, {
type: 'line',
data: {
labels: {{days}}
datasets: {
label: "All users",
backgroundColor: "rgba(30,144,255, 0.31)",
borderColor: "rgba(30,144,255, 0.7)",
pointBorderColor: "rgba(30,144,255, 0.7)",
pointBackgroundColor: "rgba(30,144,255, 0.7)",
pointHoverBackgroundColor: "#fff",
pointHoverBorderColor: "rgba(220,220,220,1)",
pointBorderWidth: 1,
data: {{values}}
}
},
});
我在javascript中测试了两个选项:
不工作(无法生成图表)
labels: {{days}}
data: {{values}}
如果我明确提供字符串列表
,则工作labels: ["12/18/18","12/25/18","1/1/19","1/8/19","1/15/19","1/22/19","1/29/19","2/5/19"]
data: {{values}}
似乎传递数值数组(本例中的值)很好,但传递字符串列表会带来麻烦。
您有什么建议可以解决这个问题吗? 非常感谢!