作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是这方面的新手(JS、Rails 和 highcharts,但它们真的很棒),我非常感谢您的帮助。我的故事是我有一个 for 循环
,我想在 highcharts
中显示 for 循环的结果。我应该怎么做?
<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>
最佳答案
如果你有一个在循环中计算的单个值,你可以将它放在你想要的位置
series: [{
data: [<%=@amount%>]
}]
或者,如果您在某处存储了一系列数据,您想要迭代并将它们添加到您的图表中
series: [
{
data: [
<% for i in 0..@calculation.a do %>
<% @amount = ((@amount/(@percent)) + @amount) %>
<%= @amount %>,
<% end %>
]}
]
这是页面中的很多逻辑,您可能要考虑将其移动到 Controller 或模型中。为了在多个地方实现 super 可重用性,您可以使用一个操作来提供您在需要它的页面中异步获取的 JSON 数据。
编辑以匹配 Highcharts 格式
关于javascript - 如何在 highcharts 中显示 for 循环的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14741912/
我是一名优秀的程序员,十分优秀!