gpt4 book ai didi

javascript - 一页中的多个 Highcharts - 仅显示一个图表

转载 作者:行者123 更新时间:2023-11-30 09:17:11 25 4
gpt4 key购买 nike

我正在创建一个 Flask 应用程序,对于我想使用 Highcharts 的图表元素,我设法让一个图表显示在网页上,但是一旦我尝试只显示一个图表渲染。

我尝试了很多方法来开始,更改变量名称,逐行检查代码以查看某个图表是否存在问题。先注释掉一张图表,然后再注释掉另一张图表,以确保它们都能正常工作。最后,我检查了 Chrome 中的脚本以查看返回的内容,一切看起来都很好。

我认为问题出在脚本元素中的变量声明中。如果我将两者都保留为 chart_id,我认为第一个图表将被覆盖,但是如果我将 var 更改为 chart_id1 和 chart_id2,两个图表都不会返回吗?

我试图将“div id={{ chart1ID|safe }}”下的所有变量标记为 chart1_id、series1 等....以及“id={{ chart2ID|safe }}”下的所有变量为2. 当我检查 Chrome 中的代码时,一切看起来都正确返回,但没有图表呈现。

.../static/main.js
$(document).ready(function() {
$(chart1_id).highcharts({
chart1: chart1,
title1: title1,
xAxis1: xAxis1,
yAxis1: yAxis1,
series1: series1
})
});

$(document).ready(function() {
$(chart2_id).highcharts({
chart2: chart2,
title2: title2,
xAxis2: xAxis2,
yAxis2: yAxis2,
series2: series2
})});

HTML

..../templates/index.html
<body>

<div id={{ chart1ID|safe }} class="chart1" style="height: 500px; width: 500px">
<script>
var chart_id = {{ chart1ID|safe }}
var series = {{ series1|safe }}
var title = {{ title1|safe }}
var xAxis = {{ xAxis1|safe }}
var yAxis = {{ yAxis1|safe }}
var chart = {{ chart1|safe }}
</script>
</div>

<br>
<P>***********************************************************************************</P>

<div id={{ chart2ID|safe }} class="chart2" style="height: 500px; width: 500px">
<script>
var chart_id = {{ chart2ID|safe }}
var series = {{ series2|safe }}
var title = {{ title2|safe }}
var xAxis = {{ xAxis2|safe }}
var yAxis = {{ yAxis2|safe }}
var chart = {{ chart2|safe }}
</script>
</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="../static/main.js"></script>
</body>

python

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
@app.route('/index')
def index(chart1ID = 'chart1_ID', chart1_type = 'line', chart1_height = 500, chart2ID = 'chart2_ID', chart2_type = 'line', chart2_height = 500):
chart1 = {"renderTo": chart1ID, "type": chart1_type, "height": chart1_height,}
series1 = [{"name": 'Label1', "data": [1,2,3]}, {"name": 'Label2', "data": [9, 10, 11]}]
title1 = {"text": 'Chart 1 Title'}
xAxis1 = {"categories": ['xAxis Data1', 'xAxis Data2', 'xAxis Data3']}
yAxis1 = {"title": {"text": 'yAxis Label'}}

chart2 = {"renderTo": chart2ID, "type": chart2_type, "height": chart2_height,}
series2 = [{"name": 'Label1', "data": [1,2,3]}, {"name": 'Label2', "data": [14, 15, 20]}]
title2 = {"text": 'Second Chart Title'}
xAxis2 = {"categories": ['xAxis Data1', 'xAxis Data2', 'xAxis Data3']}
yAxis2 = {"title": {"text": 'yAxis Label'}}
return render_template('index.html',
chart1ID=chart1ID, chart1=chart1, series1=series1, title1=title1, xAxis1=xAxis1, yAxis1=yAxis1,
chart2ID=chart2ID, chart2=chart2, series2=series2, title2=title2, xAxis2=xAxis2, yAxis2=yAxis2)

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

我目前看不到树木的木材,我想这是我忽略的基本东西。这是我目前正在使用的所有代码,我从一个更大的应用程序中删除了它,以便在发布问题时更加清晰。

谢谢

最佳答案

您只需将 HTML 文档中的 var 更改为 var series1、title1 等。我的工作代码:

HTML:

<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="author" content="">
<meta charset="utf-8">
{% block head %}
<title>{% block title %}Title!{% endblock %}</title>
{% endblock %}
</head>

<body>
<div id={{ chart1ID|safe }} class="chart1" style="height: 500px; width: 1000px"></div>
<script>
var chart1_id = {{ chart1ID|safe }}
var series1 = {{ series1|safe }}
var title1 = {{ title1|safe }}
var xAxis1 = {{ xAxis1|safe }}
var yAxis1 = {{ yAxis1|safe }}
var chart1 = {{ chart1|safe }}
</script>

<div id={{ chart2ID|safe }} class="chart2" style="height: 500px; width: 1000px">
<script>
var chart2_id = {{ chart2ID|safe }}
var series2 = {{ series2|safe }}
var title2 = {{ title2|safe }}
var xAxis2 = {{ xAxis2|safe }}
var yAxis2 = {{ yAxis2|safe }}
var chart2 = {{ chart2|safe }}
</script>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="../static/js/graph.js"></script>
</body>

JavaScript:

 $(document).ready(function() {
$(chart1_id).highcharts({
chart: chart1,
title: title1,
xAxis: xAxis1,
yAxis: yAxis1,
series: series1
});
});

$(document).ready(function() {
$(chart2_id).highcharts({
chart: chart2,
title: title2,
xAxis: xAxis2,
yAxis: yAxis2,
series: series2
})});

和 Python:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
@app.route('/index')
def index(chart1ID = 'chart1_ID', chart1_type = 'line', chart1_height = 500,
chart2ID = 'chart2_ID', chart2_type = 'line', chart2_height = 500):
chart1 = {"renderTo": chart1ID, "type": chart1_type, "height": chart1_height,}
series1 = [{"name": 'Label1', "data": [1,2,3]}, {"name": 'Label2', "data": [9, 10, 11]}]
title1 = {"text": 'Chart 1 Title'}
xAxis1 = {"categories": ['xAxis Data1', 'xAxis Data2', 'xAxis Data3']}
yAxis1 = {"title": {"text": 'yAxis Label'}}


chart2 = {"renderTo": chart2ID, "type": chart2_type, "height": chart2_height,}
series2 = [{"name": 'Label1', "data": [1,2,3]}, {"name": 'Label2', "data": [14, 15, 20]}]
title2 = {"text": 'Second Chart Title'}
xAxis2 = {"categories": ['xAxis Data1', 'xAxis Data2', 'xAxis Data3']}
yAxis2 = {"title": {"text": 'yAxis Label'}}
return render_template('index.html', chart1ID=chart1ID, chart1=chart1, series1=series1, title1=title1, xAxis1=xAxis1, yAxis1=yAxis1, chart2ID=chart2ID, chart2=chart2, series2=series2, title2=title2, xAxis2=xAxis2, yAxis2=yAxis2)

if __name__ == "__main__":
app.run(debug = True, host='127.0.0.1', port=5000, passthrough_errors=True)

当您尝试复制此 Python 代码时,请记住在变量前设置等号空格的格式(例如,每个变量前有一个制表符 - [tab]chart1 = {...} [tab]series1 = {...} 等) .

关于javascript - 一页中的多个 Highcharts - 仅显示一个图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54280879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com