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">
<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">
</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;
var format = d3.format("$,.2f");
var xValue = function(d) { return d.AverageTotalPayments;},
xScale = d3.scale.linear()
.range([0, width]),
xMap = function(d) { return xScale(xValue(d));},
xAxis = d3.svg.axis().scale(xScale).orient("bottom");
var areas = [];
var data;
var yValue = function(d) { return d["AveragedCoveredCharges"];},
yScale = d3.scale.linear().range([height, 0]),
yMap = function(d) { return yScale(yValue(d));},
yAxis = d3.svg.axis().scale(yScale).orient("left");
var cValue = function(d) { return d.Name;},
color = d3.scale.category20();
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 + ")");
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
d3.csv("data.csv", function(error, _data) {
data = _data;
data.forEach(function(d) {
d.AverageTotalPayments = +d.AverageTotalPayments;
d.AveragedCoveredCharges = +d.AveragedCoveredCharges;
d.TotalDischarges = +d.TotalDischarges;
});
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);
})
xScale.domain([d3.min(data, xValue)-1, d3.max(data, xValue)+1]);
yScale.domain([d3.min(data, yValue)-1, d3.max(data, yValue)+1]);
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");
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");
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();
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>