2022-01-21 16:59:54 django 编辑:黎为乐
绘制图表
def draw():
x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
y_data = [[random.randint(100, 200) for i in range(10)] for item in x_data]
Box = Boxplot()
Box.add_xaxis(x_data)
Box.add_yaxis("", Box.prepare_data(y_data))
return Box
视图函数:
def index(requests):
template=loader.get_template('index.html')
chart=draw()
context = dict(
pyechart=chart.render_embed(), # 必须要有
)
return HttpResponse(template.render(context, requests))
在模板里引用{{pyechart|safe}}
效果如下: