Created
March 23, 2015 09:50
-
-
Save ayozebarrera/13cac8e641ba380847a0 to your computer and use it in GitHub Desktop.
SVG chart generation in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#con { | |
resize:both; | |
overflow:hidden; | |
display:inline-block; | |
width:20em; | |
height:20em; | |
padding:0.5em; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment