nick34992
3/2/2017 - 6:31 AM

D3byEX 5.5: Linear Scale (Adapted to D3.js v4)

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>