Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.ScheduleEncoding.Views/3.3/GanttChart.cs @ 11808

Last change on this file since 11808 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 4.3 KB
RevLine 
[8924]1#region License Information
2/* HeuristicLab
[11171]3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8924]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;
[6406]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
[6482]35
[6406]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);
[6482]44        jobColors[jobNr] = Color.FromArgb(r.Next(256), r.Next(256), r.Next(256));
[6406]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          }
[6482]51        }
[6406]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
[6482]62    public void AddData(string rowName, int jobNr, int taskNr, double start, double end, string tooltip, bool showLabel = true) {
[6406]63      AddRowName(rowName);
[6482]64      AddJobColor(jobNr);
65      JobDataPoint point = new JobDataPoint(rowNames[rowName], new double[] { start, end }, jobNr, taskNr);
[6406]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) {
[6482]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() {
[6406]95      foreach (DataPoint dp in chart.Series[0].Points) {
[6482]96        if (dp.GetType() == typeof(JobDataPoint))
97          dp.Color = jobColors[(dp as JobDataPoint).JobNr];
[6406]98      }
[6482]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;
[6406]112        }
113      }
114    }
[6482]115
[6406]116  }
117}
Note: See TracBrowser for help on using the repository browser.