Skip to content

Instantly share code, notes, and snippets.

@mnmly
Created March 5, 2013 00:00
Show Gist options
  • Save mnmly/5086792 to your computer and use it in GitHub Desktop.
Save mnmly/5086792 to your computer and use it in GitHub Desktop.
Tributary inlet
{"description":"Tributary inlet","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"data.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01}
var data = d3.range(40).map(function(i) {
return {x: i / 39, y: (Math.sin(i / 3) + 2) / 4 };
});
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var colors = d3.scale.category10();
var x = d3.scale.linear()
.range([0, width]);
var opacity = d3.scale.linear().range([.25, 1]).domain([0, 39])
var barWidth = width / 39;
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.defined(function(d) { return d.y != null; })
.x(function(d) { return x(d.x); })
.y(function(d) { return y(d.y); });
var area = d3.svg.area()
.defined(line.defined())
.x(line.x())
.y1(line.y())
.y0(y(0));
var svg = d3.select("svg")
.datum(data)
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var clip = svg.append('clipPath').attr('id', 'clip')
var def = svg.append('def');
/*
<linearGradient id="Gradient2" x1="0" x2="0" y1="0" y2="1">
<stop offset="0%" stop-color="red"/>
<stop offset="50%" stop-color="black" stop-opacity="0"/>
<stop offset="100%" stop-color="blue"/>
</linearGradient>
*/
svg.append("linearGradient")
.attr("id", "temperature-gradient")
.attr("gradientUnits", "userSpaceOnUse")
.attr("x1", 0).attr("y1", '0%')
.attr("x2", 0).attr("y2", '100%')
.selectAll("stop")
.data([
{offset: "100%", color: "#579400" },
{offset: "10%", color: "#74c500"}
])
.enter().append("stop")
.attr("offset", function(d) { return d.offset; })
.attr("stop-color", function(d) { return d.color; });
clip.append("path")
.attr("class", "area")
.attr("d", area);
var rects = svg.append('g').attr('id', 'colors').attr('clip-path', 'url(#clip)');
rects.selectAll('rect').data(d3.range(40)).enter()
.append('rect')
.attr({
x: function(d, i){ return barWidth * i + 'px'; },
width: 100,
height: height,
'fill': 'url(#temperature-gradient)',
}).style({
opacity: function(d, i){ return opacity(i)}
})
// attr({x: 10, y: height - 100, width: 100, height: 100}).attr('clip-path', 'url(#clip)')
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
svg.append("path")
.attr("class", "line")
.attr("d", line);
/*
svg.selectAll(".dot")
.data(data.filter(function(d) { return d.y; }))
.enter().append("circle")
.attr("class", "dot")
.attr("cx", line.x())
.attr("cy", line.y())
.attr("r", 3.5);
*/
path{
fill: none;
stroke: red;
stroke: none;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment