javascript - 根据用户点击从绘图地图更新图表 - Flask
问题描述
我正在尝试通过用户单击更新一个绘图图表(在绘图等值线图上)。后端使用 Flask 运行。我在 AJAX 和 JS 方面的知识有限,但多亏了一些好的教程,我已经能够重现一些接近我正在寻找的东西。使用 Dash 可能会更容易,但我想坚持使用 Plotly、JS 和 AJAX。
烧瓶
#Reserve page
@app.route('/reserves/', methods = ["POST","GET"])
def reserves():
reserves_map = map()
return render_template("reserves.html", reserves_map = reserves_map, graphJSON=map_filter())
#Callback when clicking on a country from the map with oil & gas reserves
@app.route('/callback', methods=['POST', 'GET'])
def cb():
return map_filter(request.args.get('data'))
#update graph with the country selected
def map_filter(country='US'):
df = sql("""select country.id_country, year, country, oilreserves_bbl, gasreserves_tcm
from country
inner join oilgas as a on country.id_country = a.id_country;""")
fig = px.line(df[df['country']==country], x="year", y="oilreserves_bbl")
graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
return graphJSON
HTML
<!-- Map Reserves-->
<div class='persomargin' style="text-align: center; width:100%; border-radius: 7px; padding: 0px 0px;">
<div class="wrap" >
<div class="one" style="text-align: left; margin-top:35px;" shadow="">
<center><h6>Oil & Gas proved reserves in the the World</h6></center>
<div id="map" class="map"></div>
<center><i style="font-size: 70%">Source: <a href="https://www.bp.com/en/global/corporate/energy-economics/statistical-review-of-world-energy/downloads.html" target="_blank">BP - Statistical Review of World Energy (2019)</a></i></center>
</div>
</div>
</div>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
var map_var = {{reserves_map | safe}};
var config = {displayModeBar: false};
Plotly.setPlotConfig(config);
Plotly.plot("map",map_var);
</script>
<!-- Function to update line plot-->
<script>
function update_graph(selection) {
$.getJSON({
url: "/callback", data: { 'data': selection }, success: function (result) {
Plotly.newPlot('chart', result, {});;
}
});
}
</script>
<div id='myDiv'><center><h6>Oil & Gas proved reserves in the the World</h6></center></div>
<!-- user input country name-->
<input type="text" id="fname" name="fname" onchange="update_graph(this.value)">
<!-- Line plot html-->
<div id="chart" class="chart"></div>
<!-- Line plot -->
<script type="text/javascript">
d = {{ graphJSON | safe }};
var config = {displayModeBar: false};
Plotly.setPlotConfig(config);
Plotly.newPlot('chart', d, {});
</script>
如您所见,折线图将根据用户输入进行更新。我一直在尝试解决此代码以使其适应我的用例,但没有成功。我不知道如何获取用户点击值并将其传递回烧瓶。
谢谢!
解决方案
您可以尝试使用情节点击event
处理程序来调用update_graph
<script type="text/javascript">
var map_var = {{reserves_map | safe}};
var config = {displayModeBar: false};
var map = document.getElementById('map'),
Plotly.setPlotConfig(config);
Plotly.plot("map",map_var);
map.on('plotly_click', function(data){
update_graph(data.points[0].location);
});
</script>
推荐阅读
- python - 在 Python 中并行运行一个函数
- css - 手机上出现奇怪的灰色渐变背景
- python - 如何使用Django rest框架查询参数
- node.js - 快递,路由器,寄售是否有任何变化,或者我只是不知道我做错了什么?
- python-3.x - 在 python 3 中使用弹出警报身份验证抓取网页
- c# - 字段名称为空时如何使用 toList 反序列化数据表?
- python - 我想为机器人添加冷却时间。我希望他们选择冷却时间。蟒蛇不和谐
- c# - 如何判断在调试中的 TransactionScope 范围内捕获了哪些事务或 SQL 查询或服务调用?
- android - unpack (KOI8-R) 8 位字符编码,二进制,提示文件
- javascript - 当我打开时,汉堡菜单在 div 下?