如何解决列表不采用上一年的值来绘制图表
我正在尝试使用 chart.js 库绘制图形。它正在绘制所有值,但不是去年的值。用于绘制余额值的列表不接受最近几年的值。去年的余额值为负。我有两个问题,
- 如何让图表绘制过去几年的值
- 如何将负值更改为在图表上显示为 0。 非常感谢任何帮助。谢谢
这是控制器方法
public IActionResult FixedrateamortizationSchedule(decimal Principal = 312500,decimal Rate = 3.50M,decimal DownPayment = 62500,int years = 3,DateTime? startDate = null )
{
AmortizedLib amortizedLib = new AmortizedLib();
// calculates the mortgage and amortized schedule
var amortizedSchedule = amortizedLib.amortizationScheduleCalculation(Principal,Rate,DownPayment,years,startDate??DateTime.Now);
//list of the years
IEnumerable<int> EachYears()
{
DateTime startYear; DateTime endYear; int term = years;
int yr = DateTime.Now.Year;
startYear = new DateTime((yr),1,1);
//startYear = DateTime.Parse(y);
endYear = startYear.AddYears(years);
for (int i = startYear.Year; i <= endYear.Year; i++)
{
yield return i;
}
}
var yearList = EachYears().ToList();
//list for year end balance
List<decimal> yearlyBalance = new List<decimal>();
foreach (var item in amortizedSchedule)
{
var sDate = DateTime.Now;
var endDate = sDate.AddYears(years);
if (item.Date.Month == 12 || item.Date == endDate)
{
if(item.Balance < 0 )
{
item.Balance = 1;
yearlyBalance.Add(item.Balance);
}
yearlyBalance.Add(item.Balance);
}
}
ViewBag.Year = yearList;
ViewBag.Balance = yearlyBalance;
return View(amortizedSchedule);
}
这是Javascript
@section Scripts{
<script>
var year = @Html.Raw(Json.Serialize(ViewBag.Year));
var balanceArray = @Html.Raw(Json.Serialize(ViewBag.Balance));
var ctx = $("#line-chart");
var lineChart = new Chart(ctx,{
type:'line',data:{
labels:year,datasets:[{
label: "Balance",data:balanceArray,backgroundColor:["rgba(242,166,54,5)"],}]
},options:{
mantainAspectRatio:false,scales:{xAxes: [{ ticks: { beginAtZero: true } }]
},legend:{display:false},},});
</script>
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。