简体   繁体   中英

Highcharts Line Charts Out of memory

I am trying to populate a line chart using Highcharts. It was working great until a couple of days ago, then it started crashing Chrome and Internet Explorer gives the "SCRIPT7: Out of memory" error. I have 3 other line charts built the exact same way, that work fine. This chart works great as a column chart.

Here is the JSFIDDLE . Code below:

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

The particular data for this chart brings out an error in your code. You have an endless loop in your yAxis.tickPositioner function. You have this code:

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

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

In short, your increment value becomes 0 if the min and max value of the data is the same, and therefore your for loop statement tick += increment does nothing, and it keeps going forever.

This can be fixed by assuring your increment value is a minimum of 1. You could try ( JSFiddle ):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM