图表
一般需要用到两种插件
- highchart:国外的
- echarts:国内的,百度开源的
修改myproject/app01/templates/layout.html
添加数据统计菜单
<li><a href="/chart/list/">数据统计</a></li>
新建myproject/app01/views/chart.py
修改myproject/myproject/urls.py
添加路由
#数据统计 path('chart/list/', chart.chart_list),
修改myproject/app01/views/chart.py
from django.shortcuts import render def chart_list(request): """数据统计页面""" return render(request, 'chart_list.html')
新建myproject/app01/templates/chart_list.html
获取echarts
获取方式: https://registry.npmjs.org/echarts/-/echarts-5.4.1.tgz
下载完成后
将此文件放到项目静态文件目录下
修改myproject/app01/templates/chart_list.html
引入Apache ECharts
{% extends 'layout.html' %} {% load static %} {% block content %} <div class="container"> <div class="panel panel-default"> <div class="panel-heading">折线图</div> <div class="panel-body"> 图图图 </div> </div> <div class="row"> <div class="col-sm-8"> <div class="panel panel-default"> <div class="panel-heading">柱状图</div> <div class="panel-body"> 图图图 </div> </div> </div> <div class="col-sm-4"> <div class="panel panel-default"> <div class="panel-heading">饼图</div> <div class="panel-body"> 图图图 </div> </div> </div> </div> </div> {% endblock %} {% block js %} <script src="{% static 'js/echarts.js' %}"></script> {% endblock %}
绘制一个简单的柱状图
1.在绘图前我们需要为 ECharts 准备一个定义了高宽的 DOM 容器。
2.然后就可以通过 echarts.init 方法初始化一个 echarts 实例并通过 setOption 方法生成一个简单的柱状图。
修改myproject/app01/templates/chart_list.html
{% extends 'layout.html' %} {% load static %} {% block content %} <div class="container"> <div class="panel panel-default"> <div class="panel-heading">折线图</div> <div class="panel-body"> 图图图 </div> </div> <div class="row"> <div class="col-sm-8"> <div class="panel panel-default"> <div class="panel-heading">柱状图</div> <div class="panel-body"> <div id="m2" style="width: 600px;height: 400px;"></div> </div> </div> </div> <div class="col-sm-4"> <div class="panel panel-default"> <div class="panel-heading">饼图</div> <div class="panel-body"> 图图图 </div> </div> </div> </div> </div> {% endblock %} {% block js %} <script src="{% static 'js/echarts.js' %}"></script> <script type="text/javascript"> // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('m2')); // 指定图表的配置项和数据 var option = { title: { text: 'ECharts 入门示例' }, tooltip: {}, legend: { data: ['销量'] }, xAxis: { data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子'] }, yAxis: {}, series: [ { name: '销量', type: 'bar', data: [5, 20, 36, 10, 10, 20] } ] }; // 使用刚指定的配置项和数据显示图表。 myChart.setOption(option); </script> {% endblock %}
稍微修改一下柱状图
修改myproject/app01/templates/chart_list.html
修改myproject/app01/templates/chart_list.html
将图例放在底部
legend: { data: ['销量','业绩'], bottom: 0 },
修改myproject/app01/templates/chart_list.html
将图的标题居中
title: { text: 'ECharts 入门示例', textAlign: 'auto', left: 'center' },
现在表是写定的,怎么通过Ajax从后台获取数据然后展示呢
修改myproject/app01/templates/chart_list.html
{% extends 'layout.html' %}
{% load static %}
{% block content %}
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">折线图</div>
<div class="panel-body">
图图图
</div>
</div>
<div class="row">
<div class="col-sm-8">
<div class="panel panel-default">
<div class="panel-heading">柱状图</div>
<div class="panel-body">
<div id="m2" style="width: 100%;height: 400px;"></div>
</div>
</div>
</div>
<div class="col-sm-4">
<div class="panel panel-default">
<div class="panel-heading">饼图</div>
<div class="panel-body">
<div id="m3" style="width: 100%;height: 400px;"></div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block js %}
<script src="{% static 'js/echarts.js' %}"></script>
<script type="text/javascript">
$(function () {
initBar();
})
/**
*初始化柱状图
*/
function initBar() {
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('m2'));
// 指定图表的配置项和数据
var option = {
title: {
text: 'ECharts 入门示例',
{#subtext: '小标题',#}
textAlign: 'auto',
left: 'center'
},
tooltip: {},
legend: {
data: [], //后台获取
bottom: 0
},
xAxis: {
data: [] //后台获取
},
yAxis: {},
series: [] //后台获取
};
$.ajax({
url: "/chart/bar/",
type: "get",
dataType: "JSON",
success: function (res) {
//将后台返回的数据,更新到option中
if (res.status) {
option.legend.data = res.data.legend;
option.xAxis.data = res.data.x_axis;
option.series = res.data.series_list;
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
}
}
})
}
</script>
{% endblock %}
修改myproject/myproject/urls.py
添加路由
path('chart/bar/', chart.chart_bar),
修改myproject/app01/views/chart.py
from django.shortcuts import render from django.http import JsonResponse def chart_list(request): """数据统计页面""" return render(request, 'chart_list.html') def chart_bar(request): """构造柱状图的数据""" #数据可以去数据库中获取 legend = ["11","77"] series_list = [ { "name": '11', "type": 'bar', "data": [5, 20, 36, 10, 10, 20] }, { "name": '77', "type": 'bar', "data": [53, 12, 13, 10, 20, 27] } ] x_axis = ['1月', '2月', '3月', '4月', '5月', '6月'] result = { "status":True, "data":{ 'legend':legend, 'series_list': series_list, 'x_axis':x_axis } } return JsonResponse(result)
饼状图
修改myproject/app01/templates/chart_list.html
{% extends 'layout.html' %} {% load static %} {% block content %} <div class="container"> <div class="panel panel-default"> <div class="panel-heading">折线图</div> <div class="panel-body"> 图图图 </div> </div> <div class="row"> <div class="col-sm-8"> <div class="panel panel-default"> <div class="panel-heading">柱状图</div> <div class="panel-body"> <div id="m2" style="width: 100%;height: 400px;"></div> </div> </div> </div> <div class="col-sm-4"> <div class="panel panel-default"> <div class="panel-heading">饼图</div> <div class="panel-body"> <div id="m3" style="width: 100%;height: 400px;"></div> </div> </div> </div> </div> </div> {% endblock %} {% block js %} <script src="{% static 'js/echarts.js' %}"></script> <script type="text/javascript"> $(function () { initBar(); iniPie(); }) /** *初始化柱状图 */ function initBar() { // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('m2')); // 指定图表的配置项和数据 var option = { title: { text: 'ECharts 入门示例', {#subtext: '小标题',#} textAlign: 'auto', left: 'center' }, tooltip: {}, legend: { data: [], //后台获取 bottom: 0 }, xAxis: { data: [] //后台获取 }, yAxis: {}, series: [] //后台获取 }; $.ajax({ url: "/chart/bar/", type: "get", dataType: "JSON", success: function (res) { //将后台返回的数据,更新到option中 if (res.status) { option.legend.data = res.data.legend; option.xAxis.data = res.data.x_axis; option.series = res.data.series_list; // 使用刚指定的配置项和数据显示图表。 myChart.setOption(option); } } }) } /** *初始化饼状图 */ function iniPie() { // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('m3')); option = { title: { text: 'Referer of a Website', subtext: 'Fake Data', left: 'center' }, tooltip: { trigger: 'item' }, legend: { bottom: 0 }, series: [ { name: 'Access From', type: 'pie', radius: '50%', data: [ {value: 1048, name: 'Search Engine'}, {value: 735, name: 'Direct'}, {value: 580, name: 'Email'}, {value: 484, name: 'Union Ads'}, {value: 300, name: 'Video Ads'} ], emphasis: { itemStyle: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)' } } } ] }; // 使用刚指定的配置项和数据显示图表。 myChart.setOption(option); } </script> {% endblock %}
同样通过Ajax从后台获取数据然后用饼图展示
修改myproject/app01/templates/chart_list.html
{% extends 'layout.html' %} {% load static %} {% block content %} <div class="container"> <div class="panel panel-default"> <div class="panel-heading">折线图</div> <div class="panel-body"> 图图图 </div> </div> <div class="row"> <div class="col-sm-8"> <div class="panel panel-default"> <div class="panel-heading">柱状图</div> <div class="panel-body"> <div id="m2" style="width: 100%;height: 400px;"></div> </div> </div> </div> <div class="col-sm-4"> <div class="panel panel-default"> <div class="panel-heading">饼图</div> <div class="panel-body"> <div id="m3" style="width: 100%;height: 400px;"></div> </div> </div> </div> </div> </div> {% endblock %} {% block js %} <script src="{% static 'js/echarts.js' %}"></script> <script type="text/javascript"> $(function () { initBar(); iniPie(); }) /** *初始化柱状图 */ function initBar() { // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('m2')); // 指定图表的配置项和数据 var option = { title: { text: 'ECharts 入门示例', {#subtext: '小标题',#} textAlign: 'auto', left: 'center' }, tooltip: {}, legend: { data: [], //后台获取 bottom: 0 }, xAxis: { data: [] //后台获取 }, yAxis: {}, series: [] //后台获取 }; $.ajax({ url: "/chart/bar/", type: "get", dataType: "JSON", success: function (res) { //将后台返回的数据,更新到option中 if (res.status) { option.legend.data = res.data.legend; option.xAxis.data = res.data.x_axis; option.series = res.data.series_list; // 使用刚指定的配置项和数据显示图表。 myChart.setOption(option); } } }) } /** *初始化饼状图 */ function iniPie() { // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('m3')); var option = { title: { text: '部门预算占比', subtext: '分公司', left: 'center' }, tooltip: { trigger: 'item' }, legend: { bottom: 0 }, series: [ { name: '预算', type: 'pie', radius: '50%', data: [ {value: 1048, name: 'IT部门'}, {value: 735, name: '运营'}, {value: 580, name: '新媒体'}, ], emphasis: { itemStyle: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)' } } } ] }; $.ajax({ url: "/chart/pie/", type: "get", dataType: "JSON", success: function (res) { //将后台返回的数据,更新到option中 if (res.status) { option.series[0].data = res.data; // 使用刚指定的配置项和数据显示图表。 myChart.setOption(option); } } }) } </script> {% endblock %}
修改myproject/myproject/urls.py
添加路由
path('chart/pie/', chart.chart_pie),
修改myproject/app01/views/chart.py
from django.shortcuts import render from django.http import JsonResponse def chart_list(request): """数据统计页面""" return render(request, 'chart_list.html') def chart_bar(request): """构造柱状图的数据""" #数据可以去数据库中获取 legend = ["11","77"] series_list = [ { "name": '11', "type": 'bar', "data": [5, 20, 36, 10, 10, 20] }, { "name": '77', "type": 'bar', "data": [53, 12, 13, 10, 20, 27] } ] x_axis = ['1月', '2月', '3月', '4月', '5月', '6月'] result = { "status":True, "data":{ 'legend':legend, 'series_list': series_list, 'x_axis':x_axis } } return JsonResponse(result) def chart_pie(request): """构造饼图的数据""" db_data_list = [ {"value": 2048, "name": 'IT部门'}, {"value": 735, "name": '运营'}, {"value": 580, "name": '新媒体'}, ] result = { "status": True, "data": db_data_list } return JsonResponse(result)
折线图
修改myproject/app01/templates/chart_list.html
{% extends 'layout.html' %} {% load static %} {% block content %} <div class="container"> <div class="panel panel-default"> <div class="panel-heading">折线图</div> <div class="panel-body"> <div id="m1" style="width: 100%;height: 300px;"></div> </div> </div> <div class="row"> <div class="col-sm-8"> <div class="panel panel-default"> <div class="panel-heading">柱状图</div> <div class="panel-body"> <div id="m2" style="width: 100%;height: 400px;"></div> </div> </div> </div> <div class="col-sm-4"> <div class="panel panel-default"> <div class="panel-heading">饼图</div> <div class="panel-body"> <div id="m3" style="width: 100%;height: 400px;"></div> </div> </div> </div> </div> </div> {% endblock %} {% block js %} <script src="{% static 'js/echarts.js' %}"></script> <script type="text/javascript"> $(function () { initLine(); initBar(); initPie(); }) /** *初始化折线图 */ function initLine() { // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('m1')); // 指定图表的配置项和数据 var option = { xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, yAxis: { type: 'value' }, series: [ { data: [150, 230, 224, 218, 135, 147, 260], type: 'line' } ] }; // 使用刚指定的配置项和数据显示图表。 myChart.setOption(option); } /** *初始化柱状图 */ function initBar() { // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('m2')); // 指定图表的配置项和数据 var option = { title: { text: 'ECharts 入门示例', {#subtext: '小标题',#} textAlign: 'auto', left: 'center' }, tooltip: {}, legend: { data: [], //后台获取 bottom: 0 }, xAxis: { data: [] //后台获取 }, yAxis: {}, series: [] //后台获取 }; $.ajax({ url: "/chart/bar/", type: "get", dataType: "JSON", success: function (res) { //将后台返回的数据,更新到option中 if (res.status) { option.legend.data = res.data.legend; option.xAxis.data = res.data.x_axis; option.series = res.data.series_list; // 使用刚指定的配置项和数据显示图表。 myChart.setOption(option); } } }) } /** *初始化饼状图 */ function initPie() { // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('m3')); var option = { title: { text: '部门预算占比', subtext: '分公司', left: 'center' }, tooltip: { trigger: 'item' }, legend: { bottom: 0 }, series: [ { name: '预算', type: 'pie', radius: '50%', data: [ {value: 1048, name: 'IT部门'}, {value: 735, name: '运营'}, {value: 580, name: '新媒体'}, ], emphasis: { itemStyle: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)' } } } ] }; $.ajax({ url: "/chart/pie/", type: "get", dataType: "JSON", success: function (res) { //将后台返回的数据,更新到option中 if (res.status) { option.series[0].data = res.data; // 使用刚指定的配置项和数据显示图表。 myChart.setOption(option); } } }) } </script> {% endblock %}
标签:option,series,数据,list,chart,res,data,统计 From: https://www.cnblogs.com/kekeeleven/p/17812361.html