SVG radial gradient with a circle. Also works with a donut chart -- for example.
Last active
August 29, 2015 14:02
Radial Gradient
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
<!DOCTYPE html> | |
<meta charset='utf-8'> | |
<title>Radial Gradient"</title> | |
<body> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script> | |
<script> | |
var w = 500, h = 500, | |
r = Math.min(w, h) / 2; | |
var svg = d3.select("body").append("svg") | |
.attr("width", w) | |
.attr("height", h) | |
.append("g") | |
.attr("transform", "translate(" + w/2 + "," + h/2 + ")"); | |
var radialGradient = svg.append("defs") | |
.append("radialGradient") | |
.attr("id", "radial-gradient"); | |
radialGradient.append("stop") | |
.attr("offset", "0%") | |
.attr("stop-color", "red"); | |
radialGradient.append("stop") | |
.attr("offset", "100%") | |
.attr("stop-color", "#fff"); | |
svg.append("circle") | |
.attr("cx", 0) | |
.attr("cy", 0) | |
.attr("r", r) | |
.style("fill", "url(#radial-gradient)"); | |
</script> |
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
<!DOCTYPE html> | |
<meta charset='utf-8'> | |
<body> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script> | |
<script> | |
var dataset = { | |
apples: [53245, 28479, 19697, 24037, 40245], | |
}; | |
var width = 460, | |
height = 300, | |
radius = Math.min(width, height) / 2; | |
var color = d3.scale.category20(); | |
var pie = d3.layout.pie() | |
.sort(null); | |
var arc = d3.svg.arc() | |
.innerRadius(radius - 100) | |
.outerRadius(radius - 50); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height) | |
.append("g") | |
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")"); | |
var grads = svg.append("defs").selectAll("radialGradient").data(pie(dataset.apples)) | |
.enter().append("radialGradient") | |
.attr("gradientUnits", "userSpaceOnUse") | |
.attr("cx", 0) | |
.attr("cy", 0) | |
.attr("r", "100%") | |
.attr("id", function(d, i) { return "grad" + i; }); | |
grads.append("stop").attr("offset", "15%").style("stop-color", function(d, i) { return color(i); }); | |
grads.append("stop").attr("offset", "20%").style("stop-color", "white"); | |
grads.append("stop").attr("offset", "27%").style("stop-color", function(d, i) { return color(i); }); | |
var path = svg.selectAll("path") | |
.data(pie(dataset.apples)) | |
.enter().append("path") | |
.attr("fill", function(d, i) { return "url(#grad" + i + ")"; }) | |
.attr("d", arc); | |
</script> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment