質問

I am trying to use a morris chart in django. morris chart requires a data in format

$(Morris.Bar({
  element: 'mchart',
  data: [
    { y: '2006', a: 120 },
    { y: '2007', a: 75 },
    { y: '2008', a: 50 },
    { y: '2009', a: 75 },
    { y: '2010', a: 50 },
    { y: '2011', a: 95 },
    { y: '2012', a: 100 }
  ],
  xkey: 'y',
  ykeys: ['a' ],
  labels: ['SMS count']
}));

I have a model named log as class Log(models.Model):

   date= models.DateField()
   count=models.CharField(max_length=100)

and i used class view to access the data as

views

class newChartView(TemplateView):
    template_name = "new_report_view.html"

    def get_context_data(self, **kwargs):
        context = super(newChartView, self).get_context_data(**kwargs)
        context['count'] = Log.objects.all()
        return context

now how to arrange the data in to as required by morris.

役に立ちましたか?

解決

Your Django template would look like this:

data: [
{% for item in count %}
    { y: '{{ item.date|date:"Y" }}', a: '{{ item.count }}' }{% if not forloop.last %},{% endif %}
{% endfor %}
  ],

This is part of the JavaScript that produces the Morris chart. The above produces a line with y and a values from your items (year is formatted to have four digits and count is taken from the item directly).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top