source: branches/HeuristicLab.DatastreamAnalysis/HeuristicLab.DatastreamAnalysis.Views/3.4/DataBarSetView.cs @ 14547

Last change on this file since 14547 was 14547, checked in by jzenisek, 5 years ago

#2719 added type and view for representing dictionary<string,double> as bar chart (implementation in progress)

File size: 6.9 KB
RevLine 
[14547]1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Drawing;
26using System.Linq;
27using System.Windows.Forms;
28using System.Windows.Forms.DataVisualization.Charting;
29using HeuristicLab.Core.Views;
30using HeuristicLab.MainForm;
31
32namespace HeuristicLab.DatastreamAnalysis.Views {
33  [View("DataBarSet")]
34  [Content(typeof(DataBarSet), false)]
35  public partial class DataBarSetView : ItemView {
36    private bool updateInProgress;
37
38    public virtual Image ViewImage {
39      get { return HeuristicLab.Common.Resources.VSImageLibrary.Graph; }
40    }
41
42    public new DataBarSet Content {
43      get { return (DataBarSet) base.Content; }
44      set { base.Content = value; }
45    }
46
47    public DataBarSetView() : base() {
48      InitializeComponent();
49      updateInProgress = false;
50
51      this.chart.CustomizeAllChartAreas();
52      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
53      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
54      this.chart.ChartAreas[0].AxisX.Title = "Ensembles";
55      this.chart.ChartAreas[0].AxisX.Maximum = 0.0;
56      this.chart.ChartAreas[0].AxisX.Maximum = Content.Bars.Count;
57      //AddCustomLabelsToAxis(this.chart.ChartAreas[0].AxisX);
58
59      this.chart.ChartAreas[0].AxisY.Title = "Estimated Values";
60      this.chart.ChartAreas[0].AxisY.IsStartedFromZero = true;
61      this.chart.ChartAreas[0].AxisY.Minimum = 0.0;
62      this.chart.ChartAreas[0].AxisY.Maximum = 1.0;
63      this.chart.ChartAreas[0].AxisY.Interval = 0.1;
64      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
65      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
66    }
67
68    private void AddCustomLabelsToAxis(Axis axis) {
69      axis.CustomLabels.Clear();
70
71      for(int i = 0; i < Content.Bars.Count; i++) {
72        var bar = Content.Bars.Keys.ToList()[i].Value;
73        CustomLabel barLabel = new CustomLabel();
74        barLabel.Text = bar;
75        barLabel.FromPosition = i;
76        barLabel.ToPosition = i + 1;
77        axis.CustomLabels.Add(barLabel);
78      }
79    }
80
81    protected override void RegisterContentEvents() {
82      base.RegisterContentEvents();
83      Content.BarsPropertyChanged += new EventHandler(Content_BarsChanged);
84      Content.BarValuesChanged += new EventHandler(Content_BarValuesChanged);
85      Content.ThresholdsPropertyChanged += new EventHandler(Content_ThresholdsChanged);
86    }
87
88    protected override void DeregisterContentEvents() {
89      base.DeregisterContentEvents();
90      Content.BarsPropertyChanged -= new EventHandler(Content_BarsChanged);
91      Content.BarValuesChanged -= new EventHandler(Content_BarValuesChanged);
92      Content.ThresholdsPropertyChanged -= new EventHandler(Content_ThresholdsChanged);
93    }
94
95    private void Content_BarsChanged(object sender, EventArgs e) {
96      UpdateChart();
97    }
98
99    private void Content_BarValuesChanged(object sender, EventArgs e) {
100      UpdateChartValues();
101    }
102
103    private void Content_ThresholdsChanged(object sender, EventArgs e) {
104      UpdateChart();
105    }
106
107    protected override void OnContentChanged() {
108      base.OnContentChanged();
109      UpdateChart();
110    }
111    private void UpdateChart() {
112      if (updateInProgress) {
113        Invoke((Action) UpdateChart);
114      }
115
116      updateInProgress = true;
117
118      // TODO
119      // clear the whole chart and redraw everything
120
121
122      updateInProgress = false;
123    }
124
125    private void UpdateChartValues() {
126      if (updateInProgress) {
127        Invoke((Action)UpdateChart);
128      }
129
130      updateInProgress = true;
131
132      // TODO
133      // simply update the bars' values
134
135      updateInProgress = false;
136    }
137
138    private void AddThresholds() {
139
140      foreach (var threshold in Content.Thresholds) {
141        // TODO: add threshold as red bold line to each of bars' conlumns
142      }
143
144    }
145
146    private TextAnnotation CreateTextAnnotation(string name, int classIndex, Axis axisX, Axis axisY, double x, double y, ContentAlignment alignment) {
147      TextAnnotation annotation = new TextAnnotation();
148      annotation.Text = name;
149      annotation.AllowMoving = true;
150      annotation.AllowResizing = false;
151      annotation.AllowSelecting = false;
152      annotation.IsSizeAlwaysRelative = true;
153      annotation.ClipToChartArea = chart.ChartAreas[0].Name;
154      annotation.Tag = classIndex;
155      annotation.AxisX = axisX;
156      annotation.AxisY = axisY;
157      annotation.Alignment = alignment;
158      annotation.X = x;
159      annotation.Y = y;
160      return annotation;
161    }
162
163
164    #region user interaction events
165    private void chart_MouseMove(object sender, MouseEventArgs e) {
166      HitTestResult result = chart.HitTest(e.X, e.Y);
167      if (result.ChartElementType == ChartElementType.LegendItem)
168        this.Cursor = Cursors.Hand;
169      else
170        this.Cursor = Cursors.Default;
171    }
172
173    private void chart_MouseDown(object sender, MouseEventArgs e) {
174      HitTestResult result = chart.HitTest(e.X, e.Y);
175      if (result.ChartElementType == ChartElementType.LegendItem) {
176        if (result.Series != null) ToggleSeries(result.Series);
177      }
178    }
179
180    private void ToggleSeries(Series series) {
181      if (series.Points.Count == 0) {
182        // TODO
183      } else {
184        series.Points.Clear();
185      }
186    }
187
188    private void chart_AnnotationPositionChanging(object sender, AnnotationPositionChangingEventArgs e) {
189      int classIndex = (int)e.Annotation.Tag;
190      //double[] thresholds = Content.Model.Thresholds.ToArray();
191      //thresholds[classIndex] = e.NewLocationY;
192      //Array.Sort(thresholds);
193      //Content.Model.SetThresholdsAndClassValues(thresholds, Content.Model.ClassValues);
194    }
195
196    private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
197      foreach (LegendItem legendItem in e.LegendItems) {
198        var series = chart.Series[legendItem.SeriesName];
199        if (series != null) {
200          bool seriesIsInvisible = series.Points.Count == 0;
201          foreach (LegendCell cell in legendItem.Cells)
202            cell.ForeColor = seriesIsInvisible ? Color.Gray : Color.Black;
203        }
204      }
205    }
206    #endregion
207  }
208}
Note: See TracBrowser for help on using the repository browser.