gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 05:16:34 25 4
gpt4 key购买 nike

我是 d3 的新手,我正在尝试根据某些操作来组织我的数据。我查看了此处的其他帖子并遵循了类似的答案(请参阅 herehere ),但由于某种原因,x 轴没有显示我在 varworkflow_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>

最佳答案

由于您的 workflow_actions 具有一些与比例域不同的值,因此您不需要在此处使用 tickValues,而是使用 tickFormat:

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

这是一个演示,其中包含比例域和 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>

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

关于javascript - d3 条形图中的自定义 x 轴标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41579560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com