我有这个js代码
$.ajax({
url: "/home/getmonthly/",
type: "GET",
success: onSuccess,
error: onErrorCall
});
function onSuccess(result) {
var ctx = $("#BarChart")[0].getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: "something",
datasets: [{
label: 'revenues',
data: result.Revenues,
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderColor: 'rgba(75, 192, 192, 1)',
borderWidth: 1
},
{
label: 'expenses',
data: result.Expenses,
backgroundColor: 'rgba(54, 162, 235, 0.2)',
borderColor: 'rgba(54, 162, 235, 1)',
borderWidth: 1
}]
},
options: {
barValueSpacing: 20,
scales: {
yAxes: [{
ticks: {
min: 0
}
}]
}
}
});
}
function onErrorCall(response) {
alert('error');
}
带ViewModel的控制器(它们工作正常)
public JsonResult GetMonthly()
{
var sixMonth = DateTime.Now.AddMonths(-6);
MonthlyValuesViewModel mv = new MonthlyValuesViewModel();
mv.Expenses = (from x in db.Activities
where x.Operations.TypeOperations.TypeId == 2 &
x.Date > sixMonth
group x by x.Date.Month into monthgroup
select monthgroup.Sum(x => x.Amount)).ToArray();
mv.Revenues = (from x in db.Activities
where x.Operations.TypeOperations.TypeId == 1 &
x.Date > sixMonth
group x by x.Date.Month into monthgroup
select monthgroup.Sum(x => x.Amount)).ToArray();
return Json(mv, JsonRequestBehavior.AllowGet);
}
public class MonthlyValuesViewModel
{
public double [] Revenues { get; set; }
public double [] Expenses { get; set; }
}
完成后,我收到了这个错误:
未捕获的TypeError:t.ticks.map不是函数 at n.convertTicksToLabels(Chart.min.js:12) 在n.update(Chart.min.js:12) 在r(Chart.min.js:12) 在Object.o.each(Chart.min.js:12) at Object.update(Chart.min.js:12) at t.updateLayout(Chart.min.js:11) at t.update(Chart.min.js:11) at t.construct(Chart.min.js:11) at new t(Chart.min.js:12) 在Object.onSuccess [成功](sb-admin-charts.js:10)
请告诉我该怎么做才能解决这个问题?
答案 0 :(得分:0)
固定! 错误在于那个字符串:
labels: "something",
我必须分配一个字符串数组而不是一个字符串