Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Visualization.ChartControlsExtensions/3.3/GanttChart.cs @ 17366

Last change on this file since 17366 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

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