Last active
December 3, 2018 08:55
-
-
Save fsbraun/0d89df3c2cff9a5d309f4c5a7157370d to your computer and use it in GitHub Desktop.
Using sekizai to create nvd3 charts with Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% load sekizai_tags i18n static %} | |
<div id="wid{{instance.id}}" class="sparkline"><canvas></canvas><svg></svg></div> | |
{% addtoblock 'js' %} | |
{# Include d3 and nvd3 #} | |
<script src="{% static 'js/d3.min.js' %}"></script> | |
<script src="{% static 'js/nv.d3.min.js' %}"></script> | |
{% endaddtoblock %} | |
{% addtoblock 'js' %} | |
<script> | |
d3.json('{{instance.charsource}}', function(data) { | |
nv.addGraph(function() { | |
var chart = nv.models.cumulativeLineChart() | |
.x(function(d) { return d[0] }) | |
.y(function(d) { return d[1]/100 }) //adjusting, 100% is 1.00, not 100 as it is in the data | |
.color(d3.scale.category10().range()) | |
.useInteractiveGuideline(true) | |
; | |
chart.xAxis | |
.tickValues({{instance.tick_values}}) | |
.tickFormat(function(d) { | |
return d3.time.format('%x')(new Date(d)) | |
}); | |
chart.yAxis | |
.tickFormat(d3.format(',.1%')); | |
d3.select("#wid{{instance.id}} svg") | |
.datum(data) | |
.call(Chart); | |
//TODO: Figure out a good way to do this automatically | |
nv.utils.windowResize(chart.update); | |
return chart; | |
}); | |
}); | |
</script> | |
{% endaddtoblock %} | |
{% addtoblock 'css' %} | |
<link href="{% static 'css/nv.d3.min.css' %}" rel="stylesheet"> | |
{% endaddtoblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment