block by micahstubbs 2697195b31c93bb3147a

CMS MEDICARE DATA (FY 2011)

Full Screen

index.html

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<link href='https://fonts.googleapis.com/css?family=Codystar:300,400|Ubuntu:300,400,500,700|Tulpen+One|Expletus+Sans:400,600,500,700|Roboto:500,900,100,300,700,400|Oswald:400,300,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="components.css">
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">


<!-- Example based on //bl.ocks.org/mbostock/3887118 -->
<!-- Tooltip example from //www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html -->

<style>

body {
  font: 11px sans-serif;
}

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

.dot {
  stroke: #000;
}

.tooltip {
  position: absolute;
  width: 300px;
  height: 28px;
  pointer-events: none;
  background:white;
}

</style>
<body>
<div class="ui" id="ui-container">
  <!-- <h1 class="xlarge oswald">CMS Medicare data (FY 2011)</h1> -->

</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.9.3/lodash.min.js"></script>
<script src="https://d3js.org/d3.v3.min.js"></script>

<script>
var margin = {top: 20, right: 20, bottom: 30, left: 60},
    width = window.innerWidth*0.9 - margin.left - margin.right,
    height = window.innerHeight*0.9 - margin.top - margin.bottom;

/* 
 * value accessor - returns the value to encode for a given data object.
 * scale - maps value to a visual display encoding, such as a pixel position.
 * map function - maps from data value to display value
 * axis - sets up axis
    d.AverageTotalPayments = +d.AverageTotalPayments;
    d.AveragedCoveredCharges = +d.AveragedCoveredCharges;
    d.TotalDischarges = +d.TotalDischarges;
 */ 

var format = d3.format("$,.2f");

// setup x 
var xValue = function(d) { return d.AverageTotalPayments;}, // data -> value
    xScale = d3.scale.linear()
                     .range([0, width]), // value -> display

    xMap = function(d) { return xScale(xValue(d));}, // data -> display
    xAxis = d3.svg.axis().scale(xScale).orient("bottom");

var areas = [];
var data;
// setup y
var yValue = function(d) { return d["AveragedCoveredCharges"];}, // data -> value
    yScale = d3.scale.linear().range([height, 0]), // value -> display
    
    yMap = function(d) { return yScale(yValue(d));}, // data -> display
    yAxis = d3.svg.axis().scale(yScale).orient("left");

// setup fill color
var cValue = function(d) { return d.Name;},
    color = d3.scale.category20();

// add the graph canvas to the body of the webpage
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 + ")");

// add the tooltip area to the webpage
var tooltip = d3.select("body").append("div")
    .attr("class", "tooltip")
    .style("opacity", 0);

// load data
d3.csv("data.csv", function(error, _data) {

    data = _data;
    // change string (from CSV) into number format
      data.forEach(function(d) {
        d.AverageTotalPayments = +d.AverageTotalPayments;
        d.AveragedCoveredCharges = +d.AveragedCoveredCharges;
        d.TotalDischarges = +d.TotalDischarges;
    //  console.log(d);
  });

areas.push("Choose a Diagnoses Related Group");

data.forEach(function(link) {
    console.log(link);
    areas.push(link.DRG);
});

areas = _.uniq(areas);

var selector = d3.select("#ui-container")
    .append("select")
    .attr("id", "branch-selector")
    .attr("class", "form-control")
    .selectAll("option")
    .data(areas)
    .enter().append("option")
    .text(function(d) { return d; })
    .attr("value", function (d, i) {
        return d;
    });

    d3.select("#branch-selector").property("selectedIndex", 0);
    d3.select("#branch-selector")
        .on("change", function(d) {
            index = this.value;
            update(index);
        })

  // don't want dots overlapping axis, so add in buffer to data domain
  xScale.domain([d3.min(data, xValue)-1, d3.max(data, xValue)+1]);
  yScale.domain([d3.min(data, yValue)-1, d3.max(data, yValue)+1]);

  // x-axis
  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis)
    .append("text")
      .attr("class", "label")
      .attr("x", width)
      .attr("y", -6)
      .style("text-anchor", "end")
      .text("Average Total Payments");

  // y-axis
  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
    .append("text")
      .attr("class", "label")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("Average Charges Billed"); // Average Covered Charges


function update(index){

  var subdata = [];
  
  data.forEach(function(d) {
    if(d.DRG == index){
      subdata.push(d);
    }
  });
  
  var dots = svg.selectAll(".dot")
        .data(subdata);
  
  dots.exit().remove();
  // draw dots

  // update the scales
  xScale.domain([d3.min(subdata, xValue)-1, d3.max(subdata, xValue)+1]);
  yScale.domain([d3.min(subdata, yValue)-1, d3.max(subdata, yValue)+1]);

  d3.selectAll(".x.axis")
    .transition().delay(400).ease('quad').duration(500)
    .call(xAxis);

  d3.selectAll(".y.axis")
    .transition().delay(400).ease('cubic').duration(500)
    .call(yAxis);
  
  dots.enter().append("circle")
      .attr({
          'r': 5,
          'cx': width/2,
          'cy': height/2,
          'opacity': 0,
          "class": "dot"
      })

  dots.transition()
      .delay(function (d,i){ return 700 + (i * 30);}).ease('quad').duration(500)
      .attr("r", function(d){ return d.TotalDischarges*0.1;  })
      .attr("cx", xMap)
      .attr("cy", yMap)
      .style("fill", function(d) { return color(cValue(d));}) 
      .style("opacity", 0.7)
      .attr("stroke-width", 0)
     
  dots.on("mouseover", function(d) {
          tooltip.transition()
               .duration(200)
               .style("opacity", .9);
          tooltip.html(d["Name"] + "<br/>" + "billed " + format(yValue(d)) + ", " + "paid " +  format(xValue(d)) + "<br/>" + d.TotalDischarges + " total discharges")
               .style("left", (d3.event.pageX + 15) + "px")
               .style("top", (d3.event.pageY - 45) + "px");
      })
      .on("mouseout", function(d) {
          tooltip.transition()
               .duration(500)
               .style("opacity", 0);
      }); 

}
});

</script>
</body>
</html>