javascript - canvasjs 设置容器元素宽度时出现 recursionCount 错误

标签 javascript html canvasjs

我对使用 canvasjs 图表插件相当陌生。有一个要求,我必须将图表容器的大小调整为特定宽度。将宽度从 100% 更改为 300px 时,我在 IE 和 chrome 上收到以下错误: recursionCount is undefined 。以下是我正在使用的相同代码片段:

<!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 会如何导致这样的错误...

提前致谢。

最佳答案

我通过在脚本标记内的 html 页面中创建一个具有相同名称的变量来解决该错误。

我在 CanvasJS.Chart 元素的每次初始化之前添加了以下行:

    recursionCount = 0;

例如:

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

    recursionCount = 0;
    //code for chartContianer 1

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

问候,

达雷尔·维加斯。

关于javascript - canvasjs 设置容器元素宽度时出现 recursionCount 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33491259/

相关文章:

javascript - 如何从数组中的图像源创建 Canvas 图像?

java - isSelected() 不适用于 Selenium 中的单选按钮

javascript - 如何在 HTML 5 和 javascript 中为简单的猜数字游戏随机设置生成的数字

javascript - Canvas js : how to remove a datapoint dynamically but without creating a "hole" in chart?

javascript - Canvas js 图表在 Bootstrap 3 布局中无法正确响应

javascript - 由于出现以下错误,我无法同步我的项目

Javascript:在没有长度属性和切片的情况下获取字符串的长度

javascript - 这是什么意思?问题

javascript - "debugger"在词法意义上是什么?

javascript - 带有 JSON 数据的动态图表 - CanvasJS