score:53
i ended up with one of the lars kotthoff's advices.
every time when i call(axis)
i also adjust text labels.
here is simplified code:
function renderaxis() {
axiscontainer
.transition().duration(300)
.call(axis) // draw the standart d3 axis
.call(adjusttextlabels); // adjusts text labels on the axis
}
function adjusttextlabels(selection) {
selection.selectall('.major text')
.attr('transform', 'translate(' + daystopixels(1) / 2 + ',0)');
}
// calculate the width of the days in the timescale
function daystopixels(days, timescale) {
var d1 = new date();
timescale || (timescale = global.timescale);
return timescale(d3.time.day.offset(d1, days)) - timescale(d1);
}
update:
btw, here is a calendar demo with i ended up: http://bl.ocks.org/oluckyman/6199145
score:0
i often do this by stacking multiple axes, each with a custom .tickformat()
.
if i'm placing labels in between dates, i'll often do something like this:
@timedaysaxislabels = d3.svg.axis()
.scale(@timescale)
.orient('bottom')
.ticks(d3.time.hour.utc, 12) # i want ticks at noon, easiest to just get them ever 12 hours
.tickformat((d) =>
# only draw labels at noon, between the date boundaries
if d.getutchours() == 12
# draw the label!
formatter = d3.time.format.utc('%a %d %b') # "mon 12 apr"
return formatter(d)
else
# not noon, don't draw anything
return null)
.ticksize(0)
.tickpadding(30)
i'll also create a separate axis with no labels at all, and a non-zero .ticksize()
to actually draw ticks, but this block above positions date labels in the center of the "column".
score:0
svg.append("g")
.attr("class", "axis axis-years")
.attr("transform", "translate(0," + (height + 1) + ")")
.call(xaxis)
.selectall("text")
.attr("x", "-1.8em")
.attr("y", ".00em")
.attr("transform", function (d) {
return "rotate(-90)"});
score:1
already a few good replies but just to add one more. note the use of text-anchor
.
same idea: after your call, select the text, reposition.
.call(xaxis)
.selectall(".tick text")
.style("text-anchor", "start")
.attr("x", axistextadjust)
score:2
you can do this by using axis.ticksize(major[[,minor],end])
and .ticksubdivide()
. your ticks are set to line up with the major ticks, but if you set the height of these ticks to 0, and set some height for minor ticks, and specify that there is one minor tick between each pair of major ticks, you will end up with tick labels between your ticks. your code would look like this:
var myaxis = d3.svg.axis()
.ticks(15)
.ticksubdivide(1)
.ticksize(0, 6, 0);
note that you need to explicitly set an end size. if you only provide two numbers, they will be interpreted as major
and end
and minor
will default to 0.
here's a fiddle.
score:3
you might want to consider using d3fc, which has a drop-in replacement for the d3 axis component that supports this feature.
here's an example which substitutes the d3 axis d3.axisbottom
, for the d3fc equivalent fc.axisbottom
:
const axis = fc.axisbottom(linear)
.tickcenterlabel(true);
the tickcenterlabel
centres the axis labels as requested.
here's what the axis looks like with tickcenterlabel = false
:
and here with the tickcenterlabel = true
:
full disclosure - i'm a maintainer and contributor to d3fc
score:7
there is no easy (i.e. built-in) way of doing this, but you can still achieve it. there are a few options. the most straightforward one is probably to use the tickformat
function to specify a format with a suitable number of spaces in front/after the numbers. this would need to be hand-tuned for each application though.
alternatively, you could select the label elements after they have been drawn and add a suitable transform
attribute that shifts them accordingly. again, this would have to be hand-tuned.
your third option is to have two different axes, one for the ticks and one for the labels. the idea is that the axis that provides the ticks has no labels and the other one no ticks. you would need to set the tick values appropriately, but at least you wouldn't have to guess the right offset.
Source: stackoverflow.com
Related Query
- d3.js: Align text labels between ticks on the axis
- Place axis text labels between ticks
- D3 align text on left side of the axis from the right most character
- How to color the text labels individually on an axis in d3?
- Add a space between the axis and text with NVD3 charts
- How to rotate the text labels for the x Axis of a nvd3.js line chart
- D3 align text along the right axis
- With text labels for ticks in linear scale, zoom causes lables to extend beyond axis
- Changing text of the ticks in the axis
- How can I get the D3.js axis ticks and positions as an array?
- D3 grouped bar chart: How to rotate the text of x axis ticks?
- D3 line chart axis text labels in multi line
- Labels / text on the nodes of a D3 force directed graph
- Multiline svg text axis ticks in d3
- Is it possible to find the distance between ticks in D3.js?
- Ticks only at the start and end of an axis
- Data points and ticks in the scaleBand axis are not aligned
- How do I hide the text labels in d3 when the nodes are too small?
- Display only values from the data set into X axis ticks
- 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
- Add text to outer ticks of a D3 axis
- adding input text box on clicking the data labels d3.js
- Limit number of Y axis ticks by keeping a top tick above the bar in d3
- Why are the labels on my D3 axis inconsistent (e.g. month, day)?
- D3.js rotate axis labels around the middle point
- D3.JS trying to format the text in first tick of axis
- trying to get the circles next to the y axis labels in bar chart d3.js
- Is it possible to make axis ticks with appended text to have dynamic padding in d3?
More Query from same tag
- How to dynamically set distance length of connected nodes?
- D3 Zoom and Ionic popover
- Convert from hive style plot to draw axes paralelly
- D3 Apply projection to d3-tip tooltip
- d3.js radial line counterclockwise
- Not able to use symbols in d3 chart
- d3.js simple link using source/targets
- JS Equivalent of Linking to a Font File
- Forced-Directed Graph and localstorage
- Why is my choropleth coming out all the same colour?
- Adding Error Bars to Grouped Bar Chart with D3.js
- Brush.extent is returning wrong yMax and yMin values
- running D3 code on DOM ready without jquery
- Jest encountered an unexpected token This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript
- D3 and Leaflet - svg circles not showing
- nvd3 multibar chart state incorrectly updating on legend toggle?
- D3.js - Stacked Barchart from JSON?
- d3 Scale rect width based on window size
- dc.js linechart not showing anything
- angularjs url together with django framework
- d3.js enter() update() exit() pattern, but with custom comparison of key value pairs
- d3js chart with crosshair as tooltip: How to Add 2 lines which intersect at cursor position
- D3 - Uncaught TypeError: Cannot call method 'text' of undefined
- SVG text is poor quality?
- d3.scale.linear() returns NaN
- D3.js v4+ : truncate text to fit in fixed space
- How do I update bound data in d3
- D3 - dragging multiple elements across groups
- How do I console.log an attribute in a dropdown menu?
- How to change D3js Gauge chart color, using update function?