Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Scheduling/HeuristicLab.Encodings.ScheduleEncoding.Views/3.3/GanttChart.cs @ 6479

Last change on this file since 6479 was 6406, checked in by jhelm, 14 years ago

#1329: Applied suggestions from codereview. Added unit-tests. Renamed encoding-project.

File size: 2.6 KB
RevLine 
[6406]1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Linq;
5using System.Windows.Forms;
6using System.Windows.Forms.DataVisualization.Charting;
7
8namespace HeuristicLab.Encodings.ScheduleEncoding.Views {
9  public partial class GanttChart : UserControl {
10
11    private IDictionary<int, Color> jobColors = new SortedDictionary<int, Color>();
12    private IDictionary<string, int> rowNames = new Dictionary<string, int>();
13   
14
15    public GanttChart() {
16      InitializeComponent();
17      chart.Series[0].YValueType = ChartValueType.Double;
18    }
19
20    public void AddJobColor(int jobNr) {
21      if (!jobColors.ContainsKey(jobNr)) {
22        Random r = new Random(jobNr + 1);
23        jobColors[jobNr] = Color.FromArgb (r.Next(256),r.Next(256),r.Next(256));
24        chart.Legends[0].CustomItems.Clear();
25        if (jobColors.Count > 1) {
26          int i = 0;
27          foreach (Color c in jobColors.Values) {
28            chart.Legends[0].CustomItems.Add(c, "Job#" + (i++));
29          }
30        }   
31      }
32    }
33
34    private void AddRowName(string rowName) {
35      if (!rowNames.ContainsKey(rowName)) {
36        int nextId = rowNames.Count == 0 ? 1 : rowNames.Values.Max() + 1;
37        rowNames.Add(rowName, nextId);
38      }
39    }
40
41    public void AddData(string rowName, int jobNr, double start, double end, string tooltip, bool showLabel = true) {
42      AddRowName(rowName);
43      var point = new DataPoint(rowNames[rowName], new double[] { start, end });
44      point.Color = jobColors[jobNr];
45      point.AxisLabel = rowName;
46      point.ToolTip = tooltip;
47      chart.Series[0].Points.Add(point);
48    }
49
50    public void Reset() {
51      chart.Series[0].Points.Clear();
52      jobColors.Clear();
53      chart.Legends[0].CustomItems.Clear();
54      rowNames.Clear();
55    }
56
57    void chart_MouseClick(object sender, MouseEventArgs e) {
58      var pos = e.Location;
59      var results = chart.HitTest(pos.X, pos.Y, false, ChartElementType.DataPoint);
60      foreach (DataPoint dp in chart.Series[0].Points) {
61        Color newColor = Color.FromArgb(255, dp.Color);
62        dp.Color = newColor;
63      }
64      foreach (var result in results) {
65        if (result.ChartElementType == ChartElementType.DataPoint) {
66          Color currentColor = chart.Series[0].Points[result.PointIndex].Color;
67          foreach (DataPoint dp in result.Series.Points) {
68            if (dp.Color != currentColor) {
69              Color newColor = Color.FromArgb(0, dp.Color);
70              dp.Color = newColor;
71            }
72          }
73        }
74      }
75    }
76  }
77}
Note: See TracBrowser for help on using the repository browser.