简体   繁体   English

轴上的自定义刻度尺寸(d3js)

[英]Custom tick size on axis (d3js)

I am creating a svg xy-chart in d3.js. 我在d3.js中创建了一个svg xy-chart。 Is it possible to create ticks of different lengths depending on tickValue? 是否可以根据tickValue创建不同长度的刻度?

I have made my own tickFormat function myTickFormat and use that in .tickFormat([format]) and that works fine because [format] is expected to be a function. 我已经创建了自己的tickFormat函数myTickFormat并在.tickFormat([format])使用它,并且工作正常,因为[format]应该是一个函数。 But it is not possible to do the same with .innerTickSize([size]) , which expects a number. 但是不可能对.innerTickSize([size])做同样的.innerTickSize([size]) ,它需要一个数字。

Eg if I want the tick at value 70 to be longer I want to do something like this: 例如,如果我希望值70的刻度更长,我想做这样的事情:

var myTickSize = function(d) {
    if (d === 70) { return 20;}
    return 6;
};

But when I use myTickSize as argument to .innerTickSize() : 但是当我使用myTickSize作为.innerTickSize()参数时:

var yScale = d3.scale.linear();
var yAxis = d3.svg.axis()
               .scale(yScale).orient("left")
               .innerTickSize(myTickSize);

I get an Error: Invalid value for attribute x2="NaN" error for each tick. 我收到错误:每个tick的属性x2 =“NaN”错误的值无效

The tickSize function can only accept a number as argument, not a function, but there are other solutions. tickSize函数只能接受一个数字作为参数,而不是函数,但还有其他解决方案。

The easiest approach? 最简单的方法? After the axis is drawn, select all the tick lines and resize them according to their data value. 绘制轴后,选择所有刻度线并根据其数据值调整它们的大小。 Just remember that you'll have to do this after every axis redraw, as well. 请记住,每次重绘轴后你都必须这样做。

Example: 例:
https://jsfiddle.net/zUj3E/1/ https://jsfiddle.net/zUj3E/1/

Key code: 关键代码:

d3.selectAll("g.y.axis g.tick line")
    .attr("x2", function(d){
    //d for the tick line is the value
    //of that tick 
    //(a number between 0 and 1, in this case)
       if ( (10*d)%2 ) //if it's an even multiple of 10%
           return 10;
       else
           return 4;
    });

Note that the tick marks at the max and min values are also drawn as part of the same <path> as the axis line, so shortening them doesn't have much effect. 请注意,最大值和最小值的刻度线也会绘制为与轴线相同的<path>一部分,因此缩短它们没有太大影响。 If you don't like that lack of control, declare the "outer" ticks to have zero length when you set up the axis. 如果您不喜欢缺乏控制,请在设置轴时声明“外部”刻度为零长度。 That turns off the corners of the path, but the outer ticks will still have lines that you can control the same as the other tick lines: 这会关闭路径的角落,但外部刻度线仍然会有与其他刻度线相同的线条:

var axis = d3.svg.axis()
    .tickSize(10,0)

Example: https://jsfiddle.net/zUj3E/2/ 示例: https//jsfiddle.net/zUj3E/2/

If that doesn't work, google for examples of major/minor tick patterns. 如果这不起作用,请谷歌搜索主要/次要刻度模式的示例。 Just make sure the example you're looking at uses d3 version 3: there were a few extra tick-related methods added in version 2 that are no longer supported. 只需确保您正在查看的示例使用d3版本3:在版本2中添加了一些额外的与tick相关的方法,这些方法不再受支持。 See this SO Q&A. 看到这个问答。

Variant on the answer that suits my requirements. 根据我的要求改变答案。 Picked the values I just wanted tick marks for instead of ticks and value, added the "hide" class. 选择我想要的刻度标记代替刻度和值,添加“隐藏”类。 But this can be used for any variation on the theme. 但这可以用于主题的任何变化。

            var gy = humiditySVG.append( "g" )
                .attr( "class", "y axis" )
                .attr( "transform", "translate(" + 154 + "," + 0 + ")" )
                .call( yAxis );

            humiditySVG.selectAll( "text" )
                .attr( "class", function ( d ) {
                                           if ( $.inArray(d, [0,50,100])==-1  ) {
                               return "hide";
                           }
                           return;
                       } );
            humiditySVG.selectAll( "line" )
                .attr( "x2", function ( d ) {
                           if ( $.inArray(d, [0,50,100])==-1  ) {
                               return 1;
                           } else {
                               return 3;
                           }
                           return;
                       } );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM