我已经为图表上的轴计算了步长。
我还有最小值和最大值。现在我需要计算所有刻度,以便显示我的最小值和最大值之间的所有值。
例如:
步长:1000
最小值:213
最大值:4405
预期报价:0,1000,2000,3000,4000,5000
步长:500
最小值:-1213
最大值:1405
预期报价:-1500、-1000、-500、0、500、1000、1500
到目前为止,我一直在尝试使用“试错法”来计算第一个值,例如:
bool firstStepSet = false;
double firstStep = stepSize;
do
{
if (xValue >= (firstStep - (stepSize / 2)) && xValue <=
(firstStep + (stepSize / 2)))
{
firstStepSet = true;
this.myBarXValues.Add(firstStep, 0);
}
else if (xValue > stepSize)
{
firstStep += stepSize;
}
else
{
firstStep -= stepSize;
}
}
while (!firstStepSet);
然后,我将向此列表添加步骤,直到所有值都适合为止。
这对我来说似乎很脏,我想知道是否还有其他解决方案。
所以我需要的是一个计算我需要的第一个报价的解决方案。
最佳答案
此函数计算第一步和最后一步的值:
static void CalcSteps(int min, int max, int stepSize, out int firstStep, out int lastStep)
{
if (min >= 0)
{
firstStep = (min / stepSize) * stepSize;
}
else
{
firstStep = ((min - stepSize + 1) / stepSize) * stepSize;
}
if (max >= 0)
{
lastStep = ((max + stepSize - 1) / stepSize) * stepSize;
}
else
{
lastStep = (max / stepSize) * stepSize;
}
}
关于c# - 计算具有步长的图表的轴刻度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55414454/