admin管理员组文章数量:1404573
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
2 Answers
Reset to default 5Here'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.
本文标签: javascriptChanging size of D3 tick marks based on tick valueStack Overflow
版权声明:本文标题:javascript - Changing size of D3 tick marks based on tick value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744841860a2627971.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论