简体   繁体   中英

Custom x-axis labels in d3 bar charts?

I am new to d3 and I am trying to organize my data based on some actions. I looked at other posts on here and followed similar answers (see here and here ), but for some reason, the x-axis is not displaying the order I created in var workflow_actions = ["SB","SE","DR","RD","EN","RE"]; instead it is in a different order.

Perhaps the best method is to use ordinal scales instead? See D3 API

I believe the issue is on this line: x.domain(data.map(function(d) { return d.phase; }));

This was the suggestion per Stack Overflow:

var workflow_actions = ["SB","SE","DR","RD","EN","RE"];
var formatAction = function(d) {
    return workflow_actions[d % 6];
}

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

Here is my code:

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

var x = d3.scale.ordinal()
    .domain([SB","SE","DR","RD","EN","RE"]) # just added based on API
    .rangeRoundBands([0, width], .1);

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

var workflow_actions = ["SB","SE","DR","RD","EN","RE"];
var formatAction = function(d) {
    return workflow_actions[d % 6];
}

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

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left");

var tipLabelMap = {
  #labels
};

var tip = d3.tip()
  .attr('class', 'd3-tip')
  .offset([-10, 0])
  .html(function(d) {
    return "<strong>" + getTipLabel(d.phase) + ":</strong> <span style='color:red'>" + d.count + "</span>";
  })

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.call(tip);

d3.json("mydata", function(error, data) {

  x.domain(data.map(function(d) { return d.phase; })); ## ISSUE!!!!
  y.domain([0, d3.max(data, function(d) { return d.count; })]);

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

  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
    .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("Count");

  svg.selectAll(".bar")
      .data(data)
    .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function(d) { return x(d.phase); })
      .attr("width", x.rangeBand())
      .attr("y", function(d) { return y(d.count); })
      .attr("height", function(d) { return height - y(d.count); })
      .on('mouseover', tip.show)
      .on('mouseout', tip.hide)
});

function getTipLabel(phase) {
    return tipLabelMap[phase];
}
</script>

Since your workflow_actions has some values which are different from the scale's domain, you don't want tickValues here, but tickFormat instead:

var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .tickFormat((d,i) => workflow_actions[i]);

Here is a demo, with the scale's domain and the workflow_actions array:

 var svg = d3.select("svg"); var x = d3.scale.ordinal() .domain(["SU","SI","SE","CD","EN","RE"]) .rangeRoundBands([20, 280], .1); var workflow_actions = ["SB","SE","DR","RD","EN","RE"]; var xAxis = d3.svg.axis() .scale(x) .orient("bottom") .tickFormat((d,i)=>workflow_actions[i]); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0,130)") .call(xAxis); 
 .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> <svg></svg> 

EDIT: the above answer addresses the original question, where the workflow_actions was different from the domain. To know the OP's actual problem, read the comments below.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM