分类问题:
databound
dataBound
事件。但是,该功能并未被调用。我在功能中添加了一个
alert()
进行调试,但从未执行。
client端代码(HTML + JavaScript):
<div class="col-md-6 pt-20">
<label class="switch" title="Toggle to show numbers">
<input id="toggleForChart8" type="checkbox">
<span class="slider round"></span>
</label>
@(Html.Kendo().Chart<MiCATS.Models.ChartDataItem>()
.Name("chart8")
.Title("Gaps by Reasons")
.Theme("bootstrap")
.DataSource(dataSource => dataSource
.Read(read => read.Action("GetGapsIdentifiedByReasonsAnalytics", "Analytics"))
.Group(group => group.Add(model => model.Question))
)
.Series(series => {
series.Column(model => model.GapsIdentified)
.Aggregate(ChartSeriesAggregate.Sum)
.Tooltip(tooltip => tooltip.Visible(true).Template("#= value # Gaps Identified for: #= series.name #"))
.CategoryField("YearMonth")
.Name("");
})
.Legend(legend => legend
.Position(ChartLegendPosition.Bottom)
.Spacing(122)
.Labels(labels => labels.Template("#= truncateLegendLabel(text) #"))
)
.ValueAxis(axis => axis.Numeric().Color("black").Title("Gaps Count"))
.CategoryAxis(axis => axis.Color("black").Labels(label => label.Rotation(-45)).Title("Months"))
.Events(e => e.DataBound("chartDataBound"))
.Events(events => events.SeriesClick("onChartBarClick2").Render("onChartRender"))
.HtmlAttributes(new { style = "cursor: pointer;" })
)
</div>
function chartDataBound(e) {
alert("Inside chartDataBound");
var axis = e.sender.options.categoryAxis;
var monthMap = {
"Jan": 1,
"Feb": 2,
"Mar": 3,
"Apr": 4,
"May": 5,
"Jun": 6,
"Jul": 7,
"Aug": 8,
"Sep": 9,
"Oct": 10,
"Nov": 11,
"Dec": 12,
};
axis.categories = axis.categories.sort(function (a, b) {
// Ensure the categories are strings before splitting
if (typeof a !== 'string' || typeof b !== 'string') return 0;
var aParts = a.split(' ');
var bParts = b.split(' ');
// Check if the split worked correctly and both parts exist
if (aParts.length < 2 || bParts.length < 2) return 0;
var monthA = monthMap[aParts[0]]; // Get month number from the first part
var monthB = monthMap[bParts[0]];
var yearA = parseInt(aParts[2], 10); // Get year as an integer from the second part
var yearB = parseInt(bParts[2], 10);
// Compare years first, then months if years are the same
if (yearA === yearB) {
return monthA - monthB;
} else {
return yearA - yearB;
}
});
}
我尝试过的是:
werder ver ver ver vered服务器端数据已正确排序。添加了
console.log
和内部alert()
内部调试,但没有称呼该功能。
为什么
chartDataBound
事件不发射,我该如何修复?如何确保X轴类别在Kendo图表中正确排序?
dataBound
.Events(e => e.DataBound("chartDataBound"))
.Events(events => events.SeriesClick("onChartBarClick2").Render("onChartRender"))
对于分类,我不确定。修复了数据库事件后,您的现有函数可能会很好地解决。