python用Echarts进行图表展示

python用Echarts进行图表展示,第1张


一、用python进行数据提取

import sqlite3

from flask import Flask, render_template

app = Flask(__name__)


@app.route('/')
def hello_world():  # put application's code here
    score = []
    num = []
    con = sqlite3.connect("movie.db")
    print("成功打开数据库")
    cur = con.cursor()
    sql = "select score,count(score) from movie25 group by score"
    data = cur.execute(sql)
    for item in data:
        score.append(item[0])
        num.append(item[1])
    cur.close()

    return render_template("score.html", score =score, num =num)


if __name__ == '__main__':
    app.run()


二、用图表展示

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="/static/assets/js/echart.min.js">script>
    <title>Titletitle>
head>
<body>

  
  <div id="main" style="width:1000px;height:600px;">div>
    <script type="text/javascript">
      // 基于准备好的dom,初始化echarts实例
      var myChart = echarts.init(document.getElementById('main'));

      // 指定图表的配置项和数据

      var option = {
        title: {
          text: '电影评分统计表',
          left: 'center'
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        legend: {
          data: ['销量']
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: {
          type: 'category',
          data: {{ score }}
        },
        yAxis: {
            type: 'value'
        },
        series: [
          {
            data: {{ num }},
            type: 'bar',
            barWidth: '60%'
          }
        ]
      };



      // 使用刚指定的配置项和数据显示图表。


myChart.setOption(option); script> body> html>

最后的成果如下:

代码目录如下:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/571431.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存