简体   繁体   English

Flot图表勾选问题

[英]Flot graphs tick issue

I have a flot graphs with a custom tick formatter function, 我有一个带有自定义刻度格式函数的flot图,

if ((val / 1000) >= 1) {
    val = String(Math.round(val / 1000)) + 'K';
}

return String(val);

But the issue is it returns same values for some ticks. 但问题是它为某些刻度返回相同的值。

Little explanation about code: 关于代码的一点解释:

val --> any integer value ranging from 1 to infinity

val / 1000 logic is to convert the tick into user friends (readable) format (1K, 2K etc). val / 1000逻辑是将tick转换为用户朋友(可读)格式(1K,2K等)。

But the issue here is I get repeated tick labels when some val round up to equal values. 但是这里的问题是当一些val向上舍入到相等的值时,我会重复刻度标签。

It want to know any good way to fix this algorithm to calculate this? 它想知道修复此算法来计算这个的任何好方法吗?

Addendum 附录

val / 1000 does not mean val is multiple of 1000 it could be any integer value ranging from 1 to infinity. val / 1000并不意味着val是1000倍数,它可以是从1到无穷大的任何整数值。

Eg: For val = 1000 or 1001 it return 1K on two tick labels. 例如:对于val = 1000 or 1001它在两个刻度标签上返回1K

I know its algorithmic bug .I just wanna to know if there is any way to fix it cleanly 我知道它的算法错误。我只是想知道是否有任何方法可以彻底解决它

Notes 笔记

  1. Ticks must be rounding to two decimal at the most (great if no decimal points). 刻度必须最多舍入到两位小数(如果没有小数点则很好)。
  2. You cannot change val / 1000 . 你不能改变val / 1000 You can Play round with Math.round() function. 你可以使用Math.round()函数进行游戏。
  3. Even if want to use toFixed() follow Rule 1 即使想要使用toFixed()遵循规则1

Here's a solution that uses suffixes for large numbers while keeping the accuracy: 这是一个在保持准确性的同时使用大数字后缀的解决方案:

function tick_label(val) {
    if (val < 0) return "\u2212" + tick_label(-val);
    if (val < 1000) return String(val);

    var mag = 1;
    var suffix = "";

    if (val >= 1000) { mag = 1000; suffix = "k" }
    if (val >= 1000000) { mag = 1000000; suffix = "M" }
    if (val >= 1000000000) { mag = 1000000000; suffix = "G" }

    var div = mag;

    while (val % 10 == 0) {
        val /= 10;
        div /= 10;
    }

    return String(val / div) + suffix;
}

This code relies on round numbers for ticks, so that the exact number doesn't look strange or overly exact. 此代码依赖于刻度的圆数,因此确切的数字看起来并不奇怪或过于精确。 (A scale of 1.002k, 1.004k, 1.006k looks okay, but a scale of 1.102k, 1.202k, 1.302k does not. I'm not familiar with Flot, but I guess it takes care of that.) (1.002k,1.004k,1.006k的比例看起来没问题,但是1.102k,1.202k,1.302k的比例没有。我不熟悉Flot,但我想它会解决这个问题。)

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

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