block by larskotthoff f6f9c5bcffd805441605

f6f9c5bcffd805441605

Full Screen

index.html

<!DOCTYPE html>
<title>Home Price Index</title>
<meta charset="utf-8">
<style>
body {
  font: 10px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.x.axis path {
  display: none;
}

.line {
  fill: none;
  stroke: steelblue;
  stroke-width: 1.5px;
  -webkit-transition: opacity 0.3s;
  -moz-transition: opacity 0.3s;
  -o-transition: opacity 0.3s;
  transition: opacity 0.3s;
}

path.invisible {
  fill: none;
  stroke: rgba(0,0,0,0);
  stroke-width: 8px;
}

text.label {
  font-size: 12px;
  font-weight: bold;
}

</style>

<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<link rel="stylesheet" href="//projects.flowingdata.com/life-expectancy/life.css" />
<script src="//documentcloud.github.com/underscore/underscore.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>


<body>
<select id="zipselected">
	<option value=''> Select a Zip Code</option>
  <option value="32804" > 32804</option>
  <option value="test"> Test </option>
</select>
</body>
</html>
<script type="text/javascript">

var margin = {top: 20, right: 220, bottom: 30, left: 50},
    width = 1000 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;


var format = d3.time.format("%b %Y");

var start = format.parse("Jan 2000");
var end = format.parse("Feb 2014");
var range = d3.time.months(start,end);

var x = d3.time.scale()
    .range([0,width])
    .domain([start,end]);

var y = d3.scale.linear()
    .range([height, 0]);

var color = function(i) {
  return d3.rgb(192,192,192).toString();
};

var color2= function(i) {
	return d3.rgb(255,69,0).toString();
	};

var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom");

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left");

var line = d3.svg.line()
    .interpolate("basis")
    .defined(function(d) { return d != null; })
    .x(function(d,i) { return x(range[i]); })
    .y(function(d) { return y(d); });

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

d3.csv("homeindex.csv", function(raw) {
  var data = [];
  var indexzips = _(raw).pluck("zipcode");

  _(raw).each(function(series) {
    var value = {};
    data.push({
      id: series["Zipcode"],
      zipcode: series["Zipcode"],
      values: _(range).map(function(month) {
                return parseFloat(series[format(month)]) || null;
              })
    });
  });
		
  var values = _(data).chain().pluck('values').flatten().value();

  y.domain([
    100,
    d3.max(values)
  ]);


  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);

  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
    .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .style("font-weight", "bold")
      .text("Home Price Index");
      
  

  var series = svg.selectAll(".series")
      .data(data)
    .enter().append("g")
      .attr("class", "series");

  series.append("path")
      .attr("class", "line")
      .attr("d", function(d) { return line(d.values); })
      .style("opacity", 0.5)
      .style("stroke", function(d,i) { return color(i); });

  series.append("path")
      .attr("class", "invisible hover")
      .attr("d", function(d) { return line(d.values); });

  series.append("text")
      .attr("class", "label hover")
      .datum(function(d) { return {zipcode: d.zipcode, value: d.values[d.values.length - 1]}; })
      .attr("transform", function(d) { return "translate(" + x(end) + "," + y(d.value) + ")"; })
      .attr("x", 3)
      .attr("dy", ".35em")
      .style("fill", function(d,i) { return color2(i); })
      .style("opacity", 0)
      .text(function(d) { return d.zipcode; });

  series.selectAll(".hover")
      .on("mouseover", function(d,i) {
        d3.selectAll(".line")
          .style("opacity", 0.82)
          .filter(function(p) { return p.zipcode == d.zipcode; })
          .style("opacity", 1)
          .style("stroke-width", 2)
     			.style("stroke", function(d,i) { return color2(i); });
        d3.selectAll(".series text")
          .style("opacity", 0)
          .filter(function(p) { return p.zipcode == d.zipcode; })
          .style("opacity", 1)
          .on("mouseover", onmouseover)
          .on("mouseout", onmouseout);
      })
      .on("mouseout", function(d,i) {
        d3.selectAll(".line")
          .style("opacity", 1)
          .style("stroke", function(d,i) { return color(i); })
          .style("stroke-width", 1);
        d3.selectAll(".series text")
          .style("opacity", 0);
      })
      
       function onmouseover(d,i){
        d3.selectAll(".line")
          .style("opacity", 0.82)
          .filter(function(p) { return p.zipcode == d.zipcode; })
          .style("opacity", 1)
          .style("stroke-width", 2)
     	  .style("stroke", function(d,i) { return color2(i); })
     	 d3.selectAll(".series text")
          .style("opacity", 0)
          .filter(function(p) { return p.zipcode == d.zipcode; })
          .style("opacity", 1);
      }
          
           $("#zipselected").change(ziphandler)
       function ziphandler(){
       var key = $(this)
              .children(":selected")
              .val();
      onmouseover({zipcode : key});
    }
});
</script>