Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2893_BNLR/HeuristicLab.Clients.Hive.Views/3.3/StateLog/StateLogGanttChartListView.cs @ 16123

Last change on this file since 16123 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 6.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using System.Windows.Forms.DataVisualization.Charting;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30using HeuristicLab.Visualization.ChartControlsExtensions;
31
32namespace HeuristicLab.Clients.Hive.Views {
33  [View("StateLogGanttChartList View")]
34  [Content(typeof(StateLogListList), true)]
35  public sealed partial class StateLogGanttChartListView : ItemView {
36    private IList<LegendItem> invisibleLegendItems;
37
38    public new StateLogListList Content {
39      get { return (StateLogListList)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public StateLogGanttChartListView() {
44      InitializeComponent();
45      invisibleLegendItems = new List<LegendItem>();
46      ganttChart.chart.MouseMove += new System.Windows.Forms.MouseEventHandler(chart_MouseMove);
47      ganttChart.chart.MouseDown += new System.Windows.Forms.MouseEventHandler(chart_MouseDown);
48      ganttChart.chart.CustomizeLegend += new EventHandler<CustomizeLegendEventArgs>(chart_CustomizeLegend);
49    }
50
51    protected override void DeregisterContentEvents() {
52      base.DeregisterContentEvents();
53    }
54
55    protected override void RegisterContentEvents() {
56      base.RegisterContentEvents();
57    }
58
59    protected override void OnContentChanged() {
60      base.OnContentChanged();
61
62      ganttChart.Reset();
63      if (Content != null) {
64        SetupCategories(ganttChart);
65        if (Content.Count > 0) {
66          DateTime maxValue = Content.Max(x => x.Count > 0 ? x.Max(y => y.DateTime) : DateTime.MinValue);
67          DateTime minValue = Content.Min(x => x.Count > 0 ? x.Min(y => y.DateTime) : DateTime.MinValue);
68          DateTime upperLimit;
69          if (Content.All(x => x.Count > 0 ? (x.Last().State == TaskState.Finished || x.Last().State == TaskState.Failed || x.Last().State == TaskState.Aborted) : true)) {
70            upperLimit = DateTime.FromOADate(Math.Min(DateTime.Now.AddSeconds(10).ToOADate(), maxValue.AddSeconds(10).ToOADate()));
71          } else {
72            upperLimit = DateTime.Now;
73          }
74
75          if ((upperLimit - minValue) > TimeSpan.FromDays(1)) {
76            this.ganttChart.chart.Series[0].YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Date;
77          } else {
78            this.ganttChart.chart.Series[0].YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Time;
79          }
80
81          for (int i = Content.Count - 1; i >= 0; i--) {
82            for (int j = 0; j < Content[i].Count - 1; j++) {
83              if (Content[i][j].State != TaskState.Offline && invisibleLegendItems.All(x => x.Name != Content[i][j].State.ToString()))
84                AddData(ganttChart, i.ToString(), Content[i][j], Content[i][j + 1], upperLimit);
85            }
86            if (Content[i].Count > 0 && invisibleLegendItems.All(x => x.Name != Content[i][Content[i].Count - 1].State.ToString())) {
87              AddData(ganttChart, i.ToString(), Content[i][Content[i].Count - 1], null, upperLimit);
88            }
89          }
90        }
91      }
92    }
93
94    public static void SetupCategories(GanttChart ganttChart) {
95      ganttChart.AddCategory(TaskState.Offline.ToString(), Color.Gainsboro);
96      ganttChart.AddCategory(TaskState.Waiting.ToString(), Color.NavajoWhite);
97      ganttChart.AddCategory(TaskState.Paused.ToString(), Color.PaleVioletRed);
98      ganttChart.AddCategory(TaskState.Transferring.ToString(), Color.CornflowerBlue);
99      ganttChart.AddCategory(TaskState.Calculating.ToString(), Color.DarkGreen);
100      ganttChart.AddCategory(TaskState.Finished.ToString(), Color.White);
101      ganttChart.AddCategory(TaskState.Aborted.ToString(), Color.Orange);
102      ganttChart.AddCategory(TaskState.Failed.ToString(), Color.Red);
103    }
104
105    public static void AddData(GanttChart ganttChart, string name, StateLog from, StateLog to, DateTime upperLimit) {
106      DateTime until = to != null ? to.DateTime : upperLimit;
107      TimeSpan duration = until - from.DateTime;
108      string tooltip = string.Format("Task: {0} " + Environment.NewLine + "Task Id: {1}" + Environment.NewLine + "State: {2} " + Environment.NewLine + "Duration: {3} " + Environment.NewLine + "{4} - {5}" + Environment.NewLine, from.TaskName, from.TaskId, from.State, duration, from.DateTime, until);
109
110      if (to != null && to.SlaveId != null)
111        tooltip += "Slave: " + to.SlaveId;
112
113      if (!string.IsNullOrEmpty(from.Exception))
114        tooltip += Environment.NewLine + from.Exception;
115      ganttChart.AddData(name, from.State.ToString(), from.DateTime, until, tooltip, false);
116    }
117
118    protected override void SetEnabledStateOfControls() {
119      base.SetEnabledStateOfControls();
120    }
121
122    #region Events
123    void chart_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) {
124      HitTestResult result = ganttChart.chart.HitTest(e.X, e.Y);
125      if (result.ChartElementType == ChartElementType.LegendItem)
126        Cursor = Cursors.Hand;
127      else
128        Cursor = Cursors.Default;
129    }
130
131    private void chart_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
132      HitTestResult result = ganttChart.chart.HitTest(e.X, e.Y);
133      if (result.ChartElementType == ChartElementType.LegendItem)
134        ToggleLegendItemVisibility(result.Object as LegendItem);
135      ganttChart.Reset();
136      OnContentChanged();
137    }
138
139    private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
140      foreach (var item in e.LegendItems)
141        foreach (var cell in item.Cells)
142          cell.ForeColor = invisibleLegendItems.Any(x => x.Name == item.Name) ? Color.Gray : Color.Black;
143    }
144    #endregion
145
146    #region Helpers
147    private void ToggleLegendItemVisibility(LegendItem legendItem) {
148      var item = invisibleLegendItems.FirstOrDefault(x => x.Name == legendItem.Name);
149      if (item != null) invisibleLegendItems.Remove(item);
150      else invisibleLegendItems.Add(legendItem);
151    }
152    #endregion
153  }
154}
Note: See TracBrowser for help on using the repository browser.