[英]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. 我是d3的新手,我正在尝试根据一些操作来整理数据。 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"];
我查看了此处的其他帖子,并遵循了类似的答案(请参见此处和此处 ),但是由于某些原因,x轴未显示我在
var workflow_actions = ["SB","SE","DR","RD","EN","RE"];
创建的顺序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
请参阅D3 API
I believe the issue is on this line: x.domain(data.map(function(d) { return d.phase; }));
我认为问题出在这条线上:
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: 由于您的
workflow_actions
具有一些与标度的域不同的值,因此您不需要在此处使用tickValues
,而是使用tickFormat
:
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: 这是一个演示,具有秤的域和
workflow_actions
数组:
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. 编辑:上面的答案解决了原始的问题,其中
workflow_actions
不同于域。 To know the OP's actual problem, read the comments below. 要了解OP的实际问题,请阅读以下注释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.