使用javascript创建PIE图表

时间:2013-10-25 06:44:16

标签: javascript charts

我正在尝试创建饼图。数据将来自db,但在这里我采取了静态。

这是我的小提琴: http://jsfiddle.net/MJqyC/

图表可以在我的系统上看到加载。在这里,我试图按下按钮显示,但它没有显示。错在哪里?

js功能是:

var paper;
        var arc;
        var colorArr = ["#468966","#FFF0A5"];
        var pieData = [50,50];
        var sectorAngleArr = [];
        var total = 0;
        var startAngle = 0;
        var endAngle = 0;
        var x1,x2,y1,y2 = 0;

        function init(){
            paper = Raphael("holder");
            //CALCULATE THE TOTAL
            for(var k=0; k < pieData.length; k++){
                total += pieData[k];
               // alert(total);
            }
            //CALCULATE THE ANGLES THAT EACH SECTOR SWIPES AND STORE IN AN ARRAY
            for(var i=0; i < pieData.length; i++){
                var angle = Math.ceil(360 * pieData[i]/total);
               // alert(angle);
                sectorAngleArr.push(angle);
            }
            drawArcs();
        }               

        function drawArcs(){
            for(var i=0; i <sectorAngleArr.length; i++){
                startAngle = endAngle;
                endAngle = startAngle + sectorAngleArr[i];

                x1 = parseInt(200 + 180*Math.cos(Math.PI*startAngle/180));
                y1 = parseInt(200 + 180*Math.sin(Math.PI*startAngle/180));

                x2 = parseInt(200 + 180*Math.cos(Math.PI*endAngle/180));
                y2 = parseInt(200 + 180*Math.sin(Math.PI*endAngle/180));                

                var d = "M200,200  L" + x1 + "," + y1 + "  A180,180 0 0,1 " + x2 + "," + y2 + " z"; //1 means clockwise
                alert(d);
                arc = paper.path(d);
                arc.attr("fill",colorArr[i]);
            }
        }

更新:

完整代码:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Simple SVG Pie Chart</title>
    <script type="text/javascript" src="raphael-min.js"></script>
    <script type="text/javascript">
        var paper;
        var arc;
        var colorArr = ["#468966","#FFF0A5"];
        var pieData = [50,50];
        var sectorAngleArr = [];
        var total = 0;
        var startAngle = 0;
        var endAngle = 0;
        var x1,x2,y1,y2 = 0;

        function init(){
            paper = Raphael("holder");
            //CALCULATE THE TOTAL
            for(var k=0; k < pieData.length; k++){
                total += pieData[k];
          //      alert(total);
            }
            //CALCULATE THE ANGLES THAT EACH SECTOR SWIPES AND STORE IN AN ARRAY
            for(var i=0; i < pieData.length; i++){
                var angle = Math.ceil(360 * pieData[i]/total);
            //    alert(angle);
                sectorAngleArr.push(angle);
            }
            drawArcs();
        }               

        function drawArcs(){
            for(var i=0; i <sectorAngleArr.length; i++){
                startAngle = endAngle;
                endAngle = startAngle + sectorAngleArr[i];

                x1 = parseInt(200 + 180*Math.cos(Math.PI*startAngle/180));
                y1 = parseInt(200 + 180*Math.sin(Math.PI*startAngle/180));

                x2 = parseInt(200 + 180*Math.cos(Math.PI*endAngle/180));
                y2 = parseInt(200 + 180*Math.sin(Math.PI*endAngle/180));                

                var d = "M200,200  L" + x1 + "," + y1 + "  A180,180 0 0,1 " + x2 + "," + y2 + " z"; //1 means clockwise
            //s    alert(d);
                arc = paper.path(d);
                arc.attr("fill",colorArr[i]);
            }
        }
    </script>
</head>
<body>
    <div id="holder" style="width:600px; height:400px;">
    <input type="submit" onclick="init();" value="Graph"> </input>
    </div>
</body>
</html>

添加js:

https://github.com/DmitryBaranovskiy/raphael/blob/master/raphael-min.js

1 个答案:

答案 0 :(得分:1)

当我打开浏览器的控制台时,我收到了以下错误:

Uncaught SyntaxError: Unexpected token < raphael-min.js:4
Uncaught ReferenceError: init is not defined (index):77
2 Uncaught TypeError: Cannot read property 'id' of null modern_scroll.js:1191

如果我通过控制台打开raphael-min.js,我会看到HTML而不是JavaScript,我会说问题从那里开始。