Thomas Kremmel
Thomas Kremmel

Reputation: 14783

d3.js - update axis and change width of path

I'm trying to update the width of my axis like so:

Initial setup with zero width:

var x2 = d3.scale.linear()
        .domain([0, 10])
        .range([0, 0])
        .clamp(true);

svg.append("g")
        .attr("class", "x axis2")
        .attr("transform", "translate(0," + height / 2 + ")")
        .call(d3.svg.axis()
          .scale(x2)
          .orient("bottom")
          .tickFormat(function(d) { return d; })
          .tickSize(0)
          .tickPadding(12))
      .select(".domain")
      .select(function() { return this.parentNode.appendChild(this.cloneNode(true)); })
        .attr("class", "bar-highlight");

Later I want to update the width of the path (with a nice transition would be great). I tried it like suggested in this answer, with no luck:

x2 = d3.scale.linear()
        .domain([0, data.page_count])
        .range([0, 15])
        .clamp(true);

d3.selectAll("g.x.axis2")
        .call(x2);

Upvotes: 0

Views: 1054

Answers (1)

Michael
Michael

Reputation: 1467

You need to call d3.svg.axis().scale(x2) on the axis, not just x2. The following update procedure works for me (albeit on an empty plot):

// Update the range of existing variable, x2
x2.range([0, 15]);
// Select and change the axis in D3
d3.selectAll("g.x.axis2")
    .call(d3.svg.axis().scale(x2));

Upvotes: 1

Related Questions