Skip to content

Instantly share code, notes, and snippets.

@nick3499
Forked from d3byex/index.html
Last active March 3, 2017 17:49
Show Gist options
  • Save nick3499/d4a1b15fa9930c90fdff7cb41a3b02a3 to your computer and use it in GitHub Desktop.
Save nick3499/d4a1b15fa9930c90fdff7cb41a3b02a3 to your computer and use it in GitHub Desktop.
D3byEX 5.5: Linear Scale (Adapted to D3.js v4)
<!DOCTYPE html>
<html>
<meta charset=utf-8>
<head>
<meta name="description" content="D3.js v4, .scaleLinear" />
</head>
<body>
<script src="http://d3js.org/d3.v4.min.js"></script>
<script>
var url = "https://gist.githubusercontent.com/d3byex/e5ce6526ba2208014379/raw/8fefb14cc18f0440dc00248f23cbf6aec80dcc13/walking_dead_s5.csv";
d3.csv(url, function (error, data) {
var mappedAndConverted = data.map(function (d) {
return {
Episode: +d.Episode, // unary oper converts str to int
USViewers: +d.USViewers,
Title: d.Title
};
});
var viewership = mappedAndConverted.map(function (d) { return d.USViewers; });
var minViewership = d3.min(viewership);
var maxViewership = d3.max(viewership);
var minBarHeight = 100, maxBarHeight = 400;
var yScale = d3.scaleLinear() // v4
.domain([minViewership, maxViewership])
.range([minBarHeight, maxBarHeight]);
// view console
console.log(minViewership + " -> " + yScale(minViewership)); // 100
console.log(maxViewership + " -> " + yScale(maxViewership)); // 400
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment