score:3
Accepted answer
you just need to save the group element containing x-axis into a variable and use as shown below.
var wrap = function() {
var self = d3.select(this),
textlength = self.node().getcomputedtextlength(),
text = self.text();
while (textlength > (50) && text.length > 0) {
text = text.slice(0, -1);
self.text(text + '...');
textlength = self.node().getcomputedtextlength();
}
};
//calling the function
xaxis.selectall('text')
.style('text-anchor', 'end')
.attr('fill', '#8a9299')
.attr('transform', 'rotate(-60)')
.each(wrap);
// set the dimensions and margins of the graph
var margin = {
top: 20,
right: 20,
bottom: 30,
left: 40
},
width = 250 - margin.left - margin.right,
height = 250 - margin.top - margin.bottom;
// set the ranges
var x = d3.scaleband()
.range([0, width])
.padding(0.1);
var y = d3.scalelinear()
.range([height, 0]);
// append the svg object to the body of the page
// append a 'group' element to 'svg'
// moves the 'group' element to the top left margin
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 + ")");
var data = [{
"salesperson": "bob patrickson",
"sales": 33
},
{
"salesperson": "robin dearden",
"sales": 12
},
{
"salesperson": "anne hathaway",
"sales": 41
},
{
"salesperson": "mark wahlberg",
"sales": 16
}
];
// format the data
data.foreach(function(d) {
d.sales = +d.sales;
});
// scale the range of the data in the domains
x.domain(data.map(function(d) {
return d.salesperson;
}));
y.domain([0, d3.max(data, function(d) {
return d.sales;
})]);
// append the rectangles for the bar chart
svg.selectall(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) {
return x(d.salesperson);
})
.attr("width", x.bandwidth())
.attr("y", function(d) {
return y(d.sales);
})
.attr("height", function(d) {
return height - y(d.sales);
});
// add the x axis
var xaxis = svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisbottom(x));
// add the y axis
svg.append("g")
.call(d3.axisleft(y));
var wrap = function() {
var self = d3.select(this),
textlength = self.node().getcomputedtextlength(),
text = self.text();
while (textlength > (50) && text.length > 0) {
text = text.slice(0, -1);
self.text(text + '...');
textlength = self.node().getcomputedtextlength();
}
};
//calling the function
xaxis.selectall('text')
.style('text-anchor', 'end')
.attr('fill', '#8a9299')
.attr('transform', 'rotate(-60)')
.each(wrap);
.bar {
fill: steelblue;
}
<script src="//d3js.org/d3.v4.min.js"></script>
Source: stackoverflow.com
Related Query
- How do i give ellipsis in axis labels in d3 v4?
- How to wrap d3 axis labels in <a> elements?
- How to make the NVD3 discreteBarChart labels on the X axis to adapt to the width or to the number of labels?
- dimple.js How can I change the labels of a chart axis without changing the data?
- How to modify axis labels in d3 for a stacked bar chart when the axis labels are mapped as part of the scale's domain
- d3.js, how can i create an axis with custom labels and customs ticks?
- D3.js implement ellipsis for axis labels SVG
- d3.js how to place custom labels on horizontal log scaled axis
- How to make axis tick labels hyperlinks in D3.js
- How can I add axis labels to a sankey diagram in d3?
- D3.js v7 - How to make Y axis labels always show on screen for a scrollable chart
- How to customize nvd3's tooltips and remove labels from x axis
- How to add svg rectangles as background behind axis tick labels in d3js if ticks are inside chart
- How to Place labels and give color range by number
- How to skip x axis labels in d3.js chart dynamically to have always a pretty chart?
- How to color the text labels individually on an axis in d3?
- d3.js How to draw line chart with vertical x axis labels
- How do you change the default axis labels in D3
- How to limit animated axis labels position in d3?
- How to rotate the text labels for the x Axis of a nvd3.js line chart
- How can i show the labels on the x axis when there is data overlap in d3js?
- how do I labels to the axis in this d3 example
- How to setup D3.js axis labels to show rounded numbers with the $ sign?
- How to give a better Y axis label / scale in d3 chart?
- How to replace X axis numerical values(years) with custom labels in D3.js for this example?
- How to wrap only one word in axis labels
- How to make two sets of axis labels in grouped line graph in dimple.js?
- How do I include newlines in labels in D3 charts?
- How to update axis using d3.js
- d3.js: Align text labels between ticks on the axis
More Query from same tag
- css complicated selector query
- How do you import a user module in D3?
- How do I reset an animation using the d3.js and waypoints.js libraries?
- Mousemove within mousedown stops mouse up firing
- Interactive Legend on force layout with d3.legend
- Append two kind data as texts in opposite dirrections
- how to add and remove css class on svg element
- Integrate a D3.js tree into a angular 4 project
- d3.scale.quantize producing unexpected result, how to debug?
- d3 svg to fill screen
- Can I create a flow chart (no tree chart) using D3.js
- How to make information box and attach it with chart in d3.js
- d3 transition in unit-testing
- D3 Zoom & Pan - How to enable zooming & panning anywhere on the chart and restrict showing circle within the range while panning & zooming
- How can i display the count in a better way?
- d3 arc transition error when toggling sweep-flag
- Very basic syntax error, lines not drawing in d3
- How to transition D3 axis without tick text attribute reset?
- D3.js: What is the (current) standard way of using force layout?
- Issue with multiple d3 graphs on same page
- How to remove shapes from filled SVG path
- D3.JS time-series line chart with real-time data, panning and zooming
- How to filter a featurecollection to an object that can be used with path.bounds()
- rendering d3.js library visualizations for colorbrewer
- Pie is not a function in Ember D3 Pie Chart
- Is there anything like D3's Nest functionality in Objective-C?
- Mouseover events in D3 v4 Chord Diagram
- Dcjs cursor and tooltips
- D3.js Titles on Collapsible Force-Directed graph
- Cannot transform labels on spinning globe