列表不采用上一年的值来绘制图表

问题描述

我正在尝试使用 chart.js 库绘制图形。它正在绘制所有值,但不是去年的值。用于绘制余额值的列表不接受最近几年的值。去年的余额值为负。我有两个问题,

  1. 如何让图表绘制过去几年的值
  2. 如何将负值更改为在图表上显示为 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 (将#修改为@)