1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Drawing;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Windows.Forms;
|
---|
6 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
7 |
|
---|
8 | namespace 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, int taskNr, double start, double end, string tooltip, bool showLabel = true) {
|
---|
42 | AddRowName(rowName);
|
---|
43 | AddJobColor(jobNr);
|
---|
44 | JobDataPoint point = new JobDataPoint(rowNames[rowName], new double[] { start, end }, jobNr, taskNr);
|
---|
45 | point.Color = jobColors[jobNr];
|
---|
46 | point.AxisLabel = rowName;
|
---|
47 | point.ToolTip = tooltip;
|
---|
48 | chart.Series[0].Points.Add(point);
|
---|
49 | }
|
---|
50 |
|
---|
51 | public void Reset() {
|
---|
52 | chart.Series[0].Points.Clear();
|
---|
53 | jobColors.Clear();
|
---|
54 | chart.Legends[0].CustomItems.Clear();
|
---|
55 | rowNames.Clear();
|
---|
56 | }
|
---|
57 |
|
---|
58 | void chart_MouseClick(object sender, MouseEventArgs e) {
|
---|
59 | Point pos = e.Location;
|
---|
60 | HitTestResult[] results = chart.HitTest(pos.X, pos.Y, false, ChartElementType.DataPoint);
|
---|
61 | ResetDataColors();
|
---|
62 |
|
---|
63 | foreach (HitTestResult result in results) {
|
---|
64 | if (result.ChartElementType == ChartElementType.DataPoint && result.Object.GetType() == typeof(JobDataPoint)) {
|
---|
65 | int currentJobNr = (result.Object as JobDataPoint).JobNr;
|
---|
66 | int currentTaskNr = (result.Object as JobDataPoint).TaskNr;
|
---|
67 |
|
---|
68 | HighlightTaskAndJob(currentJobNr, currentTaskNr);
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | public void ResetDataColors() {
|
---|
74 | foreach (DataPoint dp in chart.Series[0].Points) {
|
---|
75 | if (dp.GetType() == typeof(JobDataPoint))
|
---|
76 | dp.Color = jobColors[(dp as JobDataPoint).JobNr];
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | public void HighlightTaskAndJob(int jobNr, int taskNr) {
|
---|
81 | foreach (DataPoint dp in chart.Series[0].Points) {
|
---|
82 | if (dp.GetType() == typeof(JobDataPoint) && ((dp as JobDataPoint).JobNr == jobNr) && ((dp as JobDataPoint).TaskNr == taskNr)) {
|
---|
83 | Color newColor = Color.FromArgb(255, dp.Color);
|
---|
84 | dp.Color = newColor;
|
---|
85 | } else if (dp.GetType() == typeof(JobDataPoint) && ((dp as JobDataPoint).JobNr == jobNr)) {
|
---|
86 | Color newColor = Color.FromArgb(180, dp.Color);
|
---|
87 | dp.Color = newColor;
|
---|
88 | } else if (dp.GetType() == typeof(JobDataPoint) && !((dp as JobDataPoint).JobNr == jobNr)) {
|
---|
89 | Color newColor = Color.FromArgb(0, dp.Color);
|
---|
90 | dp.Color = newColor;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | }
|
---|
96 | }
|
---|