Kendo Chart系列数据与Tooltip关联问题求助
解决Kendo Chart系列数据与Tooltip关联的问题
看起来你是在配置Kendo折线图的Tooltip时,遇到了自定义time字段无法正确关联的问题,我来帮你梳理下问题点和解决办法。
首先先拆解下你当前的代码逻辑:
for (var i = 0; i < ChartData.SeriesList.length; i++) { Series.push({ data: ChartData.SeriesList[i].Values, name: ChartData.SeriesList[i].SeriesName, time: ChartData.SeriesList[i].ExactDate }); }
这里你把time字段挂载在了整个系列对象上,而不是单个数据点上。Kendo Chart的Tooltip默认是针对单个数据点生效的,所以要根据你的实际数据场景,分两种方式处理:
场景1:整个系列共用同一个时间值
如果ExactDate是该系列的统一时间(比如这个系列是某一天的所有销售数据),那你可以直接在Tooltip模板中引用系列的time属性:
$("#chart").kendoChart({ title: { text: "Sprzedaż" }, legend: { position: "bottom" }, chartArea: { background: "" }, seriesDefaults: { type: "line", style: "smooth" }, // 补全你原有的style配置 series: Series, tooltip: { visible: true, template: "#= series.name #<br/>Wartość: #= value #<br/>Data: #= series.time #" } });
场景2:每个数据点对应独立的时间值
如果ExactDate是每个数据点对应的时间(比如系列是一段连续时间的数值序列),那你需要先重构数据结构,把数值和对应时间绑定成对象数组:
第一步:修改数据填充逻辑
for (var i = 0; i < ChartData.SeriesList.length; i++) { var seriesItem = ChartData.SeriesList[i]; // 将数值数组和时间数组配对成带时间的对象数组 var dataWithTime = seriesItem.Values.map((val, index) => ({ value: val, time: seriesItem.ExactDate[index] })); Series.push({ data: dataWithTime, name: seriesItem.SeriesName }); }
第二步:配置Tooltip读取数据点的time字段
$("#chart").kendoChart({ title: { text: "Sprzedaż" }, legend: { position: "bottom" }, chartArea: { background: "" }, seriesDefaults: { type: "line", style: "smooth" }, series: Series, tooltip: { visible: true, template: "#= series.name #<br/>Wartość: #= value #<br/>Data: #= dataItem.time #" } });
额外提示:如果你的ExactDate是原生日期对象,还可以用kendo.format来格式化显示,比如:
template: "#= series.name #<br/>Wartość: #= value #<br/>Data: #= kendo.format('{0:yyyy-MM-dd HH:mm}', dataItem.time) #"
内容的提问来源于stack exchange,提问作者Jacek
相关产品推荐
相关产品推荐

