Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tick marks on both sides of axis on d3 scale

I'm trying to have the tick marks show up on both sides of the y axis. As the code is shown below, I'm able to extend the tick marks based on length -width which draws the tick mark from a starting point from left to right.

Is it possible to move the starting point of the tick mark further to the left?

My intent is aesthetics, but to have the tick values be directly over a length of the tick marks.

I have a grid set up, made up of container-length tick marks:

// Axis
var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .tickSize(-height);

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left")
    .tickSize(-width)
    .tickFormat(d3.format("s"));

Based on these scales:

// Scale
var x = d3.time.scale()
    .range([0, width]);

var y = d3.scale.linear()
    .range([height, 0]);

My axis are appended to the svg like this:

svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis)
    .call(adjustXLabels);

svg.append("g")
    .attr("class", "y axis")
    .call(yAxis)
    .call(adjustYLabels);
like image 631
fhbi Avatar asked Feb 21 '16 16:02

fhbi


People also ask

What is a tick mark on a scale?

Tick marks, an element of gauges in FusionChart Suite XT, are calibration marks on the scale to help interpret readings. There are two types of tick marks: Major Tick Marks – The bigger lines on the radial scale. Minor Tick Marks – The smaller lines interspersed within the major tick marks.

Should tick marks be inside or outside graph?

William S. Cleveland, in his excellent book The Elements of Graphing Data , recommends that tick marks should point outwards 'because ticks that point inward can obscure data'.

What is d3 tick?

ticks should be a format specifier string; see d3. format. If the format specifier does not have a precision, the scale will chose an appropriate precision based on the interval between ticks. This guarantees that each tick has a distinct label, and that each label is formatted concisely but consistently.

How do you add a tick mark in Axis?

Add tick marks on an axisClick Add Chart Element > Axes > More Axis Options. On the Format Axis pane, expand Tick Marks, and then click options for major and minor tick mark types. After you add tick marks, you can change the intervals between the tick marks by changing the value in the Interval between marks box.


1 Answers

I'm a little confused about what you're after. Do you want the tick labels to overlap with the ticks themselves? If so you can select the text after the axis is drawn and then translate the ticks.

See the fiddle here: https://jsfiddle.net/6q3rpw6j/

The key bit is:

svg.append("g")
    .attr("class", "y axis")
    .call(yAxis)
    .selectAll(".tick text")
    .attr("transform", "translate(15,0)");

EDIT

To move the ticks themselves:

svg.append("g")
  .attr("class", "y axis")
  .call(yAxis)
  .selectAll(".tick line")
  .attr("transform", "translate(15,0)");

And to remove the top and bottom end ticks, make sure you set the .outerTickSize(0)

like image 133
James Avatar answered Sep 19 '22 05:09

James