Skip to content

Instantly share code, notes, and snippets.

@yashkandukuri
Created April 16, 2018 06:37
Show Gist options
  • Save yashkandukuri/3ebe608b4126687d81836133f5ca69e2 to your computer and use it in GitHub Desktop.
Save yashkandukuri/3ebe608b4126687d81836133f5ca69e2 to your computer and use it in GitHub Desktop.
fresh block
license: mit
Type Number Votes Voters Runtime Budget Revenue
Novel Adaptation 27 8.14 4662.96 149 36245813 258523435
Not based 16 8.11 5801.19 120 43200000 360091850.6
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<style>
.rectangle {
fill: brown;
}
.rectangle:hover {
fill: orange;
}
.axis {
font: 12px Garamond;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
</head>
<body>
<div id="drop" align=center></div><br>
<p>From the graph, he had few interesting observations to make.</p>
<ol><li>The number of movies adapted from novels are more in the list.</li>
<li>The average ratings for both the categories is almost the same.</li>
<li>The movies that were not adapted by novels had a lesser runtime.</li>
<li>More people voted for people for movies that were not adapted by novels. Maybe, that is why they have a slighty lesser average rating.</li>
</ol>
<p>Thus, he concluded that he had to adapt a novel for his film and make sure it is promoted well to ensure its success.</p>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 80, right: 180, bottom: 80, left: 180},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
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("average.csv", function (d) {
// change string (from CSV) into number format
d["Number"] = +d["Number"];
d["Votes"] = +d["Votes"];
d["Voters"] = +d["Voters"];
d["Runtime"] = +d["Runtime"];
return d;
}, function(error, data) {
if (error) throw error;
var elements = Object.keys(data[0])
.filter(function(d){
return ((d != "Type") & (d != "Revenue") & (d != "Budget"));
});
var selection = elements[0];
var y = d3.scale.linear()
.domain([0, d3.max(data, function(d){
return +d[selection];
})])
.range([height, 0]);
var x = d3.scale.ordinal()
.domain(data.map(function(d){ return d.Type;}))
.rangeBands([0, width]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("font-size", "10px")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", "-.55em")
.attr("transform", "rotate(-90)" )
.attr("transform", "rotate(-25)");
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
svg.selectAll("rectangle")
.data(data)
.enter()
.append("rect")
.attr("class","rectangle")
.attr("width", width/data.length)
.attr("height", function(d){
return height - y(+d[selection]);
})
.attr("x", function(d, i){
return (width / data.length) * i ;
})
.attr("y", function(d){
return y(+d[selection]);
})
.append("title")
.text(function(d){
return d.title + " : " + d[selection];
});
var selector = d3.select("#drop")
.append("select")
.attr("id","dropdown")
.on("change", function(d){
selection = document.getElementById("dropdown");
y.domain([0, d3.max(data, function(d){
return +d[selection.value];})]);
yAxis.scale(y);
d3.selectAll(".rectangle")
.transition()
.attr("height", function(d){
return height - y(+d[selection.value]);
})
.attr("x", function(d, i){
return (width / data.length) * i ;
})
.attr("y", function(d){
return y(+d[selection.value]);
})
.ease("linear")
.select("title")
.text(function(d){
return d.title + " : " + d[selection.value];
});
d3.selectAll("g.y.axis")
.transition()
.call(yAxis);
});
selector.selectAll("option")
.data(elements)
.enter().append("option")
.attr("value", function(d){
return d;
})
.text(function(d){
return d;
})
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment