Skip to content

Instantly share code, notes, and snippets.

@dechov
Created August 14, 2015 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dechov/bbec46eae766f67b6c78 to your computer and use it in GitHub Desktop.
Save dechov/bbec46eae766f67b6c78 to your computer and use it in GitHub Desktop.
MwRxzo
<div id="container"></div>
const stroke = [1, 10],
interpolationMode = "basis" // https://github.com/mbostock/d3/wiki/SVG-Shapes#line_interpolate
const a = d3.range(11).map(i => ({x: i, y: 3 + 5 * Math.random()}))
const b = d3.range(11).map(i => ({x: i, y: 2 + 7 * Math.random()}))
const xScale = d3.scale.linear().domain([0, 10]).range([0, 480])
const yScale = d3.scale.linear().domain([0, 10]).range([250, 0])
const area = d3.svg.area().interpolate(interpolationMode)
const minStroke = Math.min(stroke[0], stroke[1]),
diffStroke = Math.abs(stroke[0] - stroke[1])
if (stroke[0] > stroke[1]) {
area
.y(d => yScale(d.y))
.x0(d => xScale(d.x) - diffStroke / 2)
.x1(d => xScale(d.x) + diffStroke / 2)
}
else {
area
.x(d => xScale(d.x))
.y0(d => yScale(d.y) - diffStroke / 2)
.y1(d => yScale(d.y) + diffStroke / 2)
}
const Demo = React.createClass({
render() {
return <svg width={480} height={250}>
<path d={area(a)} fill="blue" stroke="blue" strokeWidth={minStroke} />
<path d={area(b)} fill="red" stroke="red" strokeWidth={minStroke} />
</svg>
}
})
React.render(<Demo />, document.getElementById("container"))
<script src="http://fb.me/react-0.13.1.js"></script>
<script src="http://d3js.org/d3.v3.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment