m0_74879466 2024-03-24 22:29 采纳率: 73.4%
浏览 0

django不用orm前端搜索后echarts显示房价条形图怎么做

django不用orm前端搜索后echarts显示房价条形图怎么做

  • 写回答

3条回答 默认 最新

  • 码农阿豪@新空间 新星创作者: 前端开发技术领域 2024-03-24 22:30
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    问题描述:如何在Django中不使用ORM,通过前端搜索后,使用echarts显示房价条形图。 回答: 1、首先需要在前端实现搜索功能,可以通过ajax获取到用户输入的搜索内容,然后将搜索内容传递到后端。 案例代码: HTML页面:
    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
        <script src="https://unpkg.com/vue/dist/vue.js"></script>
        <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
        <script src="https://unpkg.com/element-ui/lib/index.js"></script>
        <script src="https://cdn.staticfile.org/qs/6.5.2/qs.min.js"></script>
        <style type="text/css">
            html,body{
                height: 100%;
            }
            .el-container{
                height: 100%;
                display: flex;
                flex-direction: column;
            }
            .el-header{
                background-color: #9FB1C8;
                color: #333;
                font-size: 24px;
                line-height: 60px;
                padding: 0 20px;
            }
            .el-main{
                display: flex;
                flex-direction: row;
                flex-grow: 1;
            }
            .el-aside{
                background-color: #F8F8F8;
                width: 200px;
                padding: 20px;
            }
            .el-content{
                padding: 20px;
                flex-grow: 1;
            }
            .el-footer{
                line-height: 60px;
                text-align: center;
                background-color: #333;
                color: #fff;
            }
        </style>
    </head>
    <body>
    <div id="app" class="el-container">
        <div class="el-header">房价查询</div>
        <div class="el-main">
            <div class="el-aside">
                <el-input v-model="searchText" placeholder="请输入小区名称"></el-input>
                <el-button type="primary" @click="search">查询</el-button>
            </div>
            <div class="el-content">
                <div id="chart" style="width: 100%; height: 500px;"></div>
            </div>
        </div>
        <div class="el-footer">© 2021 AI智能助手</div>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/echarts/4.9.0/echarts.min.js"></script>
    <script>
        new Vue({
            el: '#app',
            data: {
                searchText: '',
                chartData: [],
                chart: null
            },
            methods: {
                search: function() {
                    let that = this
                    axios.post('/search', {
                        searchText: that.searchText
                    })
                    .then(function(response) {
                        that.chartData = response.data
                        that.renderChart()
                    })
                    .catch(function(error) {
                        console.log(error)
                    });
                },
                renderChart: function() {
                    let that = this
                    let chart = echarts.init(document.getElementById('chart'));
                    let option = {
                        title: {
                            text: '房价查询结果'
                        },
                        tooltip: {},
                        legend: {
                            data:['平均房价']
                        },
                        xAxis: {
                            data: that.chartData.map(item => item.name)
                        },
                        yAxis: {},
                        series: [{
                            name: '平均房价',
                            type: 'bar',
                            data: that.chartData.map(item => item.price)
                        }]
                    };
                    chart.setOption(option)
                    that.chart = chart
                }
            },
            mounted: function () {
            }
        });
    </script>
    </body>
    </html>
    

    2、在后端实现搜索功能,根据用户输入的搜索内容,在数据库中查询相应的数据。 案例代码: views.py文件:

    from django.shortcuts import render
    from django.db import connection
    from django.http import JsonResponse
    def search(request):
        cursor = connection.cursor()
        searchText = request.POST.get('searchText', '')
        cursor.execute("select name, avg_price from house_prices where name like '%%%s%%'" % searchText)
        data = cursor.fetchall()
        res = []
        for item in data:
            res.append({
                'name': item[0],
                'price': item[1] if item[1] is not None else 0
            })
        cursor.close()
        return JsonResponse(res, safe=False)
    

    上述代码中,使用了Django自带的数据库连接对象connection,使用cursor.execute方法执行SQL语句查询数据,然后将查询结果封装成一个列表对象,最后通过JsonResponse返回给前端。 3、在前端使用echarts显示房价条形图,根据查询结果生成相应的图表。 案例代码: HTML页面中的渲染图表函数renderChart中展示了如何使用echarts生成柱状图的代码。在这里不再赘述。 最终,用户输入小区名称后,点击查询按钮,前端通过ajax将搜索内容传递到后端,后端根据搜索内容在数据库中查询相应的数据,将查询结果封装成一个列表对象返回给前端,并在前端使用echarts生成相应的柱状图,展示给用户。

    评论

报告相同问题?

问题事件

  • 创建了问题 3月24日