下面是用于显示轴的代码
var xScale = d3.scale.ordinal()
.rangeRoundBands([0, totalWidth], barSpacing(optionsConfig.chart.barSpacing))
.domain(chartData.map(function(d) {
return d[xValue];
}));
// Assign Scale to X Axis
xAxis.scale(xScale);
Run Code Online (Sandbox Code Playgroud)
如果文本太长,我想在 x 轴上显示省略号我指的是此链接,其中包含以下解决方案
function wrap() {
var self = d3.select(this),
textLength = self.node().getComputedTextLength(),
text = self.text();
while (textLength > (width - 2 * padding) && text.length > 0) {
text = text.slice(0, -1);
self.text(text + '...');
textLength = self.node().getComputedTextLength();
}
}
text.append('tspan').text(function(d) { return d.name; }).each(wrap);
Run Code Online (Sandbox Code Playgroud)
但是任何人都可以帮助我如何实现上述代码,因为我是 d3.js 的新手,或者请建议是否可以使用 CSS 或 SVG 方法来完成。
试试这个方法。
function wrap() {
var self = d3.select(this),
textLength = self.node().getComputedTextLength(),
text = self.text();
while (textLength > (barWidth - 2 * padding) && text.length > 0) {
text = text.slice(0, -1);
self.text(text + '...');
textLength = self.node().getComputedTextLength();
}
}
var padding = 0, barWidth = x.bandwidth();
var xAxis = d3.select(".axis--x"); //Selector should be the class given to the x axis
xAxis.selectAll(".tick")
.selectAll("text")
.html("")
.append('tspan').text(function(d) {
return d;
}).each(wrap);
Run Code Online (Sandbox Code Playgroud)
function wrap() {
var self = d3.select(this),
textLength = self.node().getComputedTextLength(),
text = self.text();
while (textLength > (barWidth - 2 * padding) && text.length > 0) {
text = text.slice(0, -1);
self.text(text + '...');
textLength = self.node().getComputedTextLength();
}
}
var padding = 0, barWidth = x.bandwidth();
var xAxis = d3.select(".axis--x"); //Selector should be the class given to the x axis
xAxis.selectAll(".tick")
.selectAll("text")
.html("")
.append('tspan').text(function(d) {
return d;
}).each(wrap);
Run Code Online (Sandbox Code Playgroud)
var svg = d3.select("svg"),
margin = {
top: 20,
right: 20,
bottom: 30,
left: 40
},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
var x = d3.scaleBand().rangeRound([0, width]).padding(0.1),
y = d3.scaleLinear().rangeRound([height, 0]);
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var data = [{
"letter": "Abcdeafg Abcdeafg",
"frequency": 0.08167
}, {
"letter": "Bcdefagh Bcdefagh",
"frequency": 0.01492
}, {
"letter": "Cdefgahi Cdefgahi",
"frequency": 0.02782
}, {
"letter": "Defghaij Defghaij",
"frequency": 0.04253
}, {
"letter": "Efghaijk Efghaijk",
"frequency": 0.12702
}, {
"letter": "Fghijakl Fghijakl",
"frequency": 0.02288
}, {
"letter": "Ghijaklm Ghijaklm",
"frequency": 0.02015
}, {
"letter": "Hijklman Hijklman",
"frequency": 0.06094
}, {
"letter": "Ijklmnao Ijklmnao",
"frequency": 0.06966
}, {
"letter": "Jklmnoap Jklmnoap",
"frequency": 0.00153
}, {
"letter": "Klmnopqa Klmnopqa",
"frequency": 0.00772
}, {
"letter": "Lmnopqar Lmnopqar",
"frequency": 0.04025
}, {
"letter": "Mnopqrsa Mnopqrsa",
"frequency": 0.02406
}, {
"letter": "Nopqrsta Nopqrsta",
"frequency": 0.06749
}, {
"letter": "Opqrstua Opqrstua",
"frequency": 0.07507
}, {
"letter": "Pqrstuva Pqrstuva",
"frequency": 0.01929
}, {
"letter": "Qrstuvwa Qrstuvwa",
"frequency": 0.00095
}, {
"letter": "Rstuvwxya Rstuvwxya",
"frequency": 0.05987
}];
data.forEach(function(d) {
d.frequency = +d.frequency;
return d;
});
x.domain(data.map(function(d) {
return d.letter;
}));
y.domain([0, d3.max(data, function(d) {
return d.frequency;
})]);
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y).ticks(10, "%"))
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("Frequency");
g.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) {
return x(d.letter);
})
.attr("y", function(d) {
return y(d.frequency);
})
.attr("width", x.bandwidth())
.attr("height", function(d) {
return height - y(d.frequency);
});
function wrap() {
var self = d3.select(this),
textLength = self.node().getComputedTextLength(),
text = self.text();
while (textLength > (barWidth - 2 * padding) && text.length > 0) {
text = text.slice(0, -1);
self.text(text + '...');
textLength = self.node().getComputedTextLength();
}
}
var padding = 0, barWidth = x.bandwidth();
var xAxis = d3.select(".axis--x");
xAxis.selectAll(".tick")
.selectAll("text")
.html("")
.append('tspan').text(function(d) {
return d;
}).each(wrap);Run Code Online (Sandbox Code Playgroud)
.bar {
fill: steelblue;
}
.bar:hover {
fill: brown;
}
.axis--x path {
display: none;
}Run Code Online (Sandbox Code Playgroud)