I have a page which I am trying to show multiple NVD3 charts on tabs. I see on the NVD3 site that they store all chart names ahead of time but I am not sure how I should do this.
I have graph models which get stored in the database and then my page must dynamically render all of the charts and appropriate javascript on the fly. So far only the last graph constructed shows :(
I render this partial for each graph
<%if graph.present?%>
<%@graph = graph%>
<%end%>
<%if @graph.data.empty?%>
<%= render 'graphs/no_graph'%>
<%else%>
<script type="text/javascript">
$(document).ready(function() {
var data = $('#chart_<%=@graph.id%>').data('data');
reloadGraph(data);
$('a[data-toggle="tab"]').on('shown', function(e) {
reloadGraph(data)
})
})
function reloadGraph(data) {
nv.addGraph(function() {
var chart = nv.models.multiBarChart();
chart.yAxis.axisLabel('Monthly (kWh)');
d3.select('#chart_<%=@graph.id%> svg').datum(data).transition().duration(500).call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
}
</script>
<%=content_tag :div, id: "chart_#{@graph.id}",class: 'chart', data: {data: graph.monthly_data} do%>
<svg></svg>
<%end%>
<%end%>
On the NVD3 site they start off with:
var MainExample, ExampleOne, Example2
etc.
Then in their constuction call they assign e.g. MainExample = chart
Any advice on how I should structure this in rails??
See Question&Answers more detail:os