-1

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?

E_net4
  • 27,810
  • 13
  • 101
  • 139
Connor
  • 3
  • 16
  • What exactly do you mean by "first Spot"? Don't you need to align those to a Date-Axis? Also, why do skip the first one ( `for (int i = 1;` )? From the picture it looks like you sort by length and position them centered. I would have expected a sort by StartDate (if at all) and x-position according to their relative (Date-)Position to a StartDate on a Date Axis. – Fildor Nov 02 '21 at 08:39
  • @Fildor sorry i forgot to show why, its there now – Connor Nov 02 '21 at 08:42
  • Mhm, I am just wondering why you center them at all. That is not what a Gantt does. – Fildor Nov 02 '21 at 08:44
  • @Fildor I want to make a Gantt Chart: https://www.google.com/search?q=easy+gantt+chart&tbm=isch&ved=2ahUKEwitmZbhpPnzAhXN3eAKHSfqC9oQ2-cCegQIABAA&oq=easy+gantt+chart&gs_lcp=CgNpbWcQAzIECAAQEzIECAAQEzIECAAQEzIICAAQCBAeEBM6BwgjEO8DECc6BQgAEIAEOgQIABBDOggIABCABBCxAzoICAAQsQMQgwE6BAgAEB46BggAEB4QE1ChC1jfJ2DmKGgCcAB4AIABuwGIAaALkgEEMTcuMpgBAKABAaoBC2d3cy13aXotaW1nwAEB&sclient=img&ei=W_qAYe3_Gc27gwen1K_QDQ&bih=751&biw=1503&client=opera-gx&hs=1VY#imgrc=qA1p_lBAuKYoFM – Connor Nov 02 '21 at 08:45
  • @Fildor yea the centering was just for now and I didn't delete it yet – Connor Nov 02 '21 at 08:45
  • Ah, ok. So, I would say you only need to figure out the x-positioning along an axis. If you want them sorted by startdate to appear by that order from top to bottom, you just need to sort them by startdate when rendering them into the chart. – Fildor Nov 02 '21 at 08:47

1 Answers1

0

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.

Connor
  • 3
  • 16