Django -无法得到highchart来显示数据。

时间:2022-09-22 15:16:53

I'm trying to display a chart with help of Highchart by following this solution:

我正试图通过以下的解决方案来显示一个图表,帮助图表:

Passing Django Database Queryset to Highcharts via JSON

通过JSON向Highcharts传递Django数据库。

But I can't get the data to appear:

但是我不能让数据显示出来:

Django -无法得到highchart来显示数据。

Still new to this and appreciate your help, folks!

这还不新鲜,感谢你们的帮助,伙计们!


views.py

views.py

class ChartData(object):
    def check_valve_data():
        data = {'member_no': []}

        people = Member.objects.all()

        for unit in people:
             data['member_no'].append(unit.member_no)

        return data


 def chartViewHigh(request, chartID='chart_ID', chart_type='column', chart_height=500):
     data = ChartData.check_valve_data()

     chart = {"renderTo": chartID, "type": chart_type, "height": chart_height, }
     title = {"text": 'Check Member Data'}
     xAxis = {"title": {"text": 'Member'}, "categories": data['member_no']}
     yAxis = {"title": {"text": 'Data'}}

     return render(request, 'chart/chartViewHigh.html', {'chartID': chartID, 'chart': chart,
                                                    'title': title, 'xAxis': xAxis, 'yAxis': yAxis})

chartViewHigh.html

chartViewHigh.html

{% extends 'base.html' %}

{% load staticfiles i18n %}

{% block head %}
  <link href="{% static 'css/chart.css' %}" rel="stylesheet">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
      <script src="https://code.highcharts.com/highcharts.js"></script>
      <script src="https://code.highcharts.com/modules/exporting.js"></script>

 {% endblock head %}

 {% block main %}

<h1 align="center">Analysis</h1>

{% block content %}
   <div id={{ chartID|safe }} class="chart" style="height:100px; width:100%"></div>
{% endblock %}


{% block extrajs %}
<script>
   var chart_id = {{ chartID|safe }};
   var chart = {{ chart|safe }};
   var title = {{ title|safe }};
   var xAxis = {{ xAxis|safe }};
   var yAxis = {{ yAxis|safe }};
</script>


<script>
$(document).ready(function() {
   $(chart_id).highcharts({
       chart: chart,
       title: title,
       xAxis: xAxis,
       yAxis: yAxis,
   });
});
</script>
{% endblock %}

{% endblock main %}

urls.py

urls . py

urlpatterns = patterns[
    url(r'^chartViewHigh/$', views.chartViewHigh, name='chartViewHigh'),
]

1 个解决方案

#1


1  

A few issues:

几个问题:

You need quote marks around the chart ID template variable to make it an HTML attribute:

您需要在图表ID模板变量周围加上引号,使其成为HTML属性:

<div id="{{ chartID|safe }}" ...

You're not passing in a valid JQuery selector: to select the above div you should use $("#chart_ID") (see JQuery selectors), so with your Django template variable for example:

您没有传递一个有效的JQuery选择器:要选择上面的div,您应该使用$(“#chart_ID”)(请参阅JQuery选择器),因此对于Django模板变量,例如:

$("#{{ chartID|safe }}")

Also the data appears to need a series key to render (I haven't used highcharts much but see here - your chart renders when this is added):

此外,数据似乎还需要一个系列键来渲染(我并没有过多地使用highcharts,但是请参见这里——添加此键时的图表渲染):

https://www.highcharts.com/docs/getting-started/your-first-chart

https://www.highcharts.com/docs/getting-started/your-first-chart

Also the ChartData class doesn't belong in your views.py file - only HTTP request/responses belong there. I recommend working through the official Django tutorial if you haven't already to get an idea of the "Django way" to do things. For example, your ChartData method produces a list of member_nos, but you can do this with a single line of code :)

另外,ChartData类不属于视图。py文件—只包含HTTP请求/响应。如果您还没有了解“Django方法”,我建议您阅读官方的Django教程。例如,您的ChartData方法会生成一个member_nos列表,但是您可以使用一行代码来完成这个任务:)

Member.objects.values_list('member_no', flat=True)

#1


1  

A few issues:

几个问题:

You need quote marks around the chart ID template variable to make it an HTML attribute:

您需要在图表ID模板变量周围加上引号,使其成为HTML属性:

<div id="{{ chartID|safe }}" ...

You're not passing in a valid JQuery selector: to select the above div you should use $("#chart_ID") (see JQuery selectors), so with your Django template variable for example:

您没有传递一个有效的JQuery选择器:要选择上面的div,您应该使用$(“#chart_ID”)(请参阅JQuery选择器),因此对于Django模板变量,例如:

$("#{{ chartID|safe }}")

Also the data appears to need a series key to render (I haven't used highcharts much but see here - your chart renders when this is added):

此外,数据似乎还需要一个系列键来渲染(我并没有过多地使用highcharts,但是请参见这里——添加此键时的图表渲染):

https://www.highcharts.com/docs/getting-started/your-first-chart

https://www.highcharts.com/docs/getting-started/your-first-chart

Also the ChartData class doesn't belong in your views.py file - only HTTP request/responses belong there. I recommend working through the official Django tutorial if you haven't already to get an idea of the "Django way" to do things. For example, your ChartData method produces a list of member_nos, but you can do this with a single line of code :)

另外,ChartData类不属于视图。py文件—只包含HTTP请求/响应。如果您还没有了解“Django方法”,我建议您阅读官方的Django教程。例如,您的ChartData方法会生成一个member_nos列表,但是您可以使用一行代码来完成这个任务:)

Member.objects.values_list('member_no', flat=True)