使用Flask加ECharts实现多维数组柱状图展示

目的

给定十二个月的收入和支出,通过柱状图分月进行展示

目录结构

admin@ubuntu18-mypc:~/flask$ tree
.
├── run.py
└── templates
    └── index.html

1 directory, 2 files
admin@ubuntu18-mypc:~/flask$ 

具体内容

admin@ubuntu18-mypc:~/flask$ cat run.py 
from flask import Flask, render_template, request
import json

app = Flask(__name__,static_folder='static')

@app.route('/')
def index():
    expenses = [1000, 1500, 2000, 1800, 1200, 1600, 2200, 2400, 1900, 1700, 1200, 1000]
    incomes = [2000, 2500, 3000, 2800, 2000, 2600, 3200, 3400, 2800, 2600, 2000, 1800]
    return render_template('index.html', expenses=expenses, incomes=incomes)

if __name__ == '__main__':
    app.run(host='0.0.0.0',debug=True)
admin@ubuntu18-mypc:~/flask$ 



admin@ubuntu18-mypc:~/flask$ cat templates/index.html 
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Flask + ECharts绘制每月支出和收入柱形图</title>
    <!-- 引入echarts.min.js文件 -->
    <script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script>
</head>
<body>
    <div id="chart" style="width: 600px; height: 400px;"></div>

    <script type="text/javascript">
        var expenses = {{ expenses|tojson }};
        var incomes = {{ incomes|tojson }};

        // 基于准备好的dom,初始化echarts实例
        var myChart = echarts.init(document.getElementById('chart'));

        // 配置图表参数
        var option = {
            tooltip: {},
            legend: {
                data: ['支出', '收入']
            },
           xAxis: {
                type: 'category',
                data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
            },
            yAxis: {},
            series: [
                {
                    name: '支出',
                    type: 'bar',
                    data: expenses,
                    label: {
                        show: true,
                        position: 'top'
                    }
                },
                {
                    name: '收入',
                    type: 'bar',
                    data: incomes,
                    label: {
                        show: true,
                        position: 'top'
                    }
                }
            ]
        };

        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
    </script>
</body>
</html>
admin@ubuntu18-mypc:~/flask$ 

效果

  • 运行run.py
admin@ubuntu18-mypc:~/flask$ python run.py 
 * Serving Flask app "run" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 288-698-524
  • 浏览器访问 http://[server_ip]:5000 ,得到如下展示
    在这里插入图片描述
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值