Built with blockbuilder.org
forked from 35degrees's block: Multiple movie legs
forked from 35degrees's block: Ordinal movie legs
forked from 35degrees's block: Animated movie legs
xxxxxxxxxx
<head>
<meta charset="utf-8">
<link href='https://fonts.googleapis.com/css?family=Cabin:300,400' rel='stylesheet' type='text/css'>
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
.line {
fill: none;
stroke: Orange;
stroke-width: 2.5px;
}
.axis {
font-family: Cabin;
font-size: 12px;
}
text.shadow {
stroke: white;
stroke-width: 4px;
opacity: 0.9
}
.grid line {
stroke: lightgrey;
stroke-opacity: 0.6;
shape-rendering: crispEdges
}
.grid path {
stroke-width: 0;
}
</style>
</head>
<body>
<script>
var margin = {top: 30, right: 50, bottom: 340, left: 70},
width = 960 - margin.left - margin.right,
height = 750 - margin.top - margin.bottom;
// set the ranges
var x = d3.scalePoint().domain(["Week 1","Week 2","Week 3","Week 4","Week 5","Week 6","Week 7","Week 8","Week 9","Week 10","Week 11","Week 12","Week 13","Week 14","Week 15","Week 16","Week 17","Week 18","Week 19","Week 20","Week 21","Week 22","Week 23"]).range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
// define the line
var valueline = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.titanic); });
var valueline2 = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.frozen); });
var valueline3 = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.avatar); });
var valueline4 = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.forceawakens); });
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 + ")");
function make_x_gridlines() {
return d3.axisBottom(x)
.ticks(10)
}
function make_y_gridlines() {
return d3.axisLeft(y)
.ticks(5)
}
// Get the data
d3.tsv("movielegs.tsv", function(error, data) {
if (error) throw error;
// format the data
data.forEach(function(d) {
d.date = d.date;
d.titanic = +d.titanic;
d.frozen = +d.frozen;
d.avatar = +d.avatar;
d.forceawakens = +d.forceawakens;
});
// Scale the range of the data
y.domain([0, d3.max(data, function(d) { return Math.max(d.titanic, d.frozen, d.avatar, d.forceawakens); })]);
/* svg.append("clipPath")
.attr("id","chart-area")
.append("rect")
.attr("width",width)
.attr("height", height) */
// Add the valueline path.
svg.append("path")
.data([data])
.attr("class", "line")
.attr("d", valueline);
svg.append("path")
.data([data])
.attr("class", "line")
.style("stroke","SteelBlue")
.attr("d", valueline2);
svg.append("path")
.data([data])
.attr("class", "line")
.style("stroke","IndianRed")
.attr("d", valueline3);
svg.append("path")
.data([data])
.attr("class", "line")
.style("stroke","LimeGreen")
.attr("d", valueline4);
// Add the X Axis
svg.append("g")
.attr("class","axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x).ticks(12))
.selectAll("text")
.style("text-anchor","end")
.attr("dx","-.8em")
.attr("dy",".15em")
.attr("transform","rotate(-45)");
svg.append("text")
.attr("transform","translate(" + (width/2) + "," + (height + margin.top + 40) + ")")
.style("text-anchor", "middle")
.style("font","14px Cabin")
.style("font-weight","bolder")
.text("Week of Theatrical Release");
// Add the Y Axis
svg.append("g")
.attr("class","axis")
.call(d3.axisLeft(y)
);
svg.append("text")
.attr("transform","rotate(-90)")
.attr("y",0-margin.left)
.attr("x",0-height/2)
.attr("dy","2em")
.attr("text-anchor","middle")
.style("font","14px Cabin")
.text("Weekly percent of total domestic gross (%)");
var curtain = svg.append('rect')
.attr('x', 1)
.attr('height', height)
.attr('width', width)
.attr('class', 'curtain')
.style('fill', '#ffffff')
/* Create a shared transition for anything we're animating */
/* var t = d3.transition()
.duration(6000)
.ease(d3.easeLinear)
.each('end', function() {
d3.select('line.guide')
.transition()
.style('opacity', 0)
.remove()
}); */
d3.selectAll(".curtain")
.transition()
.delay(800)
.duration(8000)
.ease(d3.easeCubic)
.attr('width', 0)
.attr('transform', 'translate(' + width + ', 0)')
.remove()
svg.append("g")
.attr("class","grid")
.attr("transform","translate(0," + height + ")")
.style("stroke-dasharray",("3,3"))
.call(make_x_gridlines()
.tickSize(-height)
.tickFormat("")
)
svg.append("g")
.attr("class","grid")
.style("stroke-dasharray",("3,3"))
.call(make_y_gridlines()
.tickSize(-width)
.tickFormat("")
)
svg.append("text")
.attr("transform", "translate(" + (10) + "," + (y(data[0].avatar)-23) + ")")
.attr("dy",".35em")
.attr('text-anchor',"start")
.attr("fill","IndianRed")
.style("font-family","Cabin")
.text("Avatar");
svg.append("text")
.attr("transform", "translate(" + (10) + "," + y(data[0].frozen) + ")")
.attr("dy",".25em")
.attr('text-anchor',"start")
.attr("fill","SteelBlue")
.style("font-family","Cabin")
.text("Frozen");
svg.append("text")
.attr("transform", "translate(" + (10) + "," + (y(data[0].forceawakens)+10) + ")")
.attr("dy",".25em")
.attr('text-anchor',"start")
.attr("fill","LimeGreen")
.style("font-family","Cabin")
.text("The Force Awakens");
svg.append("text")
.attr("transform", "translate(" + (10) + "," + (y(data[0].titanic)-5) + ")")
.attr("dy",".35em")
.attr('text-anchor',"start")
.attr("fill","Orange")
.style("font-family","Cabin")
.attr("class","shadow")
.text("Titanic");
svg.append("text")
.attr("transform", "translate(" + (10) + "," + (y(data[0].titanic)-5) + ")")
.attr("dy",".35em")
.attr('text-anchor',"start")
.attr("fill","Orange")
.style("font-family","Cabin")
.text("Titanic");
});
</script>
</body>
https://d3js.org/d3.v4.min.js