繁体   English   中英

django模型的数据未传递到highcharts图表

[英]Data from django models not passing to highcharts chart

我一直在试图采用这种做法在这里 ,试图通过从Django模型数据到highcharts图。 但是,即使我的数据库中确实存在适用的数据,它似乎也没有传递任何数据。 以下是相关代码:

views.py:

def master(request):

    class ChartData(object):    
        def check_usage_data():
            data = {'usage': []}

            sources = RealTimeData.objects.all()

            for source in sources:
                data['usage'].append(source.usage)

            return data

    if request.user.is_authenticated():

        data = ChartData.check_usage_data()

        series = [
            {"data": data['usage']}
        ]

        return render(request, 'Properties/master.html', {'series': series})

    else:

        # If the usre isn't authenticated, the user is redirected to the Sign-n Page

        return render(request, 'SignIn/SignInPage.html')

master.html:

<!-- Chart builders -->
<script type="text/javascript">

    var RealTimeSeries = {{ series|safe }}

    // hours setting //
    hoursX = ['1:00', '2:00', '3:00', '4:00', '5:00', '6:00', '7:00', '8:00', '9:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00', '24:00']

    // categories/dates setting //
    oneYearX = ['Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'Jan', 'Feb', 'Mar'],
    sixMonthX = ['Oct', 'Nov', 'Dec', 'Jan', 'Feb', 'Mar']
    threeMonthX = ['Jan', 'Feb', 'Mar']

    // usage data sets //
    usageoneYearY = [9164950.4, 8154238.9, 3387452.8, 8849677.6, 9957030.6, 10740101.9, 9196562.5, 10532428.8, 10017343.7, 10190627.7, 9454554.3, 7832326.9]
    usagesixMonthY = [9196562.5, 10532428.8, 10017343.7, 10190627.7, 9454554.3, 7832326.9]
    usagethreeMonthY = [10190627.7, 9454554.3, 7832326.9]

    // rates data sets //
    oldRateoneYearY = [6.87, 10.62, 11.95, 10.65, 11.5, 12.74, 10.21, 11.16, 8.39, 8.78, 9.84, 9.81]
    oldRatesixMonthY = [10.21, 11.16, 8.39, 8.78, 9.84, 9.81]
    oldRatethreeMonthY = [8.39, 8.78, 9.84, 9.81]

    newRateoneYearY = [6.7087804, 8.452008, 6.2561854, 8.7319573, 6.2172567, 7.5617114, 10.1019943, 8.7198434, 8.1792204, 7.8935053, 6.1292755, 7.2133497]
    newRatesixMonthY = [10.1019943, 8.7198434, 8.1792204, 7.8935053, 6.1292755, 7.2133497]
    newRatethreeMonthY = [7.8935053, 6.1292755, 7.2133497]

    // costs data sets //
    costoneYearY = [614856.3935, 689196.9252, 211925.3278, 772750.0735, 619054.1545, 812135.5149, 929036.219, 918411.3026, 819340.6198, 804397.7345, 579495.6824, 564973.1276]
    costsixMonthY = [929036.219, 918411.3026, 819340.6198, 804397.7345, 579495.6824, 564973.1276]
    costthreeMonthY = [804397.7345, 579495.6824, 564973.1276]

    // savings data sets //
    savingsoneYearY = [14775.69899, 176783.2459, 192875.2818, 169740.5909, 526004.3645, 556153.4672, 9932.812293, 257007.7515, 21114.51661, 90339.37761, 350832.4608, 203378.1413]
    savingssixMonthY = [9932.812293, 257007.7515, 21114.51661, 90339.37761, 350832.4608, 203378.1413]
    savingsthreeMonthY = [90339.37761, 350832.4608, 203378.1413]

    $(document).ready(function(){
        // Construct RealTime chart //
        var usageChart = Highcharts.chart('realTime-chart', {
            chart: {
                type: 'column',
                style: {
                    fontFamily: 'Gotham-Book'
                }
            },
            title: {
                text: ''
            },
            xAxis: {
                categories: hoursX
            },
            yAxis: {
                min: 0,
                title: {
                    text: 'Usage (kWh)'
                }
            },
            plotOptions: {
                column: {
                    pointPadding: 0,
                    borderWidth: 0
                }
            },
            series: [{
                name: 'Master',
                data: RealTimeSeries,
                color: 'rgb(33,93,125)',
            }],
            credits: {
                enabled: false,
            }
        });
    });
</script>

现在,该页面将无法加载,因为我收到“必须以ChartData实例作为第一个参数来调用未绑定方法check_usage_data()(什么也不做)”错误。 但是,我以前玩过ChartData(object)类定义的放置,并且当我要加载页面时,会显示图表的布局,但上面没有任何数据(轴标题一切都会在那里)。 有人知道我遵循链接方法的问题吗?

这里的问题可能是Highcharts无法理解您传递的数据的结构。

不过,首先,您需要使用check_usage_data()修复问题。 似乎没有理由为什么该方法必须位于其自己的类中,因此我将其移至views.py模块中的顶级函数中。

def check_usage_data():
    data = {'usage': []}
    sources = RealTimeData.objects.all()

    for source in sources:
        data['usage'].append(source.usage)

    return data

然后从视图中调用它:

def master(request):

   if request.user.is_authenticated():
       data = check_usage_data()

假设您的数据是浮点数列表,则应按以下方式将其传递给模板:

return render(request, 'Properties/master.html', {'series': data['usage']})

因此,在完成所有这些操作之后,您的views.py将如下所示:

def master(request):

    if request.user.is_authenticated():
        data = check_usage_data()
        return render(request, 'Properties/master.html', {'series': data['usage']})

    return render(request, 'SignIn/SignInPage.html')

def check_usage_data():
    data = {'usage': []}
    sources = RealTimeData.objects.all()

    for source in sources:
        data['usage'].append(source.usage)

    return data  

在将数据列表输出到模板的位置添加分号可能也是值得的:

var RealTimeSeries = {{ series|safe }};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM