将具有最早开始日期的矩形放在第一个位置(甘特图)

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

在我的程序中,我有 4 个具有不同开始和结束日期的“工作包”,如下所示:

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));

然后我创建了矩形,其长度取决于包中日期时间之间的时间跨度。这看起来像这样:

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;
        }

在我的 WPF window 中它看起来像这样: 。 如果您知道甘特图是什么,那么您应该熟悉我正在尝试做的事情。现在我希望具有最早 startDate 的矩形位于第一个位置,然后根据它们的 start- 和 endDate 按顺序排列其他矩形。为了找到最早的日期,我做了这个 for 循环:

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

那么如何让程序知道日期最早的矩形必须放在第一个位置呢?

首先,你必须去掉你的居中方法,然后你用它来寻找最早的日期:

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

这用于查找最新日期:

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

然后你只需要得到定位和东西,为此你使用这个:

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
            };

这就是您如何像放置甘特图一样放置矩形。