django模板中ajax接受views返回的pyechart图片,如何在页面的指定位置显示出来呢?

django模板中ajax接受views返回的pyechart图片,如何在页面的指定位置显示出来呢?
console.log(arg) 可以看到有内容如下,但是在页面显示的时候空白。
{"html_pie": "\n\n\n <meta charset="UTF-8">\n .................}

模板代码如下

       展示整体<input id="rallzj" name="ra1" type="radio" onclick="changepic('rall')">
        展示地市<input id="rcityzj" name="ra1" type="radio" onclick="changepic('rcity')">
        <div id="all">
            <br/><br/>
            {% autoescape off %}
                {{html_pie}}
            {% endautoescape %}
        </div>


js代码如下

function changepic(id){
                $.ajax({
                    url:'/baobiao/changepic',
                    type:'get',
                    data:{'id':id},
                    success:function (arg) {
                        console.log(arg)
                        document.getElementById("all").innerHTML=arg["html_pie"]
                    }
                })
            }

views代码如下,只包含一部分

    c = (
        Pie()
        .add("", list(zip(list11, list3)))
        .set_global_opts(title_opts=opts.TitleOpts(title="Pie-基本示例"),
                         )
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    )
    html_pie = c.render_embed()
    context["html_pie"] = html_pie
不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这篇博客: django项目中pyecharts详细使用教程1中的 3. django项目中使用图表如何渲染 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    (1) pip install pyecharts

    (2)将pyecharts.render.templates中的所有模板文件复制到我们的项目templates目录下方来:

    (3)代码如下

    from jinja2 import Environment, FileSystemLoader
    from pyecharts.globals import CurrentConfig
    from django.conf import settings
    
    CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader("{}/templates".format(settings.BASE_DIR)))
    
    from django.http import HttpResponse
    from pyecharts import options as opts
    from pyecharts.charts import Bar
    from pyecharts.globals import ThemeType
    
    
    def index(request):
        c = (
            # 设置主题的样式
            Bar(init_opts=opts.InitOpts(theme=ThemeType.MACARONS))
            .add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
            .add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
            .add_yaxis("商家B", [15, 25, 16, 55, 48, 8])
            # 增加主题和副标题
            .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
        )
        return HttpResponse(c.render_embed())
    

    (4)效果如图:

     (5)此时你会发现,页面中只有一个单纯的图表,可不可以增加我自己的一些内容呢  当然可以,打开刚才复制的模板中的:simple_chart.html,

    往此Html里面增加自己的内容即可。(<h1>这是一个pyecharts制作的图表</h1>)

  • 以下回答来自chatgpt:

    由于我没有见过您的Pyechart和模板代码,因此我无法给出特别具体的答案。但是,我可以提供下面一些步骤,希望能够帮助您完成所需工作。

    1. 在模板中准备接收图像的标签

    您需要在HTML模板中为要接收图像的位置添加一个标记,以便您可以将图像插入到该位置。例如:

    <div id="my-chart"></div>
    

    请注意,此标记的关键是用一个唯一的ID值来标识它。

    2. 使用Ajax从Views中获取图像

    您需要在模板中使用Ajax来获取返回的Pyechart图像。在您的Javascript代码中,您可以使用jQuery和Ajax来完成此操作。以下是一个示例:

    $.ajax({
        url: "/path/to/your/view/",
        success: function(data) {
            $('#my-chart').html(data);
        }
    });
    

    这里的"/path/to/your/view/"应该是指向您的视图函数的URL。当Ajax请求成功时,它将在要接收的位置(在本例中为"my-chart")中插入图像数据。

    3. 在Views中生成Pyechart图像

    您需要在您的视图函数中使用Pyechart来生成图像,并将其以适当的方式传递给模板。以下是一个示例:

    from pyecharts import options as opts
    from pyecharts.charts import Bar
    from django.http import HttpResponse
    
    def my_view(request):
        # 通过Pyechart生成图像
        chart = Bar()
        chart.add_xaxis(['Apple', 'Banana', 'Orange'])
        chart.add_yaxis('Sales', [3, 2, 4])
        chart.set_global_opts(title_opts=opts.TitleOpts(title="Fruit Sales"))
    
        # 将生成的图像转换为HTML
        chart_html = chart.render_embed()
    
        # 返回HTML作为HTTP响应
        return HttpResponse(chart_html)
    

    在本例中,我们使用Pyechart生成一个柱状图,并将其存储在变量"chart_html"中。然后将HTML返回给Ajax函数,它将把图像插入到页面中。

    4. 确保浏览器正确显示图像

    最后,请确保您在浏览器中正确显示生成的图像。由于有关浏览器支持的各种问题,这有时可能需要对HTML进行微小的更改。

    如果在将Pyechart图像嵌入HTML中时遇到问题,请尝试使用render()、render_to_file()或render_to_png()等方法来代替render_embed()。这些方法将生成独立的PNG文件,您可以在HTML中将其嵌入。

    希望这些步骤能帮助您成功地在Django模板中使用Ajax接收Views返回的Pyechart图像!


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^