「東雲 忠太郎」の平凡な日常のできごと

「東雲 忠太郎」の平凡な日常のできごと

2024.03.17
XML
カテゴリ: C#.NET


以下は、C# WPFでガントチャートを作成するサンプルコードです。このサンプルでは、`GanttChartItem`というクラスを作成して、ガントチャートのアイテムを表現します。そして、`GanttChart`というユーザーコントロールを作成し、ガントチャート全体を表現します。


まず、`GanttChartItem`クラスを作成します。


```csharp

using System;


namespace GanttChartSample

{

    public class GanttChartItem

    {

        public string Name { get; set; }

        public DateTime StartDate { get; set; }

        public DateTime EndDate { get; set; }

    }

}

```


次に、`GanttChart`ユーザーコントロールを作成します。


```csharp

using System;

using System.Collections.Generic;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Media;


namespace GanttChartSample

{

    public partial class GanttChart : UserControl

    {

        public List<GanttChartItem> Items

        {

            get { return (List<GanttChartItem>)GetValue(ItemsProperty); }

            set { SetValue(ItemsProperty, value); }

        }


        public static readonly DependencyProperty ItemsProperty =

            DependencyProperty.Register("Items", typeof(List<GanttChartItem>), typeof(GanttChart), new PropertyMetadata(new List<GanttChartItem>(), OnItemsPropertyChanged));


        private static void OnItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

        {

            var chart = d as GanttChart;

            if (chart != null)

            {

                chart.DrawChart();

            }

        }


        public GanttChart()

        {

            InitializeComponent();

        }


        private void DrawChart()

        {

            ChartCanvas.Children.Clear();


            if (Items == null || Items.Count == 0)

                return;


            double totalDays = 0;

            foreach (var item in Items)

            {

                totalDays = Math.Max(totalDays, (item.EndDate - item.StartDate).TotalDays);

            }


            double itemHeight = 30;

            double itemWidth = ChartCanvas.ActualWidth / totalDays;


            for (int i = 0; i < Items.Count; i++)

            {

                var item = Items[i];

                double x = (item.StartDate - Items[0].StartDate).TotalDays * itemWidth;

                double width = (item.EndDate - item.StartDate).TotalDays * itemWidth;


                var rect = new Rectangle

                {

                    Width = width,

                    Height = itemHeight,

                    Fill = Brushes.Blue,

                    Stroke = Brushes.Black,

                    StrokeThickness = 1

                };


                Canvas.SetLeft(rect, x);

                Canvas.SetTop(rect, i * itemHeight);


                ChartCanvas.Children.Add(rect);


                var label = new Label

                {

                    Content = item.Name,

                    Foreground = Brushes.White,

                    HorizontalAlignment = HorizontalAlignment.Center,

                    VerticalAlignment = VerticalAlignment.Center,

                    FontWeight = FontWeights.Bold

                };


                Canvas.SetLeft(label, x);

                Canvas.SetTop(label, i * itemHeight);


                ChartCanvas.Children.Add(label);

            }

        }

    }

}

```


そして、XAMLファイルで`GanttChart`ユーザーコントロールを使用します。


```xml

<UserControl x:Class="GanttChartSample.MainWindow"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

             xmlns:local="clr-namespace:GanttChartSample"

             mc:Ignorable="d"

             d:DesignHeight="450" d:DesignWidth="800">

    <Grid>

        <local:GanttChart Items="{Binding GanttChartItems}" />

    </Grid>

</UserControl>

```


これで、`GanttChart`を含むWPFアプリケーションを作成し、`GanttChartItem`をバインドすることでガントチャートを表示できます。






お気に入りの記事を「いいね!」で応援しよう

Last updated  2024.03.17 09:56:56


【毎日開催】
15記事にいいね!で1ポイント
10秒滞在
いいね! -- / --
おめでとうございます!
ミッションを達成しました。
※「ポイントを獲得する」ボタンを押すと広告が表示されます。
x

© Rakuten Group, Inc.
X
Create a Mobile Website
スマートフォン版を閲覧 | PC版を閲覧
Share by: