用JavaScript生成SVG图表

Bus*_*uke 8 javascript charts svg

我正在寻找一种使用SVG生成饼图的方法.

我的数字很简单 - 只是百分比,一组数字显然加起来为100.

我对SVG有一个基本的了解,但是我想不出如何将这些数字转换成有意义的坐标以便在path标签中使用

任何人都可以指向一个有用的实用程序或库,或提供任何关于如何使用百分比绘制饼图的提示 - 在JavaScript中?

Jan*_*sen 17

积分/sf/answers/254958581/http://jbkflex.wordpress.com/2011/07/28/creating-a-svg-pie-chart-html5/(注意最后一个有错误,修复在这里)

function makeSVG(tag, attrs) {
    var el= document.createElementNS('http://www.w3.org/2000/svg', tag);
    for (var k in attrs)
        if (attrs.hasOwnProperty(k)) el.setAttribute(k, attrs[k]);
    return el;
}

function drawArcs(paper, pieData){
    var total = pieData.reduce(function (accu, that) { return that + accu; }, 0);
    var sectorAngleArr = pieData.map(function (v) { return 360 * v / total; });

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

        var x1,x2,y1,y2 ;

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

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

        var d = "M200,200  L" + x1 + "," + y1 + "  A195,195 0 " + 
                ((endAngle-startAngle > 180) ? 1 : 0) + ",1 " + x2 + "," + y2 + " z";
        //alert(d); // enable to see coords as they are displayed
        var c = parseInt(i / sectorAngleArr.length * 360);
        var arc = makeSVG("path", {d: d, fill: "hsl(" + c + ", 66%, 50%)"});
        paper.appendChild(arc);
        arc.onclick = (function (originalData) { 
          return function(event) {
            alert("Associated pie piece data: " + originalData);
          }
        })(pieData[i]);
    }
}
var svgdoc = document.getElementById("s");
drawArcs(svgdoc, [52,15,20,80]); // here is the pie chart data

// You can attach additional content (from e.g. AJAX) like this:
var parser = new DOMParser();
var docToEmbed = parser.parseFromString(
  "<svg xmlns='http://www.w3.org/2000/svg'><text x='50' y='50' fill='black'>hi</text></svg>", 
  "image/svg+xml");
Array.prototype.slice.call(docToEmbed.documentElement.childNodes).forEach(function(elem) {
  svgdoc.appendChild(document.importNode(elem, true));
});
Run Code Online (Sandbox Code Playgroud)
#con {
  resize:both;
  overflow:hidden;
  display:inline-block;
  width:20em;
  height:20em;
  padding:0.5em;
}
Run Code Online (Sandbox Code Playgroud)
<div id="con">
<!-- the div container is of course optional. It is used with 
     {width,height}="100%" below to make the chart resizable. -->
<svg width="100%" height="100%" id="s"
 xmlns="http://www.w3.org/2000/svg" viewbox="0 0 400 400">
  <style type="text/css">
    path:hover {
      opacity: 0.8;
    }
  </style>
</svg>
</div>
Run Code Online (Sandbox Code Playgroud)


Eri*_*röm 6

这里还有一些:

  • Elycharts(基于jQuery和Raphaël,MIT许可证)
  • ZingCharts(商业,有SVG/VML/HTML5/Flash后端)
  • Grafico(基于Prototype和Raphaël,麻省理工学院许可证)
  • d3.js(用于交互式和动态图形的非常好的库,类似MIT的许可证)

我尽量收集链接到所有SVG图形库在这里.


Cha*_*had 1

最好的(IMO):Highcharts

\n\n

我听说过的其他人:

\n\n\n