且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Django,AJAX和JSON根据用户输入更新图表

更新时间:2023-01-11 18:06:01

一个月后,我知道了这一点,并将回答我自己的问题.将问题编辑为工作脚本.

Month later, I figured it out and will answer my own question. Edited the question to be the working script.

代替尝试使用d3.json和访问URL,我可以选择直接在AJAX成功中调用d3图的功能:

Instead of trying to use d3.json and accessing a URL I could alternatively use call the function of the d3 graph directly within my AJAX success:

$.ajax({
    url: url,
    type: "POST",
    dataType: 'json',
    data: { selected_variable : selected_var },

    success: function(data){
        console.log(data);
        var post_data = data;
        drawLineChart(post_data);
    })}

然后,我不再使用原本想使用的d3.json,而是直接使用数据来创建图形:

Then instead of using d3.json as I orginally wanted to use, I use the data directly to create the graph:

function drawLineChart(post_data){

    post_data.forEach(function (d) {
        d.timeindex = +d.timeindex;
        d.value = +d.value;

    //ETC
    )}
}

结果

如果有人发现任何可能更有效或更有效的方法,请告诉我.

If anyone sees anything that could be more efficient or better, please let me know.