Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.ScheduleEncoding.Views/3.3/GanttChart.cs @ 11704

Last change on this file since 11704 was 11170, checked in by ascheibe, 10 years ago

#2115 updated copyright year in stable branch

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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;
28
29namespace HeuristicLab.Encodings.ScheduleEncoding.Views {
30  public partial class GanttChart : UserControl {
31
32    private IDictionary<int, Color> jobColors = new SortedDictionary<int, Color>();
33    private IDictionary<string, int> rowNames = new Dictionary<string, int>();
34
35
36    public GanttChart() {
37      InitializeComponent();
38      chart.Series[0].YValueType = ChartValueType.Double;
39    }
40
41    public void AddJobColor(int jobNr) {
42      if (!jobColors.ContainsKey(jobNr)) {
43        Random r = new Random(jobNr + 1);
44        jobColors[jobNr] = Color.FromArgb(r.Next(256), r.Next(256), r.Next(256));
45        chart.Legends[0].CustomItems.Clear();
46        if (jobColors.Count > 1) {
47          int i = 0;
48          foreach (Color c in jobColors.Values) {
49            chart.Legends[0].CustomItems.Add(c, "Job#" + (i++));
50          }
51        }
52      }
53    }
54
55    private void AddRowName(string rowName) {
56      if (!rowNames.ContainsKey(rowName)) {
57        int nextId = rowNames.Count == 0 ? 1 : rowNames.Values.Max() + 1;
58        rowNames.Add(rowName, nextId);
59      }
60    }
61
62    public void AddData(string rowName, int jobNr, int taskNr, double start, double end, string tooltip, bool showLabel = true) {
63      AddRowName(rowName);
64      AddJobColor(jobNr);
65      JobDataPoint point = new JobDataPoint(rowNames[rowName], new double[] { start, end }, jobNr, taskNr);
66      point.Color = jobColors[jobNr];
67      point.AxisLabel = rowName;
68      point.ToolTip = tooltip;
69      chart.Series[0].Points.Add(point);
70    }
71
72    public void Reset() {
73      chart.Series[0].Points.Clear();
74      jobColors.Clear();
75      chart.Legends[0].CustomItems.Clear();
76      rowNames.Clear();
77    }
78
79    void chart_MouseClick(object sender, MouseEventArgs e) {
80      Point pos = e.Location;
81      HitTestResult[] results = chart.HitTest(pos.X, pos.Y, false, ChartElementType.DataPoint);
82      ResetDataColors();
83
84      foreach (HitTestResult result in results) {
85        if (result.ChartElementType == ChartElementType.DataPoint && result.Object.GetType() == typeof(JobDataPoint)) {
86          int currentJobNr = (result.Object as JobDataPoint).JobNr;
87          int currentTaskNr = (result.Object as JobDataPoint).TaskNr;
88
89          HighlightTaskAndJob(currentJobNr, currentTaskNr);
90        }
91      }
92    }
93
94    public void ResetDataColors() {
95      foreach (DataPoint dp in chart.Series[0].Points) {
96        if (dp.GetType() == typeof(JobDataPoint))
97          dp.Color = jobColors[(dp as JobDataPoint).JobNr];
98      }
99    }
100
101    public void HighlightTaskAndJob(int jobNr, int taskNr) {
102      foreach (DataPoint dp in chart.Series[0].Points) {
103        if (dp.GetType() == typeof(JobDataPoint) && ((dp as JobDataPoint).JobNr == jobNr) && ((dp as JobDataPoint).TaskNr == taskNr)) {
104          Color newColor = Color.FromArgb(255, dp.Color);
105          dp.Color = newColor;
106        } else if (dp.GetType() == typeof(JobDataPoint) && ((dp as JobDataPoint).JobNr == jobNr)) {
107          Color newColor = Color.FromArgb(180, dp.Color);
108          dp.Color = newColor;
109        } else if (dp.GetType() == typeof(JobDataPoint) && !((dp as JobDataPoint).JobNr == jobNr)) {
110          Color newColor = Color.FromArgb(0, dp.Color);
111          dp.Color = newColor;
112        }
113      }
114    }
115
116  }
117}
Note: See TracBrowser for help on using the repository browser.