javascript - JavaScript 中的 SVG 图表生成

标签 javascript charts svg

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

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

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

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

最佳答案

归功于 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>

关于javascript - JavaScript 中的 SVG 图表生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7261318/

相关文章:

charts - Chartjs : Is it possible to add phases in line chart?

html - 在调整窗口大小时自动缩放嵌入在 HTML 中的 SVG

html - 如何使用 CSS 将动画添加到 svg

javascript - 在 AngularJS Controller 中声明函数的方法(controllerAs 方法)

javascript - 匹配带有 [ 字符的行,但如果它有 ] 字符则不匹配

javascript - Chart.js - 同一 Canvas 上的多个圆环图

c# - 使用 C# 创建图表

javascript - 有没有办法使 SVG USE 随后可修改(或不同的技术)?

javascript - pixi 无限滚动条

javascript - 将字符串拆分为两个变量 - 未定义不是一个函数