areski / django-nvd3

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

How to display multiple charts on the same view? #47

Open njuki opened 9 years ago

njuki commented 9 years ago

Please help.

Kind regards

PresidentNick commented 8 years ago

I need help with this too @areski . Is the issue that it assigns the same ID to each chart so the second one overwrites the first one?

alex-pardo commented 8 years ago

Same here @areski !

Etiennepi commented 7 years ago

To achieve this, you can simply define your data dict (the one you give to render_to_respone)

like this:

data = {
        'charttype1': charttypea,
        'chartdata1': chartdataa,
        'chartcontainer1': chartcontainera,
        'extra1': {
            'x_is_date': False,
            'x_axis_format': '',
            'tag_script_js': True,
            'jquery_on_ready': False,
        },
        'charttype2': charttype,
        'chartdata2': chartdata,
        'chartcontainer2': chartcontainer,
        'extra2': {
            'x_is_date': True,
            'x_axis_format': '%d %b %Y %H',
            'tag_script_js': True,
            'jquery_on_ready': False,
        }
    }

And then, in your template:

{% load nvd3_tags %}
<head>
    {# Jquery CDN : Needed when using jquery_on_ready=True #}
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    {% include_chart_jscss %}
    {% load_chart charttype1 chartdata1 chartcontainer1 extra1 %}
    {% load_chart charttype2 chartdata2 chartcontainer2 extra2 %}
</head>
<body>
    {% include_container chartcontainer1 %}
    <br>
    {% include_container chartcontainer2 %}
</body>
cultivater commented 4 years ago

To achieve this, you can simply define your data dict (the one you give to render_to_respone)

like this:

data = {
        'charttype1': charttypea,
        'chartdata1': chartdataa,
        'chartcontainer1': chartcontainera,
        'extra1': {
            'x_is_date': False,
            'x_axis_format': '',
            'tag_script_js': True,
            'jquery_on_ready': False,
        },
        'charttype2': charttype,
        'chartdata2': chartdata,
        'chartcontainer2': chartcontainer,
        'extra2': {
            'x_is_date': True,
            'x_axis_format': '%d %b %Y %H',
            'tag_script_js': True,
            'jquery_on_ready': False,
        }
    }

And then, in your template:

{% load nvd3_tags %}
<head>
    {# Jquery CDN : Needed when using jquery_on_ready=True #}
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    {% include_chart_jscss %}
    {% load_chart charttype1 chartdata1 chartcontainer1 extra1 %}
    {% load_chart charttype2 chartdata2 chartcontainer2 extra2 %}
</head>
<body>
    {% include_container chartcontainer1 %}
    <br>
    {% include_container chartcontainer2 %}
</body>

Hey @Etiennepi , I used your solution for pie chart, but it doesn't work.

pcmaniac23 commented 4 years ago

I got it working. Try the following code:

In views.py:

    # ... code above omitted....
    chartcontainer1 = 'piechart_container1'  # container name
    chartcontainer2 = 'piechart_container2'  # container name

    data = {
        'charttype1': charttype,
        'chartdata1': chartdata,
        'chartcontainer1': chartcontainer1,
        'extra1': {
            'x_is_date': False,
            'x_axis_format': '',
            'tag_script_js': True,
            'jquery_on_ready': False,
        },
        'charttype2': charttype,
        'chartdata2': chartdata,
        'chartcontainer2': chartcontainer2,
        'extra2': {
            'x_is_date': False,
            'x_axis_format': '',
            'tag_script_js': True,
            'jquery_on_ready': False,
        }
    }

In template (such as piechart.html):

{% load nvd3_tags %}
<head>
    {% include_chart_jscss %}
    {% load_chart charttype1 chartdata1 chartcontainer1 extra1 %}
    {% load_chart charttype2 chartdata2 chartcontainer2 extra2 %}
</head>
<body>
        <h1>Fruits vs Calories</h1>
        {% include_container chartcontainer1 %}
        <br>
        {% include_container chartcontainer2 %}
</body>

Here's a sample screenshot (two pie-charts based on the same dataset): image