diff --git a/ControlUse/Timeline/ChartConstants.cs b/ControlUse/Timeline/ChartConstants.cs new file mode 100644 index 0000000..4fcdc2c --- /dev/null +++ b/ControlUse/Timeline/ChartConstants.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using Timeline.Model; +using System.Windows.Forms; +using System.Drawing; + +namespace Timeline +{ + public static class ChartConstants + { + public const int BarStartRight = 25; + public const int BarStartLeft = 50; + public const int HeaderTimeStartTop = 30; + public const int BarStartTop = 50; + public const int BarHeight = 40; + public const int BarSpace = 8; + public const int ToolTipTitleHeight = 14; + public const int ToolTipfontHeight = 12; + public const int MinimumIntervalInMinutes = 5; + public static Font RowFont = new Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point); + public static Font TitleFont = new Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Point); + public static Pen GridColor = Pens.LightBlue; + } +} diff --git a/ControlUse/Timeline/Model/BarModel.cs b/ControlUse/Timeline/Model/BarModel.cs new file mode 100644 index 0000000..133c6a5 --- /dev/null +++ b/ControlUse/Timeline/Model/BarModel.cs @@ -0,0 +1,20 @@ +using System; +using System.Drawing; + +namespace Timeline.Model +{ + public class BarModel + { + public DateTime StartValue { get; set; } + public DateTime EndValue { get; set; } + public Color Color { get; set; } + public string Name { get; set; } + public TimeSpan Duration { get; set; } + public int RowIndex { get; set; } + public bool IsClicked { get; set; } + public bool IsMouseOver { get; set; } + public bool Visible { get; set; } + public Rectangle BarRectangle { get; set; } + } + +} diff --git a/ControlUse/Timeline/Model/HeaderModel.cs b/ControlUse/Timeline/Model/HeaderModel.cs new file mode 100644 index 0000000..a21a68a --- /dev/null +++ b/ControlUse/Timeline/Model/HeaderModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace Timeline.Model +{ + public class HeaderModel + { + public int StartLocation { get; set; } + public DateTime HeaderDateTime { get; set; } + } +} diff --git a/ControlUse/Timeline/Model/ItemModel.cs b/ControlUse/Timeline/Model/ItemModel.cs new file mode 100644 index 0000000..77556fa --- /dev/null +++ b/ControlUse/Timeline/Model/ItemModel.cs @@ -0,0 +1,13 @@ +using System; +using System.Drawing; + +namespace Timeline.Model +{ + public class ItemModel + { + public string ItemName { get; set; } + public DateTime StartDate { get; set; } + public Color ItemColor { get; set; } + public TimeSpan Duration { get; set; } + } +} diff --git a/ControlUse/Timeline/Processor/BarChartProcessor.cs b/ControlUse/Timeline/Processor/BarChartProcessor.cs new file mode 100644 index 0000000..72a6d9f --- /dev/null +++ b/ControlUse/Timeline/Processor/BarChartProcessor.cs @@ -0,0 +1,131 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Runtime.Remoting.Messaging; +using Timeline.Model; +using Timeline; +using System.Threading.Tasks; + +namespace Timeline.Processor +{ + public class BarChartProcessor + { + private List ItemNameList { get; set; } + public List GetBarList(List itemList) + { + var barList = new List(); + itemList.ForEach(x => + { + var bar = new BarModel + { + Name = x.ItemName, + StartValue = x.StartDate, + EndValue = x.StartDate + x.Duration, + Duration = x.Duration, + Color = x.ItemColor, + RowIndex = GetRowIndex(x.ItemName) + }; + barList.Add(bar); + }); + return barList; + } + + private int GetRowIndex(string barName) + { + if (ItemNameList == null) + { + ItemNameList = new List(); + } + if (!ItemNameList.Contains(barName)) + { + ItemNameList.Add(barName); + } + return ItemNameList.FindIndex(x => x == barName); + } + + public List GetFullHeaderList(DateTime startDate, DateTime endDate, int availableWidth, Font timeFont) + { + var headerList = new List(); + + var timeInterval = endDate - startDate; + + var headerSpace = System.Windows.Forms.TextRenderer.MeasureText("12-12-12", timeFont); + + var numberOfHeaders = availableWidth / headerSpace.Width; + + var timeIncrement = new TimeSpan(timeInterval.Ticks/numberOfHeaders); + + var index = 0; + + for (var date = startDate; date <= endDate; date = date.Add(timeIncrement), index ++) + { + var header = new HeaderModel + { + HeaderDateTime = date, + StartLocation = ChartConstants.BarStartLeft + (index * headerSpace.Width) + }; + + headerList.Add(header); + } + + return headerList; + } + + public bool MouseInsideBar(Point mousePosition, BarModel bar) + { + return mousePosition.X >= bar.BarRectangle.Left + && mousePosition.X <= bar.BarRectangle.Right + && mousePosition.Y >= bar.BarRectangle.Top + && mousePosition.Y <= bar.BarRectangle.Bottom; + + } + internal List MouseClickHandler(List list, Point localMousePosition) + { + Parallel.ForEach(list, bar => + { + if (MouseInsideBar(localMousePosition, bar) && bar.Visible) + { + bar.IsClicked = true; + } + else + { + bar.IsClicked = false; + } + }); + + return list; + } + internal double GetPixelsPerSecond(List headerList) + { + var timeBetweenHeaders = headerList[1].HeaderDateTime - headerList[0].HeaderDateTime; + var widthBetween = headerList[1].StartLocation - headerList[0].StartLocation; + var pixelsPerSecond = widthBetween / timeBetweenHeaders.TotalSeconds; + return pixelsPerSecond; + } + + internal BarModel GetBar(BarModel bar, DateTime startDate, double pixelsPerSecond, int scrollPosition, int chartWidth) + { + var availableWidth = chartWidth - ChartConstants.BarStartLeft - ChartConstants.BarStartRight; + + bar.Visible = true; + + var startTimeSpan = bar.StartValue - startDate; + var startLocation = (int)(pixelsPerSecond * startTimeSpan.TotalSeconds); + var x = ChartConstants.BarStartLeft + startLocation; + var y = ChartConstants.BarStartTop + (ChartConstants.BarHeight * (bar.RowIndex - scrollPosition)) + + (ChartConstants.BarSpace * (bar.RowIndex - scrollPosition)) + 4; + var width = (int)(pixelsPerSecond * bar.Duration.TotalSeconds); + + //restrict the width if longer than the right size + if (x + width > (chartWidth - ChartConstants.BarStartRight)) + { + width = availableWidth + ChartConstants.BarStartLeft - x; + } + + bar.BarRectangle = new Rectangle(x, y, width, ChartConstants.BarHeight); + + return bar; + } + } +} \ No newline at end of file diff --git a/ControlUse/Timeline/Timeline.Designer.cs b/ControlUse/Timeline/Timeline.Designer.cs new file mode 100644 index 0000000..751a5db --- /dev/null +++ b/ControlUse/Timeline/Timeline.Designer.cs @@ -0,0 +1,47 @@ +namespace Timeline +{ + partial class Timeline + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Component Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.SuspendLayout(); + // + // Timeline + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.MinimumSize = new System.Drawing.Size(100, 100); + this.Name = "Timeline"; + this.Size = new System.Drawing.Size(157, 136); + this.Load += new System.EventHandler(this.Timeline_Load); + this.ResumeLayout(false); + + } + + #endregion + } +} diff --git a/ControlUse/Timeline/Timeline.cs b/ControlUse/Timeline/Timeline.cs new file mode 100644 index 0000000..7c4a884 --- /dev/null +++ b/ControlUse/Timeline/Timeline.cs @@ -0,0 +1,318 @@ +/// +/// Adds a timeline or Gantt user control. +/// See Timeline Test for usage. +/// Created by Maxime Jacques - 2014 +/// Parts of code and inspiration from VBGanttChart v0.55 +/// by Adrian "Adagio" Grau http://www.codeproject.com/Articles/20731/Gantt-Chart +/// + +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; +using Timeline.Model; +using Timeline.Processor; + +namespace Timeline +{ + public partial class Timeline : UserControl + { + #region public properties + public SolidBrush HoverClickSolidBrush { get; set; } + public Font RowFont { get; set; } + public Font DateFont { get; set; } + public Font TimeFont { get; set; } + #endregion + + #region private properties + private ToolTip ToolTip { get; set; } + private VScrollBar VScrollBar1 { get; set; } + private int AvailableWidth { get; set; } + private Point OldMousePosition { get; set; } + private int ScrollPosition { get; set; } + private List ToolTipTextList { get; set; } + private string ToolTipTextTitle { get; set; } + private List BarList { get; set; } + private DateTime StartDate { get; set; } + private int DistinctItemCount { get; set; } + private DateTime EndDate { get; set; } + #endregion + public Timeline() + { + InitializeComponent(); + } + public void ShowBarChart(DateTime chartStartDate, DateTime chartEndDate, List items) + { + this.StartDate = chartStartDate; + this.EndDate = chartEndDate; + var proc = new BarChartProcessor(); + this.BarList = proc.GetBarList(items); + this.DistinctItemCount = items.Select(i => i.ItemName).Distinct().Count(); + + this.Refresh(); + } + private void ChartMouseMove(Object sender, MouseEventArgs e) + { + var localMousePosition = new Point(e.X, e.Y); + + if (BarList == null || BarList.Count == 0) + { + return; + } + + if (localMousePosition == this.OldMousePosition) + { + return; + } + + var proc = new BarChartProcessor(); + + var mouseOverObject = false; + var tempText = new List(); + var tempTitle = ""; + + Parallel.ForEach(this.BarList, bar => + { + if (proc.MouseInsideBar(localMousePosition, bar) && bar.Visible) + { + bar.IsMouseOver = true; + tempTitle = bar.Name; + tempText.Add("Event Start: " + bar.StartValue.ToUniversalTime()); + tempText.Add("Event End: " + bar.EndValue.ToUniversalTime()); + mouseOverObject = true; + } + else + { + bar.IsMouseOver = false; + } + }); + + this.ToolTipTextList = tempText; + this.ToolTipTextTitle = tempTitle; + this.ToolTip.SetToolTip(this, this.ToolTipTextList.Count > 0 ? this.ToolTipTextList.ToString() : ""); + + if (mouseOverObject) + { + this.Refresh(); + } + + this.OldMousePosition = localMousePosition; + } + private void ChartMouseClick(Object sender, MouseEventArgs e) + { + if (BarList == null || BarList.Count == 0) + { + return; + } + + var localMousePosition = new Point(e.X, e.Y); + + var proc = new BarChartProcessor(); + + this.BarList = proc.MouseClickHandler(this.BarList, localMousePosition); + } + private void ChartMouseWheel(object sender, MouseEventArgs e) + { + this.VScrollBar1.Focus(); + } + private void Timeline_Load(object sender, EventArgs e) + { + //initialize public properties + RowFont = TimeFont = DateFont = new Font("Segoe UI", 10, FontStyle.Regular, GraphicsUnit.Point); + HoverClickSolidBrush = new SolidBrush(Color.LightBlue); + BackColor = Color.White; + + //initialize mouse controls + MouseMove += ChartMouseMove; + MouseWheel += ChartMouseWheel; + MouseClick += ChartMouseClick; + + //initialize Tooltip + this.ToolTip = new ToolTip + { + OwnerDraw = true + }; + this.ToolTip.Draw += ToolTipText_Draw; + this.ToolTip.Popup += ToolTipText_Popup; + + //Flicker free drawing + SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); + + //ScrollBar + this.VScrollBar1 = new VScrollBar + { Dock = DockStyle.Right, + Visible = false + }; + Controls.Add(this.VScrollBar1); + this.VScrollBar1.Scroll += vScrollBar1_Scroll; + } + private void vScrollBar1_Scroll(object sender, ScrollEventArgs e) + { + this.ScrollPosition = this.VScrollBar1.Value; + this.Refresh(); + } + private void ToolTipText_Draw(Object sender, DrawToolTipEventArgs e) + { + if (this.ToolTipTextList.Count == 0) + { + return; + } + e.Graphics.FillRectangle(Brushes.White, e.Bounds); + e.Graphics.DrawString(this.ToolTipTextTitle, ChartConstants.TitleFont, Brushes.Black, 5, 0); + + // Draws the lines in the text box + foreach (var item in this.ToolTipTextList) + { + var stringY = (ChartConstants.ToolTipTitleHeight - ChartConstants.ToolTipfontHeight - e.Graphics.MeasureString(item, ChartConstants.RowFont).Height) / 2 + + 10 + ((this.ToolTipTextList.IndexOf(item) + 1) * 14); + e.Graphics.DrawString(item, ChartConstants.RowFont, Brushes.Black, 5, stringY); + } + } + private void ToolTipText_Popup(Object sender, PopupEventArgs e) + { + var toolTipHeight = (ChartConstants.ToolTipTitleHeight + 4) + (this.ToolTipTextList.Count * (ChartConstants.ToolTipfontHeight + 3)); + e.ToolTipSize = new Size(230, toolTipHeight); + } + private void PaintChart(Graphics graphics) + { + if (BarList == null || BarList.Count == 0) + { + return; + } + var proc = new BarChartProcessor(); + var headerList = proc.GetFullHeaderList(this.StartDate, this.EndDate, this.Width, this.TimeFont); + if (headerList.Count == 0 || this.DistinctItemCount == 0) + { + return; + } + + var pixelsPerSecond = proc.GetPixelsPerSecond(headerList); + + this.AvailableWidth = Width - ChartConstants.BarStartLeft - ChartConstants.BarStartRight; + + if (this.DistinctItemCount * (ChartConstants.BarHeight + ChartConstants.BarSpace) > Height) + { + this.VScrollBar1.Visible = true; + this.VScrollBar1.Maximum = this.DistinctItemCount - 3; + } + + graphics.Clear(BackColor); + DrawChartHeadersAndNet(graphics, headerList); + + DrawBars(graphics, this.BarList, pixelsPerSecond); + } + protected override void OnPaint(PaintEventArgs pe) + { + PaintChart(pe.Graphics); + } + private void DrawBars(Graphics graphics, IEnumerable barList, double pixelsPerSecond) + { + //list of machineNames to add to the left of each row + var rowTitleList = new List(); + + var proc = new BarChartProcessor(); + + // Draws each bar + foreach (var bar in barList) + { + var numberOfBarsInControl = (Height - ChartConstants.BarStartTop)/(ChartConstants.BarHeight + ChartConstants.BarSpace); + + if ((bar.RowIndex >= this.ScrollPosition && + bar.RowIndex < numberOfBarsInControl + this.ScrollPosition)) + { + var newBar = proc.GetBar(bar, this.StartDate, pixelsPerSecond, ScrollPosition, this.Width); + DrawBarAndRowText(newBar, rowTitleList, graphics); + } + else + { + bar.Visible = false; + } + } + } + private void DrawBarAndRowText(BarModel newBar, ICollection rowTitleList, Graphics graphics) + { + var barBrush = new SolidBrush(newBar.Color); + if (newBar.IsMouseOver || newBar.IsClicked) + { + barBrush = HoverClickSolidBrush; + } + + graphics.FillRectangle(barBrush, newBar.BarRectangle); + graphics.DrawRectangle(Pens.Black, newBar.BarRectangle); + + // Draws the rowtext, only once for each machine + if (!rowTitleList.Contains(newBar.Name)) + { + graphics.DrawString(newBar.Name, + RowFont, + Brushes.Black, + 0, + ChartConstants.BarStartTop + (ChartConstants.BarHeight * (newBar.RowIndex - this.ScrollPosition)) + + (ChartConstants.BarSpace * (newBar.RowIndex - this.ScrollPosition))); + + rowTitleList.Add(newBar.Name); + } + } + private void DrawChartHeadersAndNet(Graphics graphics, IList headerList) + { + var verticalLineLastY = ChartConstants.BarStartTop + (this.DistinctItemCount - this.ScrollPosition) * (ChartConstants.BarHeight + ChartConstants.BarSpace); + + //draw headers + foreach (var header in headerList) + { + //draw the date when there is a change of day + var index = headerList.IndexOf(header); + + if (headerList.IndexOf(header) == 0 + || header.HeaderDateTime.Day != headerList[index - 1].HeaderDateTime.Day) + { + graphics.DrawString( + header.HeaderDateTime.ToShortDateString(), + DateFont, + Brushes.Black, + header.StartLocation, + 0); + } + + graphics.DrawString( + header.HeaderDateTime.ToShortTimeString(), + TimeFont, + Brushes.Black, + header.StartLocation, + ChartConstants.HeaderTimeStartTop); + + //draw vertical line under header + graphics.DrawLine( + ChartConstants.GridColor, + header.StartLocation, + ChartConstants.HeaderTimeStartTop, + header.StartLocation, + verticalLineLastY); + + } + + //draw last vertical line + graphics.DrawLine( + ChartConstants.GridColor, + ChartConstants.BarStartLeft + this.AvailableWidth, + ChartConstants.HeaderTimeStartTop, + ChartConstants.BarStartLeft + this.AvailableWidth, + verticalLineLastY); + + //draw horizontal net + for (var index = 0; index < this.DistinctItemCount; index++) + { + var y = ChartConstants.BarStartTop + index * (ChartConstants.BarHeight + ChartConstants.BarSpace); + graphics.DrawLine( + ChartConstants.GridColor, + ChartConstants.BarStartLeft, + y, + ChartConstants.BarStartLeft + this.AvailableWidth, + y + ); + } + } + } +} \ No newline at end of file diff --git a/ControlUse/Timeline/Timeline.resx b/ControlUse/Timeline/Timeline.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/ControlUse/Timeline/Timeline.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/SunlightCentralizedControlManagement_SCCM_.csproj b/SunlightCentralizedControlManagement_SCCM_.csproj index 86c7310..99b97be 100644 --- a/SunlightCentralizedControlManagement_SCCM_.csproj +++ b/SunlightCentralizedControlManagement_SCCM_.csproj @@ -78,6 +78,17 @@ MSBuild:Compile Designer + + + + + + + UserControl + + + Timeline.cs + @@ -139,6 +150,9 @@ RoilingTextBlock.xaml + + Timeline.xaml + Whole.xaml @@ -217,6 +231,10 @@ Designer MSBuild:Compile + + Designer + MSBuild:Compile + Designer MSBuild:Compile @@ -248,6 +266,9 @@ Settings.settings True + + Timeline.cs + PublicResXFileCodeGenerator diff --git a/View/Timeline.xaml b/View/Timeline.xaml new file mode 100644 index 0000000..8fef305 --- /dev/null +++ b/View/Timeline.xaml @@ -0,0 +1,12 @@ + + + + + diff --git a/View/Timeline.xaml.cs b/View/Timeline.xaml.cs new file mode 100644 index 0000000..4a3fcdd --- /dev/null +++ b/View/Timeline.xaml.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace SunlightCentralizedControlManagement_SCCM_.View +{ + /// + /// Timeline.xaml 的交互逻辑 + /// + public partial class Timeline : UserControl + { + public Timeline() + { + InitializeComponent(); + } + } +}