Home > Enterprise >  Regular spacing of data depending on a number
Regular spacing of data depending on a number

Time:11-03

Im trying to create a Gantt Chart depending on the earliest date and latest date of these:

workpackages[0] = new WorkPackage("Package 1", new DateTime(2021, 1, 1), new DateTime(2021, 1, 2));
workpackages[1] = new WorkPackage("Package 2", new DateTime(2021, 2, 2), new DateTime(2021, 3, 3));
workpackages[2] = new WorkPackage("Package 3", new DateTime(2021, 3, 6), new DateTime(2021, 6, 10));
workpackages[3] = new WorkPackage("Package 4", new DateTime(2021, 4, 2), new DateTime(2021, 6, 5));
workpackages[4] = new WorkPackage("Package 5", new DateTime(2021, 3, 2), new DateTime(2021, 4, 5));
workpackages[5] = new WorkPackage("Package 6", new DateTime(2021, 4, 2), new DateTime(2021, 5, 5));
workpackages[6] = new WorkPackage("Package 7", new DateTime(2021, 2, 4), new DateTime(2021, 3, 3));

The program creates an array that's as long as the time span between the earliest date (1.1.2021) and the latest date (10.6.2021) so that would be 160 Days. And now I could just place all the dates along like 1.1.2021, 2.1.2021, 3.1.2021...... but I want the user to decide how many tics he wants. So if he types 10 he should get 12 dates (start and end too) with a regular distance between them, if he would put 3 as how many tics he wants he would get the start date the end date, and a date that's the middle of these two. But what calculation do I have to do for that?

By the way, if you don't know what a Gantt Chart is, this is one.

This is my method for this so far but I can't solve the problem:

public void ArrayDate(DateTime start, DateTime end, int n)
    {
        double timeSpan;

        timeSpan = (end - start).TotalDays;

        DateTime[] alldates = new DateTime[(int)timeSpan];

        for (int i = 0; i < timeSpan; i  )
        {
            alldates[i] = start.AddDays(i);
        }
    }

CodePudding user response:

You can only do that with numbers that you can divide with having a full number as result, so it would work for example with 11. Then you have 16 Days between every date. This is what you got to do to solve it:

double timeSpan;
n = 11;

timeSpan = (end - start).TotalDays;

DateTime[] alldates = new DateTime[n];

int timeStep = (int)timeSpan / (n - 1);

for (int i = 0; i < n; i  )
{
    alldates[i] = start.AddDays(i * timeStep);
}

So you make a new Variable (timeStep) that has the number of the distance between every date by just dividing timeSpan with the number of how my dates you have then you still need to do -1 because otherwise you cant divide it by getting a full number and also -1 is the exact amount of how many gaps are between the dates, I give you a little example for that. As you see in the picture n = 3 and with doing -1 it's dividable and it's the number of gaps between the dates. Then you just do AddDays() and do i * timeStep. This would be 0 * 16 so in the first iteration you get the first date because its times 0, then in the second iteration it would be 1 * 16 that would be 16, and then 2 * 16 and so on, so you always add 16 days by multiplicating it with i.

I hope ths its helpin u. If u still ned help then tel it.

  • Related