gpt4 book ai didi

javascript - 使用d3 js的水平条形图

转载 作者:行者123 更新时间:2023-11-30 11:53:18 24 4
gpt4 key购买 nike

我正在使用 d3.js 绘制条形图。我想在两个输入值之间显示条形图,但在 x 轴上以字符串值而不是整数值的形式显示。

[![我正在努力实现的一个例子][1]][1]

var y-axis= ['配件', '发烧友', '相机和照片', '手机', '电脑', '电子书阅读器'];

var x-axis= ['开发', '设计', '测试', 'UAT', 'LiveDeployment'];

我用过的引用网址是[D3 js简单水平条形图][2]

代码包含以下功能:1) 使用 D3 js 和自定义 x 轴标签的水平条2) 将 x 轴线显示为文本换行:(01/01/2016 生产开始日期)3) 将 y 轴线显示为文本换行:(Category 1Category 1Category 1Category 1)

function wrap(text, width) {
text.each(function() {
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = 0,
lineHeight = 1.1, // ems
y = text.attr("y"),
dy = parseFloat(text.attr("dy")),
tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
while (word = words.pop()) {
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > width) {
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
}
}
});
}

data = [
{label:"Category 1Category 1Category 1", value:25},
{label:"Category 2", value:50},
{label:"Category 3", value:100},
{label:"Category 4", value:75},
{label:"Category 5", value:40},
{label:"Category 6", value:20}
];

var axisMargin = 10,
margin = 20,
valueMargin = 2,
width = parseInt(d3.select('#barchart').style('width'), 9),
height = parseInt(d3.select('body').style('height'), 9),
barHeight = (height-axisMargin-margin*2)* 0.6/data.length,
barPadding = (height-axisMargin-margin*2)*0.4/data.length,
data, bar, svg, scale, scale2, xAxis, labelWidth = 0;

max = d3.max(data, function(d) { return d.value; });

svg = d3.select('#barchart')
.append("svg")
.attr("width", width)
.attr("height", height);


bar = svg.selectAll("g")
.data(data)
.enter()
.append("g");

bar.attr("class", "bar")
.attr("cx",0)
.attr("transform", function(d, i) {
return "translate(" + margin + "," + (i * (barHeight + barPadding) + barPadding) + ")";
});

bar.append("text")
.attr("class", "label")
.attr("y", barHeight / 2)
.attr("dy", ".35em") //vertical align middle
.text(function(d){
return d.label;
}).each(function() {
labelWidth = 150; //Math.ceil(Math.max(labelWidth, this.getBBox().width));
})
.call(wrap, 150);


scale = d3.scale.linear()
.domain([0, max])
.range([0, width - margin*2 - labelWidth]);

/*scale = d3.scale.linear()
.domain([0, max])
.range([0, width - margin*2 - labelWidth]);

xAxis = d3.svg.axis()
.scale(scale)
.tickSize(-height + 2*margin + axisMargin);
.orient("bottom");*/

var scale2 = d3.scale.ordinal()
.domain(["01/01/2016 Production Start Date", "01/01/2016 Production End Date", "Transcoding", "Encryption", "Published", "Published Release Date"])
.rangePoints([0, width - margin*2 - labelWidth]);

var xAxis = d3.svg.axis()
.scale(scale2)
.tickSize(-height + 2*margin + axisMargin)
.orient("bottom");

bar.append("rect")
.attr("transform", "translate("+labelWidth+", 0)")
.attr("height", barHeight)
.attr("width", function(d){
return scale(d.value);
});

bar.append("text")
.attr("class", "value")
.attr("y", barHeight / 2)
.attr("dx", -valueMargin + labelWidth) //margin right
.attr("dy", ".35em") //vertical align middle
.attr("text-anchor", "end")
.text(function(d){
return (d.value+"%");
})
.attr("x", function(d){
var width = this.getBBox().width;
return Math.max(width + valueMargin, scale(d.value));
});

svg.insert("g",":first-child")
.attr("class", "axisHorizontal")
.attr("transform", "translate(" + (margin + labelWidth) + ","+ (height - axisMargin - margin)+")")
.call(xAxis)
.selectAll(".tick text")
.call(wrap, 100);

最佳答案

您当然可以设置刻度的格式,但有一个简单的解决方法:为刻度创建另一个刻度,并为您的 x 轴调用此替代刻度:

var xScaleString = d3.scale.ordinal()
.domain(["design", "development", "testing", "UAT"])
.rangePoints([0, width]);

使用这种不同寻常的方法的优势在于,您可以按照自己的方式沿 x 轴展开字符串。缺点是您可能无法完美匹配原始比例的值(10、20、30 等)。

请记住保持原始比例(这是用于矩形的比例)。

关于javascript - 使用d3 js的水平条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38821800/

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