django - 图表未在 django-nvd3 中呈现

标签 django d3.js nvd3.js

我正在尝试在我的 django 项目中使用 django-nvd3,并尝试使折线图示例正常工作。当我的页面加载时,我在 chrome js 控制台中收到以下错误:

Uncaught TypeError: Cannot set property 'monthEnd' of undefined nv.d3.js:115 (anonymous function) nv.d3.js:115 (anonymous function) nv.d3.js:14369 Uncaught TypeError: Object # has no method 'lineChart' (index):13 (anonymous function) (index):13 (anonymous function) nv.d3.js:63

我基本上是从这里复制了代码: http://django-nvd3.readthedocs.org/en/latest/classes-doc/line-chart.html

我的观点是:

from django.shortcuts import render_to_response, render
import random
import datetime
import time

def demo_linechart(request):
    """
    lineChart page
    """
    start_time = int(time.mktime(datetime.datetime(2012, 6, 1).timetuple()) * 1000)
    nb_element = 100
    xdata = range(nb_element)
    xdata = map(lambda x: start_time + x * 1000000000, xdata)
    ydata = [i + random.randint(1, 10) for i in range(nb_element)]
    ydata2 = map(lambda x: x * 2, ydata)

    tooltip_date = "%d %b %Y %H:%M:%S %p"
    extra_serie = {"tooltip": {"y_start": "", "y_end": " cal"},
                   "date_format": tooltip_date}
    chartdata = {'x': xdata,
                 'name1': 'series 1', 'y1': ydata, 'extra1': extra_serie,
                 'name2': 'series 2', 'y2': ydata2, 'extra2': extra_serie}
    charttype = "lineChart"

    kw_extra = {
        'x_is_date': True,
        'x_axis_format':"%d %b %Y",
    }

    data = {
        'charttype': charttype,
        'chartdata': chartdata,
        'kw_extra':kw_extra,
    }
    return render(request, 'smcore/nvd3.html', data)

我的html是:

{% load static %}

<link rel="stylesheet" type="text/css" href="{% static 'nvd3/nv.d3.css' %}" />
<script type="text/javascript" src='{% static 'smcore/jquery-latest.js' %}'></script>
<script type="text/javascript" src='{% static 'smcore/d3.min.js' %}'></script>
<script type="text/javascript" src='{% static 'nvd3/nv.d3.js' %}'></script>

{% load nvd3_tags %}
<head>
    {% load_chart charttype chartdata "linechart_container" kw_extra %}

    {#    {% load_chart charttype chartdata "linechart_container" True '%d %b %Y %H' %}#}
</head>
<body>
    <h1>Fruits vs Calories</h1>
    {#    {% include_container "piechart_container" 400 500 %}#}
    {% include_container "linechart_container" 400 600 %}

</body>

我的网址是:

url(r'testvis/$', temp.demo_linechart, name='testvis'),

我正在使用 nv.version = '1.1.15b'; d3 = {版本:“2.7.0”};//语义版本

请帮忙!!

最佳答案

对我来说,解决方案是来自 samesense 的第一条评论:

https://github.com/areski/django-nvd3/issues/24

即:

data = {
    'charttype': charttype,
    'chartdata': chartdata,
    'extra': {'x_is_date': True,
    'x_axis_format': "%d %b %Y %H"},
  }
Then you fix the template to make it look like the piechart example.
Change
{% load_chart charttype chartdata "linechart_container" True "%d %b %Y %H" %}
to
{% load_chart charttype chartdata "linechart_container" extra %}

关于django - 图表未在 django-nvd3 中呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22159622/

相关文章:

python - 如何在 Django 中使用具有相同属性的另一个模型创建对象

javascript - d3 - 了解 d3.svg.line 的文档

javascript - 旋转的 X 轴标签未显示

javascript - d3.js nvd3 x 轴上的日期 : only some dates are show

javascript - 在弧形内弯曲折线图

python - 如何在 Python/Django 中用逗号作为千位分隔符和小数位打印数字

Django 休息框架 : How to download image with this image send directly in the body

python - ReportLab:大字体的文本挤在段落中

d3.js - d3 无法创建引导卡

JavaScript 将值添加到对象中的对象