gpt4 book ai didi

javascript - Highcharts 折线图内存不足

转载 作者:行者123 更新时间:2023-11-29 19:23:37 24 4
gpt4 key购买 nike

我正在尝试使用 Highcharts 填充折线图。直到几天前它都运行良好,然后它开始崩溃 Chrome 和 Internet Explorer 给出“SCRIPT7:内存不足”错误。我还有其他 3 个以完全相同的方式构建的折线图,效果很好。此图表作为柱形图效果很好。

这是 JSFIDDLE .代码如下:

function average(arr) {
return _.reduce(arr, function(memo, num) {
var total = memo + num;
return memo + num;

}, 0) / (arr.length === 0 ? 1 : arr.length);
}

function translateMonth(d) {
var month;
if (d.getMonth() === 0) {
month = 'Jan';
} else if (d.getMonth() === 1) {
month = 'Feb';
} else if (d.getMonth() === 2) {
month = 'Mar';
} else if (d.getMonth() === 3) {
month = 'Apr';
} else if (d.getMonth() === 4) {
month = 'May';
} else if (d.getMonth() === 5) {
month = 'Jun';
} else if (d.getMonth() === 6) {
month = 'Jul';
} else if (d.getMonth() === 7) {
month = 'Aug';
} else if (d.getMonth() === 8) {
month = 'Sep';
} else if (d.getMonth() === 9) {
month = 'Oct';
} else if (d.getMonth() === 10) {
month = 'Nov';
} else if (d.getMonth() === 11) {
month = 'Dec';
}
return month;
}

var npsData = [];
npsData = [{
avg: 100,
coach: "NUNES",
date: "Jul"
},{
avg: 100,
coach: "NUNES",
date: "Jul"
},{
avg: 100,
coach: "NUNES",
date: "Jul"
},{
avg: 100,
coach: "NUNES",
date: "Jul"
},{
avg: 100,
coach: "NUNES",
date: "Aug"
},{
avg: 100,
coach: "NUNES",
date: "Aug"
}]


var npsChartData = [];
npsChartData = _.chain(npsData)
.groupBy("date")
.map(function(value, key) {
return {
name: key,
y: parseFloat(Math.round(average(_.pluck(value, "avg"))))
}
})
.value();


var chart4 = new Highcharts.Chart({
chart: {
type: 'line',
renderTo: 'postCoachingSurvey',
plotBorderColor: '#0574AC',
borderWidth: .5,
plotShadow: true
},
credits: {
enabled: false
},
title: {
text: 'Post Coaching Survey',
style: {
color: '#666666',
fontWeight: 'bold'
}
},
legend: {
enabled: false,
itemStyle: {
color: '#868686',
fontSize: '10px'
}
},
xAxis: {
categories: _.pluck(npsChartData, name),
labels: {
enabled: true,
formatter: function() {
return this.value;
}
}
},
yAxis: {
tickPositioner: function () {
var positions = [],
tick = Math.floor(this.dataMin),
increment = Math.ceil((this.dataMax - this.dataMin) / 6);

for (tick; tick - increment <= this.dataMax; tick += increment) {
positions.push(tick);
}
//console.log (positions);
return positions;
}, title: {
text: 'eNPS',
style: {
color: '#666666'
}
},
labels: {
format: '{value}%'
}
},
tooltip: {
enabled:false,
pointFormat: '{point.y}' + '%',
crosshairs: true
//percentageDecimals: 1
},
plotOptions: {
series: {
cursor: 'pointer',
dataLabels: {
enabled: true,
//formatter: function() {
//return '{point.y}' + '%';
//}
}
}
},
series: [{
type: 'line',
name: 'Nps Average Score',
data: npsChartData,
color: '#EB6E00'
}]
});//end chart4

最佳答案

此图表的特定数据导致您的代码出错。您的 yAxis.tickPositioner 函数中有一个无限循环。你有这个代码:

increment = Math.ceil((this.dataMax - this.dataMin) / 6);

for (tick; tick - increment <= this.dataMax; tick += increment) {
// ...
}

简而言之,如果数据的最小值和最大值相同,您的increment 值将变为0,因此您的for 循环语句tick += increment 什么都不做,它会一直持续下去。

这可以通过确保您的 increment 值至少为 1 来解决。您可以尝试 ( JSFiddle):

increment = Math.max(1, Math.ceil((this.dataMax - this.dataMin) / 6));

关于javascript - Highcharts 折线图内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31925204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com