G Boomanikandan G Boomanikandan - 16 days ago 4
jQuery Question

d3 waterfall graph rotate in x axis values 180 to -90 degree

can you resolve this problem.

I am not able to rotate in X-axis values. can you please check below examples. Now x-axis text is coming horizontally but we wants Vertical alignment.

In my requirement is rotate -60 or -90 only. in "Model 1 , Module 2, Module 3" values i needs to rotate.



var margin = {top: 20, right: 30, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom,
padding = 0.3;

var x = d3.scale.ordinal()
.rangeRoundBands([0, width], padding);

var y = d3.scale.linear()
.range([height, 0]);

var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");

var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(function(d) { return dollarFormatter(d); });

var chart = d3.select(".chart")
.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("data.csv", type, function(error, data) {

var data = [{ name :"Module 1",value : 20 },{ name :"Module 2",value :15},{ name :"Module 3 ",value :45},
{ name :"Final Count ",value :200}];
//console.log(data);
// Transform data (i.e., finding cumulative values and total) for easier charting
var cumulative = 0;
for (var i = 0; i < data.length; i++) {
data[i].start = cumulative;
cumulative += data[i].value;
data[i].end = cumulative;

data[i].class = ( data[i].value >= 0 ) ? 'positive' : 'negative'
}
data.push({
name: 'Total',
end: cumulative,
start: 0,
class: 'total'
});

x.domain(data.map(function(d) { return d.name; }));
y.domain([0, d3.max(data, function(d) { return d.end; })]);

chart.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);

chart.append("g")
.attr("class", "y axis")
.call(yAxis);

var bar = chart.selectAll(".bar")
.data(data)
.enter().append("g")
.attr("class", function(d) { return "bar " + d.class })
.attr("transform", function(d) { return "translate(" + x(d.name) + ",0)"; });

bar.append("rect")
.attr("y", function(d) { return y( Math.max(d.start, d.end) ); })
.attr("height", function(d) { return Math.abs( y(d.start) - y(d.end) ); })
.attr("width", x.rangeBand());

bar.append("text")
.attr("x", x.rangeBand() / 2)
.attr("y", function(d) { return y(d.end) + 5; })
.attr("dy", function(d) { return ((d.class=='negative') ? '-' : '') + ".75em" })
.text(function(d) { return dollarFormatter(d.end - d.start);});


bar.filter(function(d) { return d.class != "total" }).append("line")
.attr("class", "connector")
.attr("x1", x.rangeBand() + 5 )
.attr("y1", function(d) { return y(d.end) } )
.attr("x2", x.rangeBand() / ( 1 - padding) - 5 )
.attr("y2", function(d) { return y(d.end) } )
//});

function type(d) {
d.value = +d.value;
return d;
}

function dollarFormatter(n) {
n = Math.round(n);
var result = n;
if (Math.abs(n) > 1000) {
result = Math.round(n/1000) + 'K';
}
return result;
}

.bar.total rect {
fill: steelblue;
}

.bar.positive rect {
fill: darkolivegreen;
}
.bar.negative rect {
fill: crimson;
}

.bar line.connector {
stroke: grey;
stroke-dasharray: 3;
}

.bar text {
fill: white;
font: 10px sans-serif;
text-anchor: middle;
}

.axis text {
font: 10px sans-serif;
}

.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}

<script src="http://d3js.org/d3.v3.min.js"></script>
<svg class="chart"></svg>




Answer

To rotate only the first 3 ticks ("module 1", "module 2" and "module 3"):

var ticks = d3.selectAll(".x.axis text").each(function(d, i) {
    if (i < 3) {
        d3.select(this).attr("y", 0)
        d3.select(this).attr("x", 10)
        d3.select(this).attr("dy", ".35em")
        d3.select(this).attr("transform", "rotate(90)")
        d3.select(this).style("text-anchor", "start");
    }
});

Check the demo:

var margin = {
        top: 20,
        right: 30,
        bottom: 60,
        left: 40
    },
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom,
    padding = 0.3;

var x = d3.scale.ordinal()
    .rangeRoundBands([0, width], padding);

var y = d3.scale.linear()
    .range([height, 0]);

var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom");

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left")
    .tickFormat(function(d) {
        return dollarFormatter(d);
    });

var chart = d3.select(".chart")
    .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("data.csv", type, function(error, data) {

var data = [{
    name: "Module 1",
    value: 20
}, {
    name: "Module 2",
    value: 15
}, {
    name: "Module 3 ",
    value: 45
}, {
    name: "Final Count ",
    value: 200
}];
//console.log(data);
// Transform data (i.e., finding cumulative values and total) for easier charting
var cumulative = 0;
for (var i = 0; i < data.length; i++) {
    data[i].start = cumulative;
    cumulative += data[i].value;
    data[i].end = cumulative;

    data[i].class = (data[i].value >= 0) ? 'positive' : 'negative'
}
data.push({
    name: 'Total',
    end: cumulative,
    start: 0,
    class: 'total'
});

x.domain(data.map(function(d) {
    return d.name;
}));
y.domain([0, d3.max(data, function(d) {
    return d.end;
})]);

chart.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis);

var ticks = d3.selectAll(".x.axis text").each(function(d, i) {
    if (i < 3) {
        d3.select(this).attr("y", 0)
        d3.select(this).attr("x", 10)
        d3.select(this).attr("dy", ".35em")
        d3.select(this).attr("transform", "rotate(90)")
        d3.select(this).style("text-anchor", "start");
    }
});

chart.append("g")
    .attr("class", "y axis")
    .call(yAxis);

var bar = chart.selectAll(".bar")
    .data(data)
    .enter().append("g")
    .attr("class", function(d) {
        return "bar " + d.class
    })
    .attr("transform", function(d) {
        return "translate(" + x(d.name) + ",0)";
    });

bar.append("rect")
    .attr("y", function(d) {
        return y(Math.max(d.start, d.end));
    })
    .attr("height", function(d) {
        return Math.abs(y(d.start) - y(d.end));
    })
    .attr("width", x.rangeBand());

bar.append("text")
    .attr("x", x.rangeBand() / 2)
    .attr("y", function(d) {
        return y(d.end) + 5;
    })
    .attr("dy", function(d) {
        return ((d.class == 'negative') ? '-' : '') + ".75em"
    })
    .text(function(d) {
        return dollarFormatter(d.end - d.start);
    });


bar.filter(function(d) {
        return d.class != "total"
    }).append("line")
    .attr("class", "connector")
    .attr("x1", x.rangeBand() + 5)
    .attr("y1", function(d) {
        return y(d.end)
    })
    .attr("x2", x.rangeBand() / (1 - padding) - 5)
    .attr("y2", function(d) {
        return y(d.end)
    })
    //});

function type(d) {
    d.value = +d.value;
    return d;
}

function dollarFormatter(n) {
    n = Math.round(n);
    var result = n;
    if (Math.abs(n) > 1000) {
        result = Math.round(n / 1000) + 'K';
    }
    return result;
}
.bar.total rect {
  fill: steelblue;
}

.bar.positive rect {
  fill: darkolivegreen;
}
.bar.negative rect {
  fill: crimson;
}

.bar line.connector {
  stroke: grey;
  stroke-dasharray: 3;
}

.bar text {
  fill: white;
  font: 10px sans-serif;
  text-anchor: middle;
}

.axis text {
  font: 10px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}
<script src="http://d3js.org/d3.v3.min.js"></script>
<svg class="chart"></svg>

Comments