areski / django-nvd3

Django wrapper for nvd3 - It's time for beautiful charts
Other
416 stars 124 forks source link

'map' object is not subscriptable #22

Open sergeyvladimirovichmelnichenko opened 10 years ago

sergeyvladimirovichmelnichenko commented 10 years ago

Please - help to understand :

Request Method: GET Request URL: http://189.189.2.499:8080/ITN/chart_test1/ Django Version: 1.5.1 Exception Type: TypeError Exception Value:

'map' object is not subscriptable

Exception Location: C:\Python33\lib\site-packages\nvd3\NVD3Chart.py in , line 189 Python Executable: C:\Program Files (x86)\Apache Software Foundation\Apache2.2\bin\httpd.exe Python Version: 3.3.2

def demo_linechart(request): """ lineChart page """ start_time = int(time.mktime(datetime.datetime(2012, 6, 1).timetuple()) * 1000) nb_element = 150 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_serie1 = {
    "tooltip": {"y_start": "", "y_end": " cal"},
    "date_format": tooltip_date,
    'color': '#a4c639'
}
extra_serie2 = {
    "tooltip": {"y_start": "", "y_end": " cal"},
    "date_format": tooltip_date,
    'color': '#FF8aF8'
}
chartdata = {'x': xdata,
             'name1': 'series 1', 'y1': ydata, 'extra1': extra_serie1,
             'name2': 'series 2', 'y2': ydata2, 'extra2': extra_serie2}

charttype = "lineChart"
chartcontainer = 'linechart_container'  # container name
data = {
    'charttype': charttype,
    'chartdata': chartdata,
    'chartcontainer': chartcontainer,
    'extra': {
        'x_is_date': True,
        'x_axis_format': '%d %b %Y %H',
        'tag_script_js': True,
        'jquery_on_ready': False,
    }
}
return render_to_response('linechart.html', data)

{% load nvd3_tags %}

{# Jquery CDN : Needed when using jquery_on_ready=True #} {% include_chart_jscss %} {% load_chart charttype chartdata chartcontainer extra %} {% include_container chartcontainer %}

https://github.com/areski/django-nvd3/tree/master/demoproject/demoproject

I made everything - as is specified in the demontsratsionny project, but I receive the following error:

'map' object is not subscriptable

in what there can be a reason?

Renkai commented 10 years ago

Try to change xdata = map(lambda x: start_time + x * 1000000000, xdata) to xdata = list(map(lambda x: start_time + x * 1000000000, xdata)) and so on.

toasty commented 9 years ago

This confused me too for a bit when trying to get a demo line chart to work. Writing here in case it helps someone else who's equally new to all this.

The error I was getting from Django was the same 'map' object is not subscriptable referring to line 220 in my venv's lib/python3.4/site-packages/django_nvd3/templatetags/nvd3_tags.py but that was the symptom not the cause. As Renkai points out above the issue is a python2/3 compatibilty problem in the demo code (not in the nvd3 package), in my case xdata = map(lambda x: start_time + x * 1000000000, xdata) in the demo_linechart function.

Changing it and the similar line beneath to xdata = list(map(lambda x: start_time + x * 1000000000, xdata)) fixed it for me.

GraphGrailAi commented 9 years ago

The "list(" should be added in several such places. Ando more: to get nvd3 working on Python 3.4.2 i changed nvd3_tags.py : unicode to str in 2 places. Is it possible to make relevant versions to download via pip for Python 2.7 and Python 3 ?