Ошибка recursionCount в canvasjs при установке ширины элемента контейнера

Я довольно новичок в использовании плагина canvasjs для диаграмм. Было требование, при котором я должен был изменить размер контейнера контейнера до определенной ширины. При изменении ширины от 100% до 300px я получил следующую ошибку как в IE, так и в chrome: recursionCount не определен. Ниже приведен фрагмент кода, который я использую:

<!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>

Я не понимаю, как изменение ширины элемента id: chartContainer от 100% до 300px может вызвать такую ​​ошибку...

Заранее спасибо.

1 ответ

Решение

Мне удалось устранить эту ошибку, создав переменную с тем же именем на html-странице внутри тега script.

Я добавил следующую строку перед каждой инициализацией элемента CanvasJS.Chart:

    recursionCount = 0;

например:

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

    recursionCount = 0;
    //code for chartContianer 1

    recursionCount = 0;
    //code for chartContianer 2
};

С Уважением,

Даррел Виегас.

Другие вопросы по тегам