如何在Python的Flask中使用Vue的FunnelGraph.js

时间:2019-09-15 05:33:26

标签: vue.js flask

我对Vue和Python Flask还是陌生的。我一直在寻找Vue提供的一些图表。其中之一是FunnelGraph.js。但是,我不太确定如何将它们集成在一起。

我尝试遵循文档https://github.com/greghub/funnel-graph-js#usage,但是我不确定在哪里使用graph变量。我选择使用它的CDN。

在python的Flask的app.py中,

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/about')
def about():
    return render_template('about.html')

if __name__ == "__main__":
  app.run(debug=True)

about.html中,我有

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    <link rel="stylesheet" type="text/css" href="https://unpkg.com/funnel-graph-js@1.3.9/dist/css/main.min.css">
    <link rel="stylesheet" type="text/css" href="https://unpkg.com/funnel-graph-js@1.3.9/dist/css/theme.min.css">
    <title></title>
  </head>
  <body>
    <div id="graph">
      <h1>graph</h1>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/funnel-graph-js@1.3.9/dist/js/funnel-graph.min.js"></script>
    <script>

      var graph = new FunnelGraph({
          container: '.funnel',
          gradientDirection: 'horizontal',
          data: [12000, 5700, 360],
          displayPercent: true,
          direction: 'horizontal'
      });

      graph.draw();
    </script>
  </body>
</html>

在当前设置下,我从chrome inspector控制台收到了错误消息:

funnel-graph.min.js:1 Uncaught TypeError: Invalid attempt to spread non-iterable instance
    at funnel-graph.min.js:1
    at g (funnel-graph.min.js:1)
    at e.value (funnel-graph.min.js:1)
    at new e (funnel-graph.min.js:1)
    at about:21

1 个答案:

答案 0 :(得分:1)

看起来像FunnelGraph的问题:将data定义为数组时,尽管他们在文档中有此示例,但似乎引发了此错误。要解决此问题,请将数据定义为对象:

var graph = new FunnelGraph({
    container: '#graph',
    gradientDirection: 'horizontal',
    displayPercent: true,
    direction: 'horizontal',
    data: {
        values: [12000, 5700, 360]
    },
});

graph.draw();

我还必须将容器选择器调整为#graph,因为这就是标记中的内容。