source: trunk/sources/HeuristicLab.Clients.Hive.Views/3.3/StateLog/StateLogGanttChartListView.cs @ 8107

Last change on this file since 8107 was 8107, checked in by jkarder, 7 years ago

#1709:

  • implemented filtering according to the legend items
  • added zoom support
File size: 7.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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    }
47
48    protected override void DeregisterContentEvents() {
49      // Deregister your event handlers here
50      ganttChart.chart.MouseMove -= new System.Windows.Forms.MouseEventHandler(chart_MouseDown);
51      ganttChart.chart.MouseDown -= new System.Windows.Forms.MouseEventHandler(chart_MouseDown);
52      ganttChart.chart.CustomizeLegend -= new EventHandler<CustomizeLegendEventArgs>(chart_CustomizeLegend);
53      base.DeregisterContentEvents();
54    }
55
56    protected override void RegisterContentEvents() {
57      base.RegisterContentEvents();
58      // Register your event handlers here
59      ganttChart.chart.MouseMove += new System.Windows.Forms.MouseEventHandler(chart_MouseMove);
60      ganttChart.chart.MouseDown += new System.Windows.Forms.MouseEventHandler(chart_MouseDown);
61      ganttChart.chart.CustomizeLegend += new EventHandler<CustomizeLegendEventArgs>(chart_CustomizeLegend);
62    }
63
64    protected override void OnContentChanged() {
65      base.OnContentChanged();
66      if (Content == null) {
67        ganttChart.Reset();
68      } else {
69        ganttChart.Reset();
70        SetupCategories(ganttChart);
71        if (Content.Count > 0) {
72          DateTime maxValue = Content.Max(x => x.Count > 0 ? x.Max(y => y.DateTime) : DateTime.MinValue);
73          DateTime minValue = Content.Min(x => x.Count > 0 ? x.Min(y => y.DateTime) : DateTime.MinValue);
74          DateTime upperLimit;
75          if (Content.All(x => x.Count > 0 ? (x.Last().State == TaskState.Finished || x.Last().State == TaskState.Failed || x.Last().State == TaskState.Aborted) : true)) {
76            upperLimit = DateTime.FromOADate(Math.Min(DateTime.Now.AddSeconds(10).ToOADate(), maxValue.AddSeconds(10).ToOADate()));
77          } else {
78            upperLimit = DateTime.Now;
79          }
80
81          if ((upperLimit - minValue) > TimeSpan.FromDays(1)) {
82            this.ganttChart.chart.Series[0].YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Date;
83          } else {
84            this.ganttChart.chart.Series[0].YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Time;
85          }
86
87          for (int i = Content.Count - 1; i >= 0; i--) {
88            for (int j = 0; j < Content[i].Count - 1; j++) {
89              if (Content[i][j].State != TaskState.Offline && invisibleLegendItems.All(x => x.Name != Content[i][j].State.ToString()))
90                AddData(ganttChart, i.ToString(), Content[i][j], Content[i][j + 1], upperLimit);
91            }
92            if (Content[i].Count > 0 && invisibleLegendItems.All(x => x.Name != Content[i][Content[i].Count - 1].State.ToString())) {
93              AddData(ganttChart, i.ToString(), Content[i][Content[i].Count - 1], null, upperLimit);
94            }
95          }
96        }
97      }
98    }
99
100    public static void SetupCategories(GanttChart ganttChart) {
101      ganttChart.AddCategory(TaskState.Offline.ToString(), Color.Gainsboro);
102      ganttChart.AddCategory(TaskState.Waiting.ToString(), Color.NavajoWhite);
103      ganttChart.AddCategory(TaskState.Paused.ToString(), Color.PaleVioletRed);
104      ganttChart.AddCategory(TaskState.Transferring.ToString(), Color.CornflowerBlue);
105      ganttChart.AddCategory(TaskState.Calculating.ToString(), Color.DarkGreen);
106      ganttChart.AddCategory(TaskState.Finished.ToString(), Color.White);
107      ganttChart.AddCategory(TaskState.Aborted.ToString(), Color.Orange);
108      ganttChart.AddCategory(TaskState.Failed.ToString(), Color.Red);
109    }
110
111    public static void AddData(GanttChart ganttChart, string name, StateLog from, StateLog to, DateTime upperLimit) {
112      DateTime until = to != null ? to.DateTime : upperLimit;
113      TimeSpan duration = until - from.DateTime;
114      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);
115
116      if (to != null && to.SlaveId != null)
117        tooltip += "Slave: " + to.SlaveId;
118
119      if (!string.IsNullOrEmpty(from.Exception))
120        tooltip += Environment.NewLine + from.Exception;
121      ganttChart.AddData(name, from.State.ToString(), from.DateTime, until, tooltip, false);
122    }
123
124    protected override void SetEnabledStateOfControls() {
125      base.SetEnabledStateOfControls();
126    }
127
128    #region Events
129    void chart_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) {
130      HitTestResult result = ganttChart.chart.HitTest(e.X, e.Y);
131      if (result.ChartElementType == ChartElementType.LegendItem)
132        Cursor = Cursors.Hand;
133      else
134        Cursor = Cursors.Default;
135    }
136
137    private void chart_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
138      HitTestResult result = ganttChart.chart.HitTest(e.X, e.Y);
139      if (result.ChartElementType == ChartElementType.LegendItem)
140        ToggleLegendItemVisibility(result.Object as LegendItem);
141      ganttChart.Reset();
142      OnContentChanged();
143    }
144
145    private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
146      foreach (var item in e.LegendItems)
147        foreach (var cell in item.Cells)
148          cell.ForeColor = invisibleLegendItems.Any(x => x.Name == item.Name) ? Color.Gray : Color.Black;
149    }
150    #endregion
151
152    #region Helpers
153    private void ToggleLegendItemVisibility(LegendItem legendItem) {
154      var item = invisibleLegendItems.FirstOrDefault(x => x.Name == legendItem.Name);
155      if (item != null) invisibleLegendItems.Remove(item);
156      else invisibleLegendItems.Add(legendItem);
157    }
158    #endregion
159  }
160}
Note: See TracBrowser for help on using the repository browser.