gpt4 book ai didi

javascript - 如何在 highcharts 中显示 for 循环的结果

转载 作者:行者123 更新时间:2023-11-30 05:51:15 25 4
gpt4 key购买 nike

我是这方面的新手(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com