|
<!DOCTYPE html> |
|
|
|
<html> |
|
<head> |
|
<title>usage pattern visualization</title> |
|
</head> |
|
|
|
<body> |
|
<!DOCTYPE html> |
|
<meta charset="utf-8"> |
|
<style> |
|
.axis text { |
|
font: 10px sans-serif; |
|
fill: #333333; |
|
} |
|
|
|
.axis path { |
|
display: none; |
|
} |
|
|
|
.y.axis line { |
|
fill: none; |
|
stroke: #eaeaea; |
|
shape-rendering: crispEdges; |
|
} |
|
|
|
.x.axis path line{ |
|
display: none; |
|
} |
|
|
|
</style> |
|
<svg class="chart"> </svg> |
|
|
|
<script src="http://d3js.org/d3.v3.min.js"></script> |
|
<script> |
|
|
|
var margin = {top: 20, right: 30, bottom: 200, left: 30 }, |
|
width = 1000 - margin.left - margin.right, |
|
height = 500 - margin.top - margin.bottom; |
|
|
|
var x = d3.scale.ordinal() |
|
.rangeRoundBands([0, width], 0.1); |
|
|
|
var y = d3.scale.linear() |
|
.range([height, 0]); |
|
|
|
var xAxis = d3.svg.axis() |
|
.scale(x) |
|
.orient("bottom"); |
|
|
|
var yAxis = d3.svg.axis() |
|
.scale(y) |
|
.tickSize(-(width-margin.right*1.2)) |
|
.orient("left"); |
|
yAxis.tickFormat(function (d) { return ''; }); |
|
|
|
var chart = d3.select(".chart") |
|
.attr("width", width + margin.left + margin.right) |
|
.attr("height", height + margin.top + margin.bottom) |
|
.append("g") |
|
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
|
|
|
d3.tsv("data60.tsv", type, function(error, data) { |
|
x.domain(data.map(function(d) { return d.name; })); |
|
y.domain([0, 100]); |
|
|
|
//create a stack for each point |
|
data.forEach(function(d){ |
|
d.values = [{h0:0, y1:d.cumulative, h1:d.cumulative, color:"#fafafa"}, //initial bar height: 0 -> transitoin height to d.cumulative |
|
{h0:d.value, y1:d.cumulative, h1:d.value, color:"#ff7f0e"}]; //initial bar height: d.value -> transition y offset by d.cumulative |
|
}); |
|
|
|
yAxis.tickValues(data.map(function(d){return d.value;})); |
|
var yLabelVar = [d3.max(function(d){return d.value;})]; |
|
|
|
var yAxisLabel = d3.svg.axis() |
|
.scale(y) |
|
.tickSize(-(width-margin.right*1.2)) |
|
.orient("left"); |
|
yAxisLabel.tickValues([0, d3.max(data.map(function(d){return d.value;}))]); |
|
|
|
chart.append("g") |
|
.attr("class", "x axis") |
|
.attr("transform", "translate(" + -x.rangeBand()*.5+"," + height + ")") |
|
.call(xAxis) |
|
.selectAll("text") |
|
.style("text-anchor", "end") |
|
.attr("dy", "0.5em") |
|
.attr("transform", "rotate(-65)"); |
|
|
|
var gy = chart.append("g") |
|
.attr("class", "y axis") |
|
.attr("transform", "translate("+ x.rangeBand()*0.5 + ", 0)") |
|
.call(yAxis); |
|
|
|
var yText = gy.append("text") |
|
.attr("transform", "rotate(-90)") |
|
.attr("x", -height/2) |
|
.attr("y", -22) |
|
.attr("dy", ".71em") |
|
.style("text-anchor", "end") |
|
.text("Usage %");; |
|
|
|
var gy2 = chart.append("g") |
|
.attr("class", "y axis") |
|
.attr("transform", "translate("+ x.rangeBand()*0.5 + ",0)") |
|
.call(yAxisLabel); |
|
|
|
var bars = chart.selectAll(".bar") |
|
.data(data).enter() |
|
.append("g") |
|
.attr("transform", function(d){ return "translate("+ x(d.name) + ",0)";}); |
|
|
|
var rects = bars.selectAll("g") |
|
.data(function(d){ return d.values;}).enter() |
|
.append("rect") |
|
.attr("y", function(d) { return y(d.h0); }) |
|
.attr("height", function(d) { return height - y(d.h0); }) |
|
.attr("width", x.rangeBand()) |
|
.attr("fill", function(d){ return d.color;} ); |
|
|
|
|
|
yAxis.tickValues(data.map(function(d){return d.cumulative;})); |
|
var searchPoint = 80, min_diff= 100, min_val=0; |
|
data.forEach(function(d){ |
|
var diff = Math.abs(d.cumulative-searchPoint); |
|
if (diff<min_diff) { |
|
min_diff = diff; |
|
min_val = d.cumulative; |
|
} |
|
}); |
|
yAxisLabel.tickValues([0, min_val, d3.max(data.map(function(d){return d.cumulative;}))]); |
|
|
|
|
|
rects.transition() |
|
.delay(2500) |
|
.duration(2000) |
|
.attr("y", function(d){ return y(d.y1);}) |
|
.attr("height", function(d){ return height-y(d.h1);}); |
|
|
|
gy.transition() |
|
.delay(3000) |
|
.duration(1000) |
|
.call(yAxis); |
|
|
|
gy2.transition() |
|
.delay(4500) |
|
.duration(1000) |
|
.call(yAxisLabel) |
|
.selectAll("line") |
|
.style("stroke", function(d){ return(d!=0)? "#aaaaaa":"#eaeaea"} ); |
|
|
|
|
|
yText.transition() |
|
.delay(5000) |
|
.duration(1000) |
|
.text("Cumulative usage%"); |
|
|
|
}); |
|
|
|
function type(d) { |
|
d.value = +d.value; // coerce to number |
|
d.cumulative = +d.cumulative; |
|
return d; |
|
} |
|
|
|
</script> |
|
|
|
</body> |
|
</html> |