如何确定图表上的Y轴值?

问题描述 投票:0回答:2

我正在研究一个图表算法,它将给我一组n个y轴值的数组,我将在我的图表上使用。

主要的问题是,我还想计算出要使用的步数,并为它们使用漂亮的数字。 它必须能够接受整数和双数,并且能够处理小范围(1以下)和大范围(10000以上等)。

例如,如果给我一个0.1-0.9的范围,理想情况下,我会用0,0.2,0.4,0.6,0.8,1,但如果给我0.3-0.7,我可能会用0.3,0.4,0.5,0.6,0.7。

这是我目前所拥有的,它在小范围内工作得很好,但在大范围内工作得很糟糕,而且不能给我很好的数字。

-(double*)yAxisValues:(double)min (double):max {

    double diff = max - min;
    double divisor = 1.0;

    if (diff > 1) {
        while (diff > 1) {
            diff /= 10;
            divisor *= 10;
        }
    } else {
        while (diff < 1) {
            diff *= 10;
            divisor *= 10;
        }
    }

    double newMin = round(min * divisor) / divisor;
    double newMax = round(max * divisor) / divisor;

    if (newMin > min) {
        newMin -= 1.0/divisor;
    }
    if (newMax < max) {
        newMax += 1.0/divisor;
    }

    int test2 = round((newMax - newMin) * divisor); 
    if (test2 >= 7) {
        while (test2 % 6 != 0 && test2 % 5 != 0 && test2 % 4 != 0 && test2 % 3 != 0) {
            test2++;
            newMax += 1.0/divisor;
        }
    }

    if (test2 % 6 == 0) {
        test2 = 6;
    } else if (test2 % 5 == 0) {
        test2 = 5;
    } else if (test2 % 4 == 0 || test2 == 2) {
        test2 = 4;
    } else if (test2 % 3 == 0) {
        test2 = 3;
    }

    double *values = malloc(sizeof(double) * (test2 + 1));
    for (int i = 0; i < test2 + 1; i++) {
        values[i] = newMin + (newMax - newMin) * i / test2;
    }
    return values;
}

有什么建议吗?

iphone objective-c graph charts
2个回答
0
投票

这里有一段代码,可以做一些类似的事情,不过方法略有不同。单位 "指的是你在图上绘制的内容。因此,如果你的比例尺是让你的图形上的一个单位在屏幕上应该是20个像素,这个函数将返回每一步应该是多少个单位。有了这些信息,你就可以很容易地计算出轴值是什么,以及在哪里绘制它们。

- (float)unitsPerMajorGridLine:(float)pixelsPerUnit {
    float amountAtMinimum, orderOfMagnitude, fraction;

    amountAtMinimum = [[self minimumPixelsPerMajorGridLine] floatValue]/pixelsPerUnit;  
    orderOfMagnitude = floor(log10(amountAtMinimum));
    fraction = amountAtMinimum / pow(10.0, orderOfMagnitude);

    if (fraction <= 2) {
        return 2 * pow(10.0, orderOfMagnitude);
    } else if (fraction <= 5) {
        return 5 * pow(10.0, orderOfMagnitude);
    } else {
        return 10 * pow(10.0, orderOfMagnitude);
    }
}

0
投票

简单的JavaScript改编(非常感谢Johan Kool提供的源码)。

const step = (() => {let pixelPerUnit = height / (end - size)
, amountAtMinimum = minimumPixelsPerMajorGridLine / pixelPerUnit
, orderOfMagnitude = Math.floor(Math.log10(amountAtMinimum))
, fraction = amountAtMinimum / Math.pow(10.0, orderOfMagnitude);

let result;
if (fraction <= 2) {
    result = 2 * Math.pow(10.0, orderOfMagnitude);
} else if (fraction <= 5) {
    result = 5 * Math.pow(10.0, orderOfMagnitude);
} else {
    result = 10 * Math.pow(10.0, orderOfMagnitude);
}})();

let arr = [];
arr.push(start);
let curVal = start - start % step + step
, pxRatio = height / (end - start);

while (curVal < end) {
    arr.push(curVal);
    curVal += step;
}
arr.push(end);
© www.soinside.com 2019 - 2024. All rights reserved.