最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Changing size of D3 tick marks based on tick value - Stack Overflow

programmeradmin3浏览0评论

I've created a x-axis with a custom tickFormat in D3.

It prints a tick for each month, but labels January as the year, i.e 2014, 2013, etc.

var xAxis = d3.svg.axis()
    .scale(x)
    .orient('bottom')
    .ticks(d3.time.month, 1)
    .tickFormat(d3.time.format.multi([["%b", function(d) { return d.getMonth(); }], ["%Y", function() { return true; }]]))
    .tickSize(4)
    .tickPadding(8);

I'd like to vary the tickSize as well and have a long tick for the year markers.

But it doesn't appear you can use a function to return a value. This should print a random tick size for each date, but fails:

.tickSize(function(d){ return Math.random() * 50 })

I've created a x-axis with a custom tickFormat in D3.

It prints a tick for each month, but labels January as the year, i.e 2014, 2013, etc.

var xAxis = d3.svg.axis()
    .scale(x)
    .orient('bottom')
    .ticks(d3.time.month, 1)
    .tickFormat(d3.time.format.multi([["%b", function(d) { return d.getMonth(); }], ["%Y", function() { return true; }]]))
    .tickSize(4)
    .tickPadding(8);

I'd like to vary the tickSize as well and have a long tick for the year markers.

But it doesn't appear you can use a function to return a value. This should print a random tick size for each date, but fails:

.tickSize(function(d){ return Math.random() * 50 })
Share Improve this question asked Feb 18, 2014 at 23:49 arm5077arm5077 3023 silver badges16 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

Here's your answer. Custom tick size on axis (d3js)

TLDR: tickSize can only accept numbers, not functions. You need to select them after they are drawn then resize them.

axis.tickSize doesn't accept a function as an argument. It only accepts a scalar value or an array of 2 scaler values (one value for the length of the inner ticks and one for the length of the 2 outer ticks at the end of the domain line).

If you wish to conditionally style ticks you can use post-selection to grab the ticks after they are drawn and either style them directly or set CSS classes to get them to pick up the styles you wish. There is a good example of how to do this here: D3 Axis Tick Post-selection.

发布评论

评论列表(0)

  1. 暂无评论