Skip to content

added multichart capability #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
11 changes: 9 additions & 2 deletions django_nvd3/templatetags/nvd3_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from nvd3 import lineWithFocusChart, lineChart, \
multiBarChart, pieChart, stackedAreaChart, \
multiBarHorizontalChart, linePlusBarChart, \
cumulativeLineChart, discreteBarChart, scatterChart
cumulativeLineChart, discreteBarChart, scatterChart, \
multiChart


@register.simple_tag
Expand Down Expand Up @@ -65,13 +66,19 @@ def load_chart(chart_type, series, container, kw_extra={}, *args, **kwargs):
name = series['name' + axis_no] if series.get('name' + axis_no) else None
extra = series['extra' + axis_no] if series.get('extra' + axis_no) else {}
kwargs = series['kwargs' + axis_no] if series.get('kwargs' + axis_no) else {}
chartType = series['type' + axis_no] if series.get('type' + axis_no) else {}
yaxis = series['axisy' + axis_no] if series.get('axisy' + axis_no) else {}

chart.add_serie(name=name, y=ydata, x=xdata, extra=extra, **kwargs)
chart.add_serie(name=name, type=chartType, yaxis=yaxis, y=ydata, x=xdata, extra=extra, **kwargs)

chart.display_container = False
chart.buildcontent()

html_string = chart.htmlcontent + '\n'
# hack for multichart dual axis: without this line added to js the yAxis2 tooltip has the same yAxis1 format
if chart_type=='multiChart':
i=str.find(html_string,'nv.addGraph')
html_string = html_string[:i] + 'data_' + chart.name + '[1].yAxis=2;\n\n' + html_string[i:]
return mark_safe(html_string)


Expand Down
3 changes: 2 additions & 1 deletion nvd3_tests/testproject/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ <h3>List of supported chart:</h3>
<a href="/cumulativelinechart/">cumulativeLineChart</a> <br/>
<a href="/discretebarchart/">discreteBarChart</a> <br/>
<a href="/scatterchart/">scatterChart</a> <br/>

<a href="/multichart/">multiChart</a> <br/>

</body>
16 changes: 16 additions & 0 deletions nvd3_tests/testproject/templates/multichart.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% load static %}
<link media="all" href="{% static 'nvd3/src/nv.d3.css' %}" type="text/css" rel="stylesheet" />
<script type="text/javascript" src='{% static 'd3/d3.min.js' %}'></script>
<script type="text/javascript" src='{% static 'nvd3/nv.d3.min.js' %}'></script>

{% load nvd3_tags %}
<head>
{% if date_tag %}
{% load_chart charttype chartdata "multichart_container" True "%d %b %Y %H" %}
{% else %}
{% load_chart charttype chartdata "multichart_container" %}
{% endif %}
</head>
<body>
{% include_container "multichart_container" 400 600 %}
</body>
1 change: 1 addition & 0 deletions nvd3_tests/testproject/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
url(r'^cumulativelinechart/', 'demo_cumulativelinechart', name='demo_cumulativelinechart'),
url(r'^discretebarchart/', 'demo_discretebarchart', name='demo_discretebarchart'),
url(r'^scatterchart/', 'demo_scatterchart', name='demo_scatterchart'),
url(r'^multichart/', 'demo_multichart', name='demo_multichart'),
# url(r'^demoproject/', include('demoproject.foo.urls')),

# Uncomment the admin/doc line below to enable admin documentation:
Expand Down
28 changes: 27 additions & 1 deletion nvd3_tests/testproject/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,30 @@ def demo_scatterchart(request):
'charttype': charttype,
'chartdata': chartdata,
}
return render_to_response('scatterchart.html', data)
return render_to_response('scatterchart.html', data)

def demo_multichart(request):
"""
multiChart page
"""
start_time = int(time.mktime(datetime.datetime(2012, 6, 1).timetuple()) * 1000)
nb_element = 100
xdata = range(nb_element)
xdata = list(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*x, ydata)
ydata3 = map(lambda x: 1.5*x, ydata)
ydata4 = map(lambda x: .75 * x, ydata)

chartdata = {'x': xdata,
'name1': 'y1','y1': ydata, 'type1': 'line', 'axisy1': "1",
'name2': 'y2', 'y2': ydata2, 'type2': 'line', 'axisy2': "2",
'name3': 'y3', 'y3': ydata3, 'type3': 'scatter', 'axisy3': "1",'kwargs3': { 'shape': 'square'},
'name4': 'y4', 'y4': ydata4, 'type4': 'bar', 'axisy4': "1",
}
charttype = "multiChart"
data = {
'charttype': charttype,
'chartdata': chartdata
}
return render_to_response('multichart.html', data)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
python-nvd3==0.14.2
python-nvd3==0.14.2