如何在jqplot中仅显示y轴整数值

9

如何将y轴的值转换为整数?

我目前有这个值0.0 1.0 1.5 2.0 2.5 3.0。我想将其更改为类似于0 1 2 3等的内容。

谢谢!干杯!

4个回答

28

如果我理解您的意思正确,您希望在y轴上显示整数值。

请尝试以下代码:

axesDefaults: 
{ 
    min: 0,  
    tickInterval: 1, 
    tickOptions: { 
            formatString: '%d' 
        } 
}

3
我只使用了 "tickOptions: formatString: '%d'",它非常好用。 - Ned Batchelder
但是对于y轴的最大值为1,它在y轴上显示0、1、1。 - jbmyid

1

重写createTicks函数并引入新的轴布尔属性 - integersOnly。

// jqplot adding integersOnly option for an axis
var oldCreateTicks = $.jqplot.LinearAxisRenderer.prototype.createTicks;
$.jqplot.LinearAxisRenderer.prototype.createTicks = function (plot) {
    if (this.integersOnly == true) {
        var db = this._dataBounds;
        var min = ((this.min != null) ? this.min : db.min);
        var max = ((this.max != null) ? this.max : db.max);
        var range = max - min;
        if (range < 3) {
            if (this.min == null) {
                this.min = 0;
            }
            this.tickInterval = 1;
        }
    }
    return oldCreateTicks.apply(this, plot);
}

1
只是在顶部答案的基础上进行构建。
axes: {
         yaxis: {
             min: 0,
             tickInterval: 1, 
             tickOptions: {
                  formatString: '%d'
             }

          }
       }

只需将此应用于y轴即可。如果您有一个条形图或其他图表,并且想要隔离轴,则非常有帮助。


-4
尝试使用 parseInt(y_axis)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接