趋势图按间隔取点时出现小数

1.假设趋势图无论表中数据有多少条,在里面最多取2个点显示。
2.取点方式用以下算法,取点间隔出现了小数,正确取点方式是怎样的呢?

int totalPoint = 2;//在趋势图显示的点数  
List<int> data = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,11,12,13,14,15,16,17,18,19,20,21};
float span = (float)data.Count / totalPoint;//获取取数间隔
List<int> newData=new List<int>();
for (int i = 0; i < data.Count; i++)
{
    if (i% span == 0)//span出现了小数
    {
        newData.Add(data[i]);
    }
}
Console.WriteLine(newData.Count);
Console.ReadLine();

题主原始代码里我其实看不出逻辑,仅仅只能看成抽样

所以如果是100个点,按楼主原始代码来看就算没有小数也有50个数(假设趋势图无论表中数据有多少条,在里面最多取2个点显示)
??所以我糊涂了,你是要50个数,还是只要2个??

至于说小数处理把,其实比较容易的。
int pagecount=(data.Count+totalPoint-1) / totalPoint; 即可

比如:12个点每隔10个取,那么你“分页”页数是2页对吧,那么 12+10-1=21 ,21/10=2(页)对么

如果span出现小数,则应该以数据项的实际间隔为准进行取点。根据span计算出每个数据项的预计取样次数,将每个数据项的取样次数取整,并将剩余的取样次数按比例分配到前面的数据项中。然后,按照计算出的实际间隔进行取点。

int totalPoint = 2;
List<int> data = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,11,12,13,14,15,16,17,18,19,20,21};
float span = (float)data.Count / totalPoint;
List<int> newData = new List<int>();

int currentIndex = 0;
int remainingSamples = 0;
for (int i = 0; i < totalPoint; i++)
{
    int currentSamples = Mathf.RoundToInt(span) + remainingSamples;
    if (i == totalPoint - 1)\n    {
        currentSamples += data.Count - currentIndex - currentSamples;
    }
    for (int j = 0; j < currentSamples && currentIndex < data.Count; j++)
    {
        if (j == currentSamples - 1)
        {
            remainingSamples = currentSamples - j - 1;
        }
        newData.Add(data[currentIndex]);
        currentIndex++;
    }
}

Console.WriteLine(newData.Count);
Console.ReadLine();