Connor
Connor

Reputation: 3

Placing Rectangle with earliest startdate on first spot (Gantt Chart)

In my program i have 4 "workPackages" with different start and enddates, this looks like this:

WorkPackage[] workpackages = new WorkPackage[4];

workpackages[0] = new WorkPackage("Package 1", new DateTime(2021, 1, 7), new DateTime(2021, 2, 2));
workpackages[1] = new WorkPackage("Package 2", new DateTime(2021, 1, 1), new DateTime(2021, 4, 1));
workpackages[2] = new WorkPackage("Package 3", new DateTime(2022, 1, 3), new DateTime(2022, 8, 1));
workpackages[3] = new WorkPackage("Package 4", new DateTime(2023, 1, 8), new DateTime(2024, 12, 1));

Then I created rectangles whose length depends on the time span between the date times from the packages. This looks like this:

for (int i = 0; i < workpackages.Length; i++)
        {
            timeSpan[i] = (workpackages[i].enddate - workpackages[i].startdate).TotalDays;

            Rectangle rectangle = new Rectangle
            {
                Fill = brush,
                StrokeThickness = strokethickness,
                Stroke = Brushes.Black,
                Width = timeSpan[i],
                Height = rectangleHeight / howmanyrect - distancebetweenrectangles
            };
            MyRectangle[i] = rectangle;

            Canvas.SetLeft(rectangle, rectangle.Width / 2.0 - rectangle.Width / 2.0);
            Canvas.SetTop(rectangle, i * (rectangleHeight / howmanyrect));

            myCanvas.Children.Add(rectangle);
            Content = myCanvas;
        }

And in my WPF window it looke like this: looks like this. If you know what a Gantt Chart is then it should be familiar to you what I'm trying to do. Now I want the rectangle with the earliest startDate to be on the first spot and then the others in order according to their start- and endDate. For finding the earliest date I did this for loop:

for (int i = 1; i < workpackages.Length; i++)
        {
            if (workpackages[i].startdate < earliestdate)
            {
                earliestdate = workpackages[i].startdate;
            }
        }

So how do I let the program know that the rectangle with the earliest date must be placed on the first spot?

Upvotes: -1

Views: 121

Answers (1)

Connor
Connor

Reputation: 3

First, you got to cut out your centering method and then you use this for finding earliest date:

DateTime earliestdate = workpackages[0].startdate;
for (int i = 1; i < workpackages.Length; i++)
{
    if (workpackages[i].startdate < earliestdate)
    {
        earliestdate = workpackages[i].startdate;
    }
}
return earliestdate;

and this for finding the latest date:

DateTime latestdate = workpackages[0].enddate;
for (int i = 1; i < workpackages.Length; i++)
{
    if (workpackages[i].enddate > latestdate)
    {
        latestdate = workpackages[i].enddate;
    }
}
return latestdate;

Then you just need to get the positioning and stuff, for that you use this:

double totalDaysDiff = (latestdate - earliestdate).TotalDays;
double totalDistanceDiff = data.latestDatePosition - data.earliestDatePosition;

double dayStep = totalDistanceDiff / totalDaysDiff;

double[] width = new double[workpackages.Length];

for (int i = 0; i < workpackages.Length; i++)
{
    double daysFromStart = (workpackages[i].startdate - earliestdate).TotalDays;
    double rectStartPosition = data.earliestDatePosition + daysFromStart * dayStep;

    double daysFromEnd = (workpackages[i].enddate - earliestdate).TotalDays;
    double rectEndPosition = data.earliestDatePosition + daysFromEnd * dayStep;

    width[i] = rectEndPosition - rectStartPosition;

    double rectangleOffset = 14;
    Rectangle rectangle = new Rectangle
    {
        Fill = Brushes.Green,
        StrokeThickness = rectValues.strokethickness,
        Stroke = Brushes.Black,
        Width = width[i] + rectangleOffset,
        Height = rectValues.rectangleHeight / rectValues.heightDevider - rectValues.distancebetweenrectangles
    };
}

And that's how you can place your rectangles like it's a Gantt Chart.

Upvotes: 0

Related Questions