运行之后,在浏览器中打开,效果如下:
定时全量更新图表
前面讲的是一个静态数据的展示的方法,用 Pyecharts 和 Django 结合最主要是实现一种动态更新数据,增量更新数据等功能!
定时全量更新主要是前端主动向后端进行数据刷新,定时刷新的核心在于 HTML 的 setInterval 方法。
那么 index.html
代码就是下面这样的:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Awesome-pyecharts</title> <script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.min.js"></script> <script type="text/javascript" src="https://assets.pyecharts.org/assets/echarts.min.js"></script> </head> <body> <div id="bar" style="width:1600px; height:800px;"></div> <script> var chart = echarts.init(document.getElementById('bar'), 'white', {renderer: 'canvas'}); $( function () { fetchData(chart); setInterval(fetchData, 100); } ); function fetchData() { $.ajax({ type: "GET", url: "http://127.0.0.1:8000/demo/bar", dataType: 'json', success: function (result) { chart.setOption(result.data); } }); } </script> </body> </html>
同时在 demo/views.py
中,增加并修改代码:
views.py
demo/urls.py
中,增加如下代码:
urlpatterns = [ url(r'^pie/$', views.ChartView.as_view(), name='demo'), url(r'^bar/$', views.ChartView.as_view(), name='demo'), url(r'^index/$', views.IndexView.as_view(), name='demo'), ]
运行之后,效果如下:
贴一张以前做的图(因为我懒),效果和上面一样
定时增量更新图表
原理一样,先修改 index.html ,代码如下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Awesome-pyecharts</title> <script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.min.js"></script> <script type="text/javascript" src="https://assets.pyecharts.org/assets/echarts.min.js"></script> </head> <body> <div id="bar" style="width:1600px; height:800px;"></div> <script> var chart = echarts.init(document.getElementById('bar'), 'white', {renderer: 'canvas'}); var old_data = []; $( function () { fetchData(chart); setInterval(fetchData, 2000); } ); function fetchData() { $.ajax({ type: "GET", url: "http://127.0.0.1:8000/demo/line", dataType: "json", success: function (result) { var options = result.data; chart.setOption(options); old_data = chart.getOption().series[0].data; } }); } function getDynamicData() { $.ajax({ type: "GET", url: "http://127.0.0.1:8000/demo/lineUpdate", dataType: 'json', success: function (result) { var options = result.data; old_data.push([options.name, options.value]); chart.setOption({ series: [{ data: old_data }] }); } }); } </script> </body> </html>
细心的你应该可以发现,里面新增了两个请求地址 demo/line
, demo/lineUpdate
so,在 urlpatterns
中增加以下路径的匹配
url(r'^line/$', views.ChartView.as_view(), name='demo'), url(r'^lineUpdate/$', views.ChartView.as_view(), name='demo'),
最后在 views.py
中增加以下代码:
def line_base() -> Line: line = ( Line() .add_xaxis(["{}".format(i) for i in range(10)]) .add_yaxis( series_name="", y_axis=[randrange(50, 80) for _ in range(10)], is_smooth=True, label_opts=opts.LabelOpts(is_show=False), ) .set_global_opts( title_opts=opts.TitleOpts(title="动态数据"), xaxis_opts=opts.AxisOpts(type_="value"), yaxis_opts=opts.AxisOpts(type_="value"), ) .dump_options_with_quotes() ) return line class ChartView(APIView): def get(self, request, *args, **kwargs): return JsonResponse(json.loads(line_base()) cnt = 9 class ChartUpdateView(APIView): def get(self, request, *args, **kwargs): global cnt cnt = cnt + 1 return JsonResponse({"name": cnt, "value": randrange(0, 100)})
运行并打开,效果如下:
全部代码已上传,后台回复 django 获取