在x轴类别中使用动态生成的日期数组时,我面临一个问题。最初,我尝试使用硬编码的日期值,并且效果很好。 以下是我的javaScript代码段:-
var TempDates = [
'2018-01-01',
'2018-01-02',
'2018-01-03',
'2018-01-04',
'2018-01-05',
'2018-01-06',
'2018-01-07',
'2018-01-08',
'2018-01-09',
'2018-01-10',
'2018-01-11',
'2018-01-12',
'2018-02-01',
'2018-02-02',
'2018-02-03',
'2018-02-04',
'2018-02-05',
'2018-02-06',
'2018-02-07',
'2018-02-08',
'2018-02-09',
'2018-02-10',
'2018-02-11',
'2018-02-12',
'2018-03-01',
'2018-03-02',
'2018-03-03',
'2018-03-04',
'2018-03-05',
'2018-03-06'
];
var Dates = TempDates.map(function (date) {
let formatOptions = { month: '2-digit', day: '2-digit', year: 'numeric' };
return new Date(date).toLocaleDateString(undefined, formatOptions);
});
上面的代码运行时没有给出任何错误。现在,当我尝试使用通过Ajax调用生成的数组时,则遇到了问题。下面给出的是我使用的代码:-
$.ajax({
type: "POST",
url: '@Url.Action("GetLastDates", "abc")',
dataType: "json",
contentType: "application/json",
async: false,
success: function (data) {
for (var i in data) {
TempDates1.push((data[i]));
}
}
});
var Dates = TempDates1.map(function (date) {
let formatOptions = { month: '2-digit', day: '2-digit', year: 'numeric' };
return new Date(date).toLocaleDateString(undefined, formatOptions);
});
TempDates1输出:-
["2018-09-04", "2018-09-05", "2018-09-06", "2018-09-07", "2018-09-08", "2018-09-09", "2018-09-10", "2018-09-11", "2018-09-12", "2018-09-13", "2018-09-14", "2018-09-15", "2018-09-16", "2018-09-17", "2018-09-18", "2018-09-19", "2018-09-20", "2018-09-21", "2018-09-22", "2018-09-23", "2018-09-24", "2018-09-25", "2018-09-26", "2018-09-27", "2018-09-28", "2018-09-29", "2018-09-30", "2018-10-01", "2018-10-02", "2018-10-03"]
错误:-“未捕获的RangeError:在highcharts_date_range_grouping.min.js:1处Date.toISOString()处的时间值无效
注意:-我正在使用与TempDates数组中相同的日期类型。
请帮助我,因为我在过去8个小时中一直坚持使用。我无法获得一些技巧。
高级图表代码:-
Highcharts.chart('abc_Chart', {
chart: {
type: 'column'
},
dateRangeGrouping: {
dayFormat: { month: 'numeric', day: 'numeric', year: 'numeric' }, weekFormat: { month: 'numeric', day: 'numeric', year: 'numeric' }, monthFormat: { month: 'numeric', year: 'numeric' }
},
title: {
text: 'Total Message Count'
},
credits: {
enabled: false
},
xAxis: {
categories: Dates
},
yAxis: {
title: {
text: 'Count'
}
},
plotOptions: {
column: {
stacking: 'normal'
}
},
tooltip: {
formatter: function () {
return '<b>' + this.x + '</b><br/>' +
this.series.name + ': ' + this.y + '<br/>' +
'Total: ' + this.point.stackTotal;
}
},
series: [{
name: 'Received',
stack: 'Received',
color: '#058DC7',
data: [
7.0, 6.0, 9.0, 14.0, 18.0, 21.0, 25.0, 26.0, 23.0, 18.0, 13.0, 9.0,
3.0, 4.0, 5.0, 8.0, 11.0, 15.0, 17.0, 16.0, 14.0, 10.0, 6.0, 4.0,
31.0, 43.0, 34.0, 22.0, 19.0, 11.0
],
}, {
name: 'Failure',
stack: 'Sent',
color: '#ff0000',
data: [
3.0, 4.0, 5.0, 8.0, 11.0, 15.0, 17.0, 16.0, 14.0, 10.0, 6.0, 4.0,
7.0, 6.0, 9.0, 14.0, 18.0, 21.0, 25.0, 26.0, 23.0, 18.0, 13.0, 9.0,
32.0, 53.0, 14.0, 27.0, 19.0, 14.0
],
legendIndex: 1,
},
{
name: 'Success',
stack: 'Sent',
color: '#50B432',
data: [
3.0, 4.0, 5.0, 8.0, 11.0, 15.0, 17.0, 16.0, 14.0, 10.0, 6.0, 4.0,
7.0, 6.0, 9.0, 14.0, 18.0, 21.0, 25.0, 26.0, 23.0, 18.0, 13.0, 9.0,
32.0, 53.0, 14.0, 27.0, 19.0, 14.0
],
legendIndex: 0,
}]
});
答案 0 :(得分:2)
我已经在jsfiddle中尝试了您的代码,即使您后来添加了数据,它也确实可以正常工作。
我要说的问题是,ajax调用是异步的,并且渲染图表时尚未填充Dates数组。尝试以加载数据后构造图表的方式重写代码。像这样:
$.ajax({
type: "POST",
url: '@Url.Action("GetLastDates", "abc")',
dataType: "json",
contentType: "application/json",
async: false,
success: function (data) {
for (var i in data) {
TempDates1.push((data[i]));
}
ctd(TempDates1);
}
});
function ctd( data ){
var Dates = data.map(function (date) {
let formatOptions = { month: '2-digit', day: '2-digit', year: 'numeric' };
return new Date(date).toLocaleDateString(undefined, formatOptions);
});
Highcharts.chart('abc_Chart', {
chart: {
type: 'column'
},
dateRangeGrouping: {
dayFormat: { month: 'numeric', day: 'numeric', year: 'numeric' }, weekFormat:
{ month: 'numeric', day: 'numeric', year: 'numeric' }, monthFormat: { month: 'numeric', year: 'numeric' }
},
title: {
text: 'Total Message Count'
},
credits: {
enabled: false
},
xAxis: {
categories: Dates
},
yAxis: {
title: {
text: 'Count'
}
},
plotOptions: {
column: {
stacking: 'normal'
}
},
tooltip: {
formatter: function () {
return '<b>' + this.x + '</b><br/>' +
this.series.name + ': ' + this.y + '<br/>' +
'Total: ' + this.point.stackTotal;
}
},
series: [{
name: 'Received',
stack: 'Received',
color: '#058DC7',
data: [
7.0, 6.0, 9.0, 14.0, 18.0, 21.0, 25.0, 26.0, 23.0, 18.0, 13.0, 9.0,
3.0, 4.0, 5.0, 8.0, 11.0, 15.0, 17.0, 16.0, 14.0, 10.0, 6.0, 4.0,
31.0, 43.0, 34.0, 22.0, 19.0, 11.0
],
}, {
name: 'Failure',
stack: 'Sent',
color: '#ff0000',
data: [
3.0, 4.0, 5.0, 8.0, 11.0, 15.0, 17.0, 16.0, 14.0, 10.0, 6.0, 4.0,
7.0, 6.0, 9.0, 14.0, 18.0, 21.0, 25.0, 26.0, 23.0, 18.0, 13.0, 9.0,
32.0, 53.0, 14.0, 27.0, 19.0, 14.0
],
legendIndex: 1,
},
{
name: 'Success',
stack: 'Sent',
color: '#50B432',
data: [
3.0, 4.0, 5.0, 8.0, 11.0, 15.0, 17.0, 16.0, 14.0, 10.0, 6.0, 4.0,
7.0, 6.0, 9.0, 14.0, 18.0, 21.0, 25.0, 26.0, 23.0, 18.0, 13.0, 9.0,
32.0, 53.0, 14.0, 27.0, 19.0, 14.0
],
legendIndex: 0,
}]
});
}