nick34992
3/1/2017 - 6:30 AM

D3byEX 4.9: Axis with Inverted Labels (Adapted to D3.js v4)

D3byEX 4.9: Axis with Inverted Labels (Adapted to D3.js v4)

<!DOCTYPE html>
<html>
    <meta charset=utf-8>
<head>
    <script src="http://d3js.org/d3.v4.min.js"></script>
    <script src="https://d3js.org/d3-selection-multi.v1.min.js"></script>
    <meta name="description" 
          content="D3.js, .axisLeft, .scaleLinear, inverted labels, 
                   axis left" />
</head>
<body>
    <script>
        var data = [55, 44, 30, 23, 17, 14, 16, 25, 41, 61, 85, 101, 95, 105,
                    114, 150, 180, 210, 125, 100, 71, 75, 72, 67];
        var maxValue = d3.max(data);
        var width = 500, height = maxValue;
        var svg = d3.select('body')
                    .append('svg')
                    .attrs({width: width, height: height}); // selection multi
        var scale = d3.scaleLinear() // v4
                      .domain([maxValue, 0]) // inverted
                      .range([0, maxValue]);
        var axis = d3.axisLeft() // v4
                     .scale(scale);
        var axisGroup = svg.append('g')
                           .attr('transform', 'translate(50, 0)');
        var axisNodes = axisGroup.call(axis);
    </script>
</body>
</html>