Highcharts Line Charts内存不足

时间:2015-08-10 17:00:31

标签: javascript jquery highcharts underscore.js

我正在尝试使用Highcharts填充折线图。它在几天前工作得很好,然后它开始崩溃Chrome和Internet Explorer出现“SCRIPT7:Out of memory”错误。我有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

1 个答案:

答案 0 :(得分:3)

此图表的特定数据会在您的代码中显示错误。您的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));