d3条形图中的自定义x轴标签?

时间:2017-01-10 22:28:22

标签: javascript d3.js graph data-visualization

我是d3的新手,我正在尝试根据某些操作组织我的数据。我查看了此处的其他帖子并遵循了类似的答案(请参阅herehere),但由于某种原因,x轴未显示我在var workflow_actions = ["SB","SE","DR","RD","EN","RE"];中创建的顺序而是是一个不同的顺序。

也许最好的方法是使用序数尺度代替?见D3 API

我认为问题出在这一行:x.domain(data.map(function(d) { return d.phase; }));

这是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);

这是我的代码:

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>

1 个答案:

答案 0 :(得分:1)

由于您的workflow_actions的某些值与比例域不同,因此您不希望tickValues在此处,tickFormat而是:

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

这是一个带有比例域和workflow_actions数组的演示:

&#13;
&#13;
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);
&#13;
.axis path, .axis line {
    fill: none;
    stroke: black;
    shape-rendering: crispEdges;
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<svg></svg>
&#13;
&#13;
&#13;

编辑:上述答案解决了原始问题,其中workflow_actions与域名不同。要了解OP的实际问题,请阅读以下评论。