Search code examples
javascripthtmlcanvasjs

recursionCount error in canvasjs on setting width of container element


I'm fairly new to using canvasjs plugin for charts. There was a requirement wherein I had to resize the chartcontainer to a particular width. On changing the width from 100% to 300px I got the following error on IE as well as on chrome: recursionCount is undefined . The following is the code snippet of the same that i'm using:

<!DOCTYPE HTML>
<html>
    <head>
        <script type="text/javascript">
            window.onload = function () {
                var chart = new CanvasJS.Chart("chartContainer",
                {
                    title:{
                        text: "Common Names"
                    },
                    exportFileName: "Pie Chart",
                    exportEnabled: true,
                    animationEnabled: true,
                    legend:{
                        verticalAlign: "bottom",
                        horizontalAlign: "center"
                    },
                    data: [
                    {
                        type: "pie",
                        showInLegend: true,
                        toolTipContent: "{legendText}: <strong>{y}%</strong>",
                        indexLabel: "{label} {y}%",
                        dataPoints: [
                            {  y: 732, legendText: "James", exploded: true, label: "James" },
                            {  y: 4, legendText: "West", label: "West" },
                            {  y: 3, legendText: "John", label: "John" },
                            {  y: 247, legendText: "Peter", label: "Peter"},
                            {  y: 2716, legendText: "Mark", label: "MArk" },
                            {  y: 2, legendText: "Luke", label: "Luke"}
                        ]
                    }
                    ]
                });
                chart.render();

                var chart1 = new CanvasJS.Chart("chartContainer1",
                {
                    title:{
                        text: "Top Categoires of New Year's Resolution"
                    },
                    exportFileName: "Bar Graph",
                    exportEnabled: true,
                            animationEnabled: true,
                    legend:{
                        verticalAlign: "bottom",
                        horizontalAlign: "center"
                    },
                    data: [
                    {       
                        type: "bar",
                        showInLegend: true,
                        toolTipContent: "{legendText}: <strong>{y}%</strong>",
                        indexLabel: "{label} {y}%",
                        dataPoints: [
                            {  y: 35, legendText: "Health", exploded: true, label: "Health" },
                            {  y: 20, legendText: "Finance", label: "Finance" },
                            {  y: 18, legendText: "Career", label: "Career" },
                            {  y: 15, legendText: "Education", label: "Education"},
                            {  y: 5, legendText: "Family", label: "Family" },
                            {  y: 7, legendText: "Real Estate", label: "Real Estate"}
                        ]
                }
                ]
                });
            chart1.render();

            var chart2 = new CanvasJS.Chart("chartContainer2",
            {
                theme: "theme2",
                title:{
                text: "Earthquakes - per month"
                },
                animationEnabled: true,
                axisX: {
                valueFormatString: "MMM",
                interval:1,
                intervalType: "month"

                },
                axisY:{
                includeZero: false

                },
                data: [
                {
                    type: "line",
                    //lineThickness: 3,
                    dataPoints: [
                        { x: new Date(2012, 00, 1), y: 450 },
                        { x: new Date(2012, 01, 1), y: 414},
                        { x: new Date(2012, 02, 1), y: 520, indexLabel: "highest",markerColor: "red", markerType: "triangle"},
                        { x: new Date(2012, 03, 1), y: 460 },
                        { x: new Date(2012, 04, 1), y: 450 },
                        { x: new Date(2012, 05, 1), y: 500 },
                        { x: new Date(2012, 06, 1), y: 480 },
                        { x: new Date(2012, 07, 1), y: 480 },
                        { x: new Date(2012, 08, 1), y: 410 , indexLabel: "lowest",markerColor: "DarkSlateGrey", markerType: "cross"},
                        { x: new Date(2012, 09, 1), y: 500 },
                        { x: new Date(2012, 10, 1), y: 480 },
                        { x: new Date(2012, 11, 1), y: 510 }
                    ]}]
                });
            chart2.render();
    }
    </script>
    <script type="text/javascript" src="canvasjs.min.js"></script>
</head>
<body>
    <div id="chartContainer" style="height: 300px; width: 300px;"></div>
    <div id="chartContainer1" style="height: 300px; width: 100%;"></div>
    <div id="chartContainer2" style="height: 300px; width: 100%;"></div>
</body>
</html>

I don't understand how changing the width of element id: chartContainer from 100% to 300px can cause such an error...

Thanks in advance.


Solution

  • I managed to solve that error by creating a variable with the same name in the html page inside the script tag.

    I've added the following line just before every initialization of CanvasJS.Chart element:

        recursionCount = 0;
    

    eg:

    window.onload = function () {
        recursionCount = 0;
        //code for chartContianer
    
        recursionCount = 0;
        //code for chartContianer 1
    
        recursionCount = 0;
        //code for chartContianer 2
    };
    

    Regards,

    Darrel Viegas.