2015-05-26 25 views
0

我使用Flask构建D3可视化,并使用JQuery和D3从窗体请求用户数据。呈现新图形数据添加新图形而不是替换现有图形

$('form').submit(function(e) { 
    e.preventDefault(); 
    d3.json("{{ url_for('crew') }}") 
     .header('Content-Type', 'application/x-www-form-urlencoded') 
     .post($(this).serialize(), function(error, graph) { 

当用户选择数据并按提交时,会显示一个图形。问题是当再次按下提交时,另一个图显示在旧的图上。在重新提交表单之前,我需要刷新。

@app.route('/crew', methods=['GET', 'POST']) 
def crew(): 
    form = CrewForm() 

    if form.validate_on_submit(): 
     return current_app.send_static_file(os.path.join('crews/', form.filename.data)) 

    return render_template('Crew.html', form=form) 

回答

2

在你json回调方法清除使用或者jQueryd3方法以前的图表。看看这个链接,它可能会帮助你。 NVD3, Clear svg before loading new chart

+0

这工作,谢谢!我添加了'd3.selectAll(“svg> *)。remove();'并在添加新图之前清除该图:) – gbhrea