Display Name
Display Name

Reputation: 337

d3 bar chart with custom x axis and bar width

I want to create a bar chart with custom bar width I tried following code but not aware if its the right way to do.

Also I want to update the bar chart with new data how can I do it?

TO update I tried - https://jsfiddle.net/eqr8deef/

var margin = {
    top: 25,
    right: 40,
    bottom: 35,
    left: 85
  },
  w = 500 - margin.left - margin.right,
  h = 350 - margin.top - margin.bottom;
var padding = 10;

var colors = {
  0: ["Local", "#377EB8"],
  1: ["Global", "#4DAF4A"]
};

var dataset = [{
  "global": 1468604556084,
  "local": 100,
}, {
  "local": 11500,
  "global": 1313048950629
}, {
  "local": 11500,
  "global": 1213048950629
}, {
  "local": 11500,
  "global": 1113048950629
}, {
  "local": 11500,
  "global": 1123048950629
}, {
  "local": 11500,
  "global": 1013048950629
}];

var xScale = d3.scale.ordinal()
  .domain(d3.range(dataset.length))
  .rangeRoundBands([0, w], 0.01);
// ternary operator to determine if global or local has a larger scale
var yScale = d3.scale.linear()
  .domain([0, d3.max(dataset, function(d) {
    return d.local;
  })])
  .range([h, 0]);
var yAxis = d3.svg.axis()
  .scale(yScale)
  .orient("left")
  .ticks(5);


var commaFormat = d3.format(',');

//SVG element
var svg = d3.select("#searchVolume")
  .append("svg")
  .attr("width", w + margin.left + margin.right)
  .attr("height", h + margin.top + margin.bottom)
  .append("g")
  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

// Graph Bars
var sets = svg.selectAll(".set")
  .data(dataset)
  .enter()
  .append("g")
  .attr("class", "set")
  .attr("transform", function(d, i) {
    return "translate(" + xScale(i) + ",0)";
  });

sets.append("rect")
  .attr("class", "global")
  .attr("width", 20)
  .attr("y", function(d) {
    return yScale(d.local);
  })
  .attr("height", function(d) {
    return h - yScale(d.local);
  })
  .attr("fill", colors[1][1])
  .attr("font-family", "sans-serif")
  .attr("font-size", "11px")
  .attr("fill", "red");

// yAxis
svg.append("g")
  .attr("class", "y axis")
  .attr("transform", "translate(0 ,0)")
  .call(yAxis);

  var yTextPadding = 20;

svg.selectAll(".bartext")
.data(dataset)
.enter()
.append("text")
.attr("class", "bartext")
.attr("text-anchor", "middle")
.attr("fill", "black")
.attr("x", function(d,i) {
    console.log(i, xScale(i))
    return xScale(i) + 10;
})
.attr("y", function(d,i) {
    return h + 15;
})
.text(function(d){
     return new Date(d.global).getFullYear();
});
// xAxis label

http://jsfiddle.net/pq0xrard/

Upvotes: 2

Views: 4617

Answers (2)

Deepak Ingole
Deepak Ingole

Reputation: 15732

To answer your question step by step -

  • rangeRoundBands is used to evenly space your bars. But if you want to have custom width then you can not use it like the way you are using it.

  • to update the data you can simply use enter-update-exit methods as shown below.

var update_sel = svg.selectAll("circle").data(data)

update_sel.attr(/* operate on old elements only */)

update_sel.enter().append("circle").attr(/* operate on new elements only */)

update_sel.attr(/* operate on old and new elements */)

update_sel.exit().remove() /* complete the enter-update-exit pattern */

Here is a complete example - https://jsfiddle.net/seej4dfd/

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

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")
  .ticks(10, "%");

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

svg.append("g")
  .attr("class", "x axis")
  .attr("transform", "translate(0," + height + ")")

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


  var labels = svg
    .selectAll(".topLabel")
  .data(data, function(d) {
      return d.letter;
    });

labels
.exit()
.remove();

labels
  .enter()
  .append("text")
  .attr("class", "topLabel")
  .attr("text-anchor", "middle")
  .attr("fill", "black")

  labels
  .attr("x", function(d, i) {
    return x(d.letter) + 7.5;
  })
  .attr("y", function(d, i) {
    return y(d.frequency);
  })
  .text(function(d, i) {
    return d.letter;
  });


var labels = svg
    .selectAll(".bartext")
  .data(data, function(d) {
      return d.letter;
    });

labels
.exit()
.remove();

labels
  .enter()
  .append("text")
  .attr("class", "bartext")
  .attr("text-anchor", "middle")
  .attr("fill", "black");

  labels
  .attr("x", function(d, i) {
    return x(d.letter) + 7.5;
  })
  .attr("y", function(d, i) {
    return height + 15;
  })
  .text(function(d, i) {
    return d.letter;
  });

  svg.select(".y.axis").transition().duration(300).call(yAxis)

  var bars = svg.selectAll(".bar").data(data, function(d) {
      return d.letter;
    })
  bars.exit()
    .transition()
    .duration(300)
    .remove();

  bars.enter().append("rect")
    .attr("class", "bar");

  bars.transition().duration(300).attr("x", function(d) {
      return x(d.letter);
    })
    .attr("width", 15)
    .attr("y", function(d) {
      return y(d.frequency);
    })
    .attr("height", function(d) {
      return height - y(d.frequency);
    });
}

Upvotes: 2

Shan Carter
Shan Carter

Reputation: 2455

To change the width, use the xScale.rangeBand() for setting the width of your rect on line 73.

http://jsfiddle.net/073u0ump/3/

Upvotes: -1

Related Questions