Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3.js Specific Tick Styling

Is there a way to style a specific tick say if I was using x [0, 100] and y [0,100] and I wanted to just draw or style the tick for (0,50) and (50,0)?

like image 410
Gthiet Avatar asked Feb 21 '14 16:02

Gthiet


People also ask

How do I get the value of a tick in D3?

The d3.axis.tickValues () Function in D3.js is used to generate ticks at specific values. This function returns the current tick values, which defaults to null. Parameters: This function accepts the following parameters. values: This parameter is used for ticks rather than using the scale’s automatic tick generator

What are the parameters of the tick function?

Parameters: This function accepts the following parameters. values: This parameter is used for ticks rather than using the scale’s automatic tick generator Return Value: This function returns ticks at specific values. Note: The explicit tick values take precedent over the tick arguments set by axis.tickArguments.

How does D3 create the Axis?

Inspecting the axis we created, using your browser inspector, gives us good insight on how D3.js constructs the axis: You can see that d3 creates the axis as a path with the class “domain”, and all the ticks are groups with the class “tick”. Knowing this allows you to select these elements and manipulate them with CSS and JavaScript.

What is the difference between explicit tick and tick arguments?

Note: The explicit tick values take precedent over the tick arguments set by axis.tickArguments. Below programs illustrate the d3.axis.tickValues () function in D3.js:


1 Answers

Ticks created by the d3 axis functions have the tick value bound to it as a data object. After you draw an axis, you can re-select the ticks and manipulate them based on their data.

For example, you can filter the selection to only find the ticks with a specific value. Then you can apply styles or classes the same as for any other d3 selection.

d3.selectAll('g.tick')
  .filter(function(d){ return d==50;} )
   //only ticks that returned true for the filter will be included
   //in the rest of the method calls:
  .select('line') //grab the tick line
  .attr('class', 'quadrantBorder') //style with a custom class and CSS
  .style('stroke-width', 5); //or style directly with attributes or inline styles

More explanation and another example, using a data-based function in the attribute call instead of using a filter:
https://stackoverflow.com/a/21583985/3128209

like image 189
AmeliaBR Avatar answered Oct 13 '22 22:10

AmeliaBR