我如何在highcharts中显示for循环的结果 [英] How can I display the outcome of the for loop in highcharts
问题描述
我是新手(JS,Rails和highcharts,但他们非常好),我真的很感谢一些帮助。我的故事是我有一个 for循环
,我想在 highcharts
中显示for循环的结果。我应该怎么做?
< p> A n:<%= @ calculation.a_n%>< / p为H.
< p> H:<%= @ calculation.h%>< / p>
< p> K:<%= @ calculation.k%>< / p>
< p> P:<%= @ calculation.p%>< / p>
< p> A:<%= @ calculation.a%>< / p>
< small> T:<%= @ calculation.created_at%>< / small>
< br />< br />
<%= @ calculation.a_n%> < br />
<%@amount =(@ calculation.h * @ calculation.k)%>
<%@percent =(@ calculation.h * @ calculation.k)/ @ calculation.p%>
<%for i in 0 .. @ calculation.a do%>
<%@newAmount =((@amount /(@ percent))+ @amount)%>
<%=#{i}+ - #{@ amount}%>< br />
<%@ amount = @ newAmount%>
<%end%>
< script type =text / javascriptcharset =utf-8>
$(function(){
new Highcharts.Chart({
chart:{
renderTo:calculate_chart
},
title:{
text:一年的总和
},
xAxis:{
text:年
},
y轴:{
title:{
text:Sum
}
},
series:[{
data:// for loop?
}]
});
});
< / script>
< div id =calculate_chartstyle =width:560px; height:300px;>< / div>
循环你可以把它放在你想要的位置
系列:[{
data:[<% = @ amount%>]
}]
如果您有一系列数据存储在你想要迭代的地方并将它们添加到你的图表中
系列:[
{
data :[
<%for i in 0 .. @ calculation.a do%>
<%@amount =((@amount /(@ percent))+ @amount)%>
<%= @amount%>,
<%end%>
]}
]
这是页面中的很多逻辑,您可能需要考虑将其移至控制器或型号。为了在多个地方实现超级可用性,您可以有一个操作,以JSON形式提供数据,并在需要它的页面中异步获取。
经过编辑以匹配高图格式
I'm new in this (JS, Rails and highcharts but they are really nice) and I would really appreciate some help. My story is that I have a for loop
and I want to display the outcome of the for loop in highcharts
. How should I do this?
<p>A n: <%= @calculation.a_n %></p>
<p>H: <%= @calculation.h %></p>
<p>K: <%= @calculation.k %></p>
<p>P: <%= @calculation.p %></p>
<p>A: <%= @calculation.a %></p>
<small>T: <%= @calculation.created_at %></small>
<br /><br />
<%= @calculation.a_n %> <br />
<% @amount = (@calculation.h * @calculation.k) %>
<% @percent = (@calculation.h * @calculation.k) / @calculation.p %>
<% for i in 0..@calculation.a do %>
<% @newAmount = ((@amount/(@percent)) + @amount) %>
<%= "#{i}" + " - #{@amount}" %><br />
<% @amount=@newAmount %>
<% end %>
<script type="text/javascript" charset="utf-8">
$(function() {
new Highcharts.Chart({
chart: {
renderTo: "calculations_chart"
},
title: {
text: "Sum for a year"
},
xAxis: {
text: "Years"
},
yAxis: {
title: {
text: "Sum"
}
},
series: [{
data: //for loop?
}]
});
});
</script>
<div id="calculations_chart" style="width:560px; height:300px;"></div>
If you have a single value that was computed in a loop you can place it right in the location you want it
series: [{
data: [<%=@amount%>]
}]
Or if you have a series of data stored somewhere you want to iterate over and add them to your chart
series: [
{
data: [
<% for i in 0..@calculation.a do %>
<% @amount = ((@amount/(@percent)) + @amount) %>
<%= @amount %>,
<% end %>
]}
]
This is a lot of logic in the page, you might want to think about moving it to the controller or model. For super re usability in multiple places you can have a action that serves up the data in JSON that you get asynchronously in the pages that need it.
edited to match highchart format
这篇关于我如何在highcharts中显示for循环的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!