d3 multiple y axis for multiple bar chart

devi

I have built a chart like this example

http://bl.ocks.org/mbostock/4679202

here I would like to have y-axis labels for each series

the below code is my code to generate graph

since its a stacked layout. it feels a little complex for me imagine the y-axis transform attribute for each series

var margin = {top: 10, right: 5, bottom: -6, left: 5};

    var width = $(div).width(), 
    height = $(div).height() - margin.top - margin.bottom;

var svg = d3.select(div).append('svg')
      .attr('width',width)
      .attr('height',height)
      .append("g")
              .attr("transform", "translate("+margin.left+"," + margin.top + ")");;

var nest = d3.nest()
    .key(function(d) { return d.group; });

var stack = d3.layout.stack()
    .values(function(d) { return d.values; })
    .x(function(d) { return d.date; })
    .y(function(d) { return d.value; })
    .out(function(d, y0) { d.valueOffset = y0; });

var dataByGroup = nest.entries(data);

stack(dataByGroup);

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

var y0 = d3.scale.ordinal()
        .rangeRoundBands([height-15, 0], .2);

var timeFormat = d3.time.format("%c");

var MinuteNameFormat = d3.time.format("%H:%M");
var y1 = d3.scale.linear();
    var formatDate = function(d,i) {if(i%3 == 0){var date = new Date(parseInt(d)); return MinuteNameFormat(date);}else{return "";} };
var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .ticks(d3.time.minutes,15)
    .tickFormat(formatDate);

var yAxis = d3.svg.axis()
.scale(y1)
.orient("Left")
    .ticks(0)
    .tickFormat(formalLabel);

x.domain(dataByGroup[0].values.map(function(d) { return d.date; }));
y0.domain(dataByGroup.map(function(d) { return d.key; }));
y1.domain([0, d3.max(data, function(d) { return d.value; })]).range([y0.rangeBand(), 0]);



  var tooltip = d3.select(div)
.append("div")
.style("position", "absolute")
.style("z-index", "10").attr("class","tooltip")
.style("visibility", "hidden")
.style("background","#fff")
.style("border","1px solid #CCC")
.style("font-size", "11px")
.style("padding","11px")
.text("a simple tooltip");


 var group = svg.selectAll(".group")
    .data(dataByGroup)
    .enter().append("g")
    .attr("class", "group")
    .attr("transform", function(d) { return "translate(0," + y0(d.key) + ")"; });

 group.filter(function(d, i) { return !i; }).append("g")
    .attr("class", "y axis")
    .attr("transform", "translate(0," + y0.rangeBand() + ")")

    .call(yAxis);
     var tipText
  var bars = group.selectAll("rect")
  .data(function(d) { return d.values; })
  .enter().append("g");

bars.append("rect")
  .style("fill", function(d,i) { return getColor(d.ip); })
  .attr("x", function(d) { return x(d.date); })
  .attr("y", function(d) { return y1(d.value); })
  .attr("width",x.rangeBand()) //x.rangeBand()
  .attr("height", function(d) { return y0.rangeBand() - y1(d.value); });

    group.filter(function(d, i) { return !i; }).append("g")
  .attr("class", "x axis")
  .attr("transform", "translate(0," + y0.rangeBand() + ")")

  .call(xAxis);

this is the code for my d3 graph

can anyone suggest ??

link

This is entirely possible, but it would clutter you graph if you have more than 2 y-axis, one on the left side and one on the right.

Here is an example: http://bl.ocks.org/benjchristensen/2579619

You declare an axis with this code:

var yAxis = d3.svg.axis()
.scale(y1)
.orient("Left")
    .ticks(0)
    .tickFormat(formalLabel);

You just need to put 1 of this for each element, and you also need to declare a new y1,y2,y3... etc for each element. And finally you need to make sure each data is bound to a different axis y1,y2,y3... etc

Hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related