【问题标题】:SVG chart generation in JavaScriptJavaScript 中的 SVG 图表生成
【发布时间】:2011-08-31 18:00:36
【问题描述】:

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

我的数字很简单——只是百分比,一个显然加起来等于 100 的数字数组。

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

谁能给我指出一个有用的实用程序或库,或者提供任何关于如何使用百分比来绘制饼图的提示 - 在 JavaScript 中?

【问题讨论】:

标签: javascript charts svg


【解决方案1】:

感谢https://stackoverflow.com/a/3642265/309483http://jbkflex.wordpress.com/2011/07/28/creating-a-svg-pie-chart-html5/(注意最后一个有bug,在此修复)

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));
});
#con {
  resize:both;
  overflow:hidden;
  display:inline-block;
  width:20em;
  height:20em;
  padding:0.5em;
}
<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>

【讨论】:

    【解决方案2】:

    这里还有一些:

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

    我尝试收集所有 svg 图形库 here 的链接。

    【讨论】:

    【解决方案3】:

    Raphael 是一个非常好的 SVG 绘图库——特别是,它优于其他库,因为在旧版本的 IE 中,它会自动回退到使用 VML,因此它可以在 IE 6 及更高版本中运行,因为以及所有其他主流浏览器。

    它有一个单独的图形库,称为gRaphael。这可以处理所有常见的图形类型(饼图、线条、条形图等),并且还可以为它们设置动画。

    如果这些还不够,使用 Raphael 主库来创建自己的库很容易 - 它非常易于使用。

    【讨论】:

      【解决方案4】:

      最好的(IMO):Highcharts

      我听说过的其他人:

      【讨论】:

        猜你喜欢
        • 2015-11-02
        • 1970-01-01
        • 2019-12-03
        • 2015-03-17
        • 1970-01-01
        • 2018-03-24
        • 1970-01-01
        • 2018-11-03
        • 2020-05-21
        相关资源
        最近更新 更多