如何在canvas标签中绘制canvasJS图形?

时间:2019-01-07 10:48:29

标签: javascript jquery html canvasjs

我试图在HTML的canvas标签中绘制canvasJS图形,所以我无法这样做,所以我需要一些帮助。.

<html>
    <head>
        <script>
            window.onload = function () {

            var options = {
                title: {
                    text: "Desktop OS Market Share in 2017"
                },
                data: [{
                    type: "pie",
                    dataPoints: [
                        { y: 48.36, label: "Windows 7" },
                        { y: 26.85, label: "Windows 10" },
                        { y: 1.49, label: "Windows 8" },
                        { y: 6.98, label: "Windows XP" },
                        { y: 6.53, label: "Windows 8.1" },
                        { y: 2.45, label: "Linux" },
                        { y: 3.32, label: "Mac OS X 10.12" },
                        { y: 4.03, label: "Others" }
                    ]
                }]
            };
            $("#chartContainer").CanvasJSChart(options);

            }
        </script>
    </head>
    <body>
        <div>
            <canvas id="chartContainer" height="300" width="400"> </canvas>
        </div>
        <script src="https://canvasjs.com/assets/script/jquery-1.11.1.min.js"></script>
        <script src="https://canvasjs.com/assets/script/jquery.canvasjs.min.js"></script>
    </body>
</html>

我希望使用canvas标记而不是使用div标记绘制图形。因为一旦在引导模板中使用此代码,就会出现分配给图形的面板。

我希望使用canvas标记而不是使用div标记绘制图形。

1 个答案:

答案 0 :(得分:1)

CanvasJS动态创建Canvas并将其添加到提供的chartContainer(div)中。将画布更改为div应该适合您的情况。请在下面找到更新的代码:

var options = {
    title: {
        text: "Desktop OS Market Share in 2017"
    },
    data: [{
        type: "pie",
        dataPoints: [
            { y: 48.36, label: "Windows 7" },
            { y: 26.85, label: "Windows 10" },
            { y: 1.49, label: "Windows 8" },
            { y: 6.98, label: "Windows XP" },
            { y: 6.53, label: "Windows 8.1" },
            { y: 2.45, label: "Linux" },
            { y: 3.32, label: "Mac OS X 10.12" },
            { y: 4.03, label: "Others" }
        ]
    }]
};
$("#chartContainer").CanvasJSChart(options);
<script src="https://canvasjs.com/assets/script/jquery-1.11.1.min.js"></script>
<script src="https://canvasjs.com/assets/script/jquery.canvasjs.min.js"></script>
<div id="chartContainer" height="300" width="400"></div>