using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace HeuristicLab.Encodings.ScheduleEncoding.Views { public partial class GanttChart : UserControl { private IDictionary jobColors = new SortedDictionary(); private IDictionary rowNames = new Dictionary(); public GanttChart() { InitializeComponent(); chart.Series[0].YValueType = ChartValueType.Double; } public void AddJobColor(int jobNr) { if (!jobColors.ContainsKey(jobNr)) { Random r = new Random(jobNr + 1); jobColors[jobNr] = Color.FromArgb (r.Next(256),r.Next(256),r.Next(256)); chart.Legends[0].CustomItems.Clear(); if (jobColors.Count > 1) { int i = 0; foreach (Color c in jobColors.Values) { chart.Legends[0].CustomItems.Add(c, "Job#" + (i++)); } } } } private void AddRowName(string rowName) { if (!rowNames.ContainsKey(rowName)) { int nextId = rowNames.Count == 0 ? 1 : rowNames.Values.Max() + 1; rowNames.Add(rowName, nextId); } } public void AddData(string rowName, int jobNr, double start, double end, string tooltip, bool showLabel = true) { AddRowName(rowName); var point = new DataPoint(rowNames[rowName], new double[] { start, end }); point.Color = jobColors[jobNr]; point.AxisLabel = rowName; point.ToolTip = tooltip; chart.Series[0].Points.Add(point); } public void Reset() { chart.Series[0].Points.Clear(); jobColors.Clear(); chart.Legends[0].CustomItems.Clear(); rowNames.Clear(); } void chart_MouseClick(object sender, MouseEventArgs e) { var pos = e.Location; var results = chart.HitTest(pos.X, pos.Y, false, ChartElementType.DataPoint); foreach (DataPoint dp in chart.Series[0].Points) { Color newColor = Color.FromArgb(255, dp.Color); dp.Color = newColor; } foreach (var result in results) { if (result.ChartElementType == ChartElementType.DataPoint) { Color currentColor = chart.Series[0].Points[result.PointIndex].Color; foreach (DataPoint dp in result.Series.Points) { if (dp.Color != currentColor) { Color newColor = Color.FromArgb(0, dp.Color); dp.Color = newColor; } } } } } } }