I'm getting the following error trying to bind a colunm to a Kendo UI MVC Chart:
InvalidOperationException: Bound columns require a field or property access expression.
<div class="demo-section k-content wide">
@(Html.Kendo().Chart(Model)
.Name("chart")
.Theme("blueOpal")
.HtmlAttributes(new { style = "height: 400px;" })
.Title("Site Visitors Stats /thousands/")
.Legend(legend => legend
.Position(ChartLegendPosition.Bottom)
)
.SeriesDefaults(seriesDefaults => seriesDefaults
.Column().Stack(true)
)
.Series(series =>
{
series.Column(model => ChartDataHelper.GetWeekTotals(model.Invoices)).Name("Week Totals"); // fails
series.Column(new double[] { 52000, 34000, 23000, 48000, 67000, 83000 }).Name("Unique visitors"); // works
})
.CategoryAxis(axis => axis
.Categories("Jan", "Feb", "Mar", "Apr", "May", "Jun")
.MajorGridLines(lines => lines.Visible(false))
)
.ValueAxis(axis => axis
.Numeric()
.Line(line => line.Visible(false))
)
.Tooltip(tooltip => tooltip
.Visible(true)
.Format("{0}")
)
)
</div>
Here is the code for the helper static class:
public static class ChartDataHelper
{
public static decimal[] GetWeekTotals(ICollection<InvoiceDto> invoices)
{
var currentWeekNumber = DateHelper.GetWeekOfYear(DateTime.Now);
var firstWeekNumber = currentWeekNumber - 5;
decimal[] results = new decimal[5];
for (var i = 0; i < 5; i++)
{
results[i] = invoices.Where(o =>
o.InvoiceDate.Year == DateTime.Now.Year &&
DateHelper.GetWeekOfYear(o.InvoiceDate) == (firstWeekNumber + i)
).Sum(o => o.TotalNetCharge);
}
return results;
}
It seems that since decimal[] is not a primitive type there seems to be a problem passing data dynamically, but when creating an anonymous object of type double[], as seen in the second series column, there's no problem. How so?
GetWeekTotalsfunction instantiates an array ofdecimalwith a length of 5 but your loop only assigns values to the array at position < 5 i.e. position 5 isn't gonna get populated with a value. PS to extend on @NeilHibbert comment above, some reference material - Sandmanintorstringto a chart column... - JF Beaulieudecimal[]but this generates this error:InvalidOperationException: The property 'WeeksTotals' could not be mapped, because it is of type 'Decimal[]' which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it.- JF Beaulieu