chartit / django-chartit

A Django app to plot charts and pivot charts directly from the models. Uses HighCharts and jQuery JavaScript libraries to render the charts on the webpage.
http://django-chartit.mrsenko.com/
Other
492 stars 164 forks source link

Multiple charts #9

Closed alabarga closed 10 years ago

alabarga commented 11 years ago

Hi,

as I cannot find a mailing list or support email, how can you get multiple charts in different containers? I mean, how can you use the local_charts filter for multiple charts? is it possible at all?

{{ linechart|load_charts:"container"}}

using two calls

{{ linechart|load_charts:"container1"}} {{ barchart|load_charts:"container2"}}

did not work

regards,

Albertos

atodorov commented 11 years ago

+1 on this.

atodorov commented 11 years ago

Hi @alabarga, I think this is not a bug in the sence that loading multiple charts is possible. We're just not doing it right.

I read through the code and figured it out. See the comments in templatetags/chartit.py.

First argument to load_charts is a Chart list(or object), the second is comma separated list of HTML IDs where the charts need to be rendered.

Here's my code, which works:

 return render(request, 'index.html',
                 {
                    'charts' : [chart_1, chart_2],
                 }
             )

{{ charts|load_charts:"chart_1,chart_2" }}

<div id="chart_1">Chart will be rendered here</div>
<div id="chart_2">Chart will be rendered here</div>

I also think I know the reason this is designed in such a way. Looking at the result it is two script tags - one to load the HighCharts javascript file and the second to define an array of all chart objects to display in the page.

This said, if we wanted to use the syntax with two calls then we should have something like:

{% load_highchart_js_files %}
{{ linechart|load_chart:"container1"}}
{{ barchart|load_chart:"container2"}}
akipkemei commented 10 years ago

It just worked as well for me, many thanks @atodorov and @alabarga

bbratchiv commented 8 years ago

Thanks guys!

ndamulelonemakh commented 8 years ago

still works in 2016!

smaislin186 commented 7 years ago

It is not working quite like documented here anymore. The first graph shows up fine, but the second does not appear with the below code. `{% load chartit %} {{chart_list|load_charts:"cht_1,cht_2"}} {% block main %}

Home

<body>
  <div id="cht_1">{{chart_list|load_charts:"cht_1"}}</div>
  <div id="cht_2">{{chart_list|load_charts:"cht_2"}}</div>
</body>

{% endblock %}`

If I remove the "{{chart_list|load_charts:"cht_1"}}" from the actual html tags, it does not work at all. I found the below does work: `{% load chartit %} {{chart_list|load_charts:"cht_1,cht_2"}} {% block main %}

Home

<body>
  <div id="cht_1">{{chart_list|load_charts:"cht_1,cht_2"}}</div>
  <div id="cht_2">{{chart_list|load_charts:"cht_1,cht_2"}}</div>
</body>

{% endblock %}`

atodorov commented 7 years ago

@smaislin186 your HTML doesn't look right and honestly I have no idea why it works or doesn't work in the two cases you describe. Here are a few notes for you though:

and finally: this piece of code hasn't been changed in years, only documentation was updated recently. Maybe the documentation isn't 100% clear but I don't think it is broken. If this still doesn't work for you please post a link to your entire HTML template.

SandeepKumar2016 commented 7 years ago

Hi, Please help me with line charts. I have posted a question in stack, please suggest.

https://stackoverflow.com/questions/46240253/getting-load-chartit-on-my-browser-charts-are-not-loading-using-chartit

atodorov commented 7 years ago

@SandeepKumar2016 answered on SO. Sorry for the delay.