Skip to content

Instantly share code, notes, and snippets.

@erizhang
Created July 30, 2014 08:01
Show Gist options
  • Save erizhang/8e79881c289391318369 to your computer and use it in GitHub Desktop.
Save erizhang/8e79881c289391318369 to your computer and use it in GitHub Desktop.
Plotarea implementation in d3js
<!DOCTYPE HTML>
<html>
<head>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<style>
path{stroke: white; strok-width; 2; fill: steelblue;}
.plot{fill: none; stroke: #aec7e8; stroke-width: 2;}
circle{fill: steelblue; stroke: white;}
</style>
</head>
<body>
<script>
var svg = d3.select('body').append('svg')
.attr('width', 900)
.attr('height', 300)
.attr('transform', 'translate(10, 20)');
var areaGenerator = d3.svg.area()
.y0(100)
.y1(function(d){return d.y})
.x(function(d, i){return d.x})
//generate some random data
var data = d3.range(100).map(function(){return Math.random() * 30 + 0})
.map(function(d, i){return {x: i * 10, y: d}});
//draw the area shape under the line of the plot
svg.append('path').datum(data).attr('d', areaGenerator);
// give the area shape a border along its top edge
var line = d3.svg.line()
.x(function(d){return d.x})
.y(function(d){return d.y});
svg.append('path').datum(data).attr('d', line).attr('class', 'plot');
//the circles at all the points
svg.selectAll('circle').data(data).enter().append('circle')
.attr('cx', function(d){return d.x})
.attr('cy', function(d){return d.y})
.attr('r', 3);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment