Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Visualization.ChartControlsExtensions/3.3/GanttChart.cs @ 14035

Last change on this file since 14035 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

File size: 2.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Visualization.ChartControlsExtensions {
30  public partial class GanttChart : UserControl {
31
32    private IDictionary<string, Color> categories = new Dictionary<string, Color>();
33    private IDictionary<string, int> rowNames = new Dictionary<string, int>();
34
35    public GanttChart() {
36      InitializeComponent();
37      chart.CustomizeAllChartAreas();
38    }
39
40    public void AddCategory(string name, Color color) {
41      categories[name] = color;
42      chart.Legends[0].CustomItems.Add(color, name);
43    }
44
45    public void AddData(string rowName, string category, DateTime start, DateTime end, string tooltip, bool showLabel = true) {
46      AddRowName(rowName);
47      var point = CreateDataPoint(rowNames[rowName], rowName, start, end, showLabel ? category : string.Empty, categories[category]);
48      point.ToolTip = tooltip;
49      chart.Series[0].Points.Add(point);
50    }
51
52    private void AddRowName(string rowName) {
53      if (!rowNames.ContainsKey(rowName)) {
54        int nextId = rowNames.Count == 0 ? 1 : rowNames.Values.Max() + 1;
55        rowNames.Add(rowName, nextId);
56      }
57    }
58
59    private static DataPoint CreateDataPoint(double x, string axisLabel, DateTime start, DateTime end, string text, Color color) {
60      var point = new DataPoint(x, new double[] { start.ToOADate(), end.ToOADate() });
61      point.Color = color;
62      point.Label = text;
63      point.AxisLabel = axisLabel;
64      return point;
65    }
66
67    public void Reset() {
68      chart.Series[0].Points.Clear();
69      categories.Clear();
70      chart.Legends[0].CustomItems.Clear();
71      rowNames.Clear();
72    }
73  }
74}
Note: See TracBrowser for help on using the repository browser.