Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14588 was 14588, checked in by jzenisek, 8 years ago

#2719 updated databar set and view

File size: 9.8 KB
Line 
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), true)]
35  public partial class DataBarSetView : ItemView {
36    private bool updateInProgress;
37    private string seriesName = "Ensembles";
38
39    public virtual Image ViewImage {
40      get { return HeuristicLab.Common.Resources.VSImageLibrary.Graph; }
41    }
42
43    public new DataBarSet Content {
44      get { return (DataBarSet) base.Content; }
45      set { base.Content = value; }
46    }
47
48    public DataBarSetView() : base() {
49      InitializeComponent();
50      updateInProgress = false;
51
52      this.chart.CustomizeAllChartAreas();
53      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
54      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
55      this.chart.ChartAreas[0].AxisX.Title = "Ensembles";
56      //this.chart.ChartAreas[0].AxisX.Maximum = 0.0;
57      //this.chart.ChartAreas[0].AxisX.Maximum = (Content != null && Content.Bars != null) ? Content.Bars.Count : 0.0;
58      //AddCustomLabelsToAxis(this.chart.ChartAreas[0].AxisX);
59
60      this.chart.ChartAreas[0].AxisY.Title = "Estimation Quality";
61      this.chart.ChartAreas[0].AxisY.IsStartedFromZero = true;
62      this.chart.ChartAreas[0].AxisY.Minimum = 0.0;
63      this.chart.ChartAreas[0].AxisY.Maximum = 1.0;
64      this.chart.ChartAreas[0].AxisY.Interval = 0.1;
65      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
66      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
67    }
68
69    private void AddCustomLabelsToAxis(Axis axis) {
70      axis.CustomLabels.Clear();
71
72      var bars = Content.Bars.ToList();
73      for(int i = 0; i < Content.Bars.Count; i++) {
74        CustomLabel barLabel = new CustomLabel();
75        barLabel.Text = bars[i].Name;
76        barLabel.FromPosition = i;
77        barLabel.ToPosition = i + 1;
78        axis.CustomLabels.Add(barLabel);
79      }
80    }
81
82    protected override void RegisterContentEvents() {
83      base.RegisterContentEvents();
84      Content.BarsPropertyChanged += new EventHandler(Content_BarsChanged);
85      Content.BarValueChanged += new EventHandler(Content_BarValuesChanged);
86      Content.ThresholdsPropertyChanged += new EventHandler(Content_ThresholdsChanged);
87    }
88
89    protected override void DeregisterContentEvents() {
90      base.DeregisterContentEvents();
91      Content.BarsPropertyChanged -= new EventHandler(Content_BarsChanged);
92      Content.BarValueChanged -= new EventHandler(Content_BarValuesChanged);
93      Content.ThresholdsPropertyChanged -= new EventHandler(Content_ThresholdsChanged);
94    }
95
96    private void Content_BarsChanged(object sender, EventArgs e) {
97      UpdateChart();
98    }
99
100    private void Content_BarValuesChanged(object sender, EventArgs e) {
101      UpdateChartValues();
102    }
103
104    private void Content_ThresholdsChanged(object sender, EventArgs e) {
105      UpdateChart();
106    }
107
108    protected override void OnContentChanged() {
109      base.OnContentChanged();
110      UpdateChart();
111    }
112    private void UpdateChart() {
113      if (InvokeRequired) {
114        Invoke((Action) UpdateChart);
115      } else if(!updateInProgress) {
116        if (Content == null) return;
117
118        updateInProgress = true;
119
120        chart.Series.Clear();
121        Series series = chart.Series.Add(seriesName);
122        series.IsVisibleInLegend = false;
123        series.ChartType = SeriesChartType.Column;
124        int i = 0;
125        foreach (var bar in Content.Bars) {
126          series.Points.Add(new DataPoint(i, bar.Value) { AxisLabel = bar.Name, Color = bar.BarColor });
127          i++;
128        }
129
130        //chart.ChartAreas[0].RecalculateAxesScale();
131        AddThresholds();
132        CheckThresholds();
133
134        chart.Refresh();
135        updateInProgress = false;
136      }
137    }
138
139    private void UpdateChartValues() {
140      if (InvokeRequired) {
141        Invoke((Action) UpdateChartValues);
142      } else if(!updateInProgress) {
143        if (Content == null) return;
144
145        updateInProgress = true;
146
147        //chart.Series[seriesName].Points.Clear();
148        //foreach (var bar in Content.Bars) {
149        //  chart.Series[seriesName].Points.AddXY(bar.Name, bar.Value);
150        //}
151
152        var bars = Content.Bars.ToList();
153
154        for (int i = 0; i < bars.Count; i++) {
155          chart.Series[seriesName].Points.ElementAt(i).SetValueY(bars[i].Value);
156        }
157
158        CheckThresholds();
159        //chart.ChartAreas[0].RecalculateAxesScale();
160        chart.Refresh();
161        updateInProgress = false;
162      }
163    }
164
165    private void CheckThresholds() {
166      var bars = Content.Bars.ToList();
167      for (int i = 0; i < bars.Count; i++) {
168        var bar = bars[i];
169        if (bar.Value > bar.Threshold) {
170          chart.Series[seriesName].Points[i].Color = Color.Orange;
171        } else {
172          chart.Series[seriesName].Points[i].Color = bar.BarColor;
173        }
174      }
175    }
176
177    private void AddThresholdsOld() {
178      Series series = chart.Series.Add("Thresholds");
179      series.IsVisibleInLegend = false;
180      series.ChartType = SeriesChartType.StackedColumn;
181
182      int i = 0;
183      foreach (var bar in Content.Bars) {
184        //series.Points.Add(new DataPoint(i, new [] { bar.Threshold, bar.Threshold + 0.05 }) { AxisLabel = bar.Name, Color = Color.Transparent, BorderWidth = 2, BorderColor = bar.ThresholdColor});
185        series.Points.Add(new DataPoint(i, new[] { bar.Threshold, bar.Threshold + 0.05 }) { AxisLabel = bar.Name, Color = bar.ThresholdColor });
186        i++;
187      }
188    }
189
190    private void AddThresholds() {
191      chart.Annotations.Clear();
192
193      var bars = Content.Bars.ToList();
194      for (int i = 0; i < bars.Count; i++) {
195        HorizontalLineAnnotation annotation = new HorizontalLineAnnotation();
196        annotation.AllowMoving = false;
197        annotation.AllowResizing = false;
198        annotation.LineWidth = 2;
199        annotation.LineColor = bars[i].ThresholdColor;
200        annotation.IsInfinitive = false;
201        annotation.ClipToChartArea = chart.ChartAreas[0].Name;
202        annotation.AxisX = chart.ChartAreas[0].AxisX;
203        annotation.AxisY = chart.ChartAreas[0].AxisY;
204        annotation.Y = bars[i].Threshold;
205
206        annotation.Alignment = ContentAlignment.MiddleCenter;
207        annotation.AnchorX = i;
208
209        annotation.X = i - 0.4;
210        annotation.Width = 52.0 / bars.Count;
211        annotation.Name = bars[i].Name;
212        annotation.Tag = bars[i].Name;
213        annotation.BringToFront();
214
215       
216
217        chart.Annotations.Add(annotation);
218      }
219    }
220
221    private TextAnnotation CreateTextAnnotation(string name, int classIndex, Axis axisX, Axis axisY, double x, double y, ContentAlignment alignment) {
222      TextAnnotation annotation = new TextAnnotation();
223      annotation.Text = name;
224      annotation.AllowMoving = true;
225      annotation.AllowResizing = false;
226      annotation.AllowSelecting = false;
227      annotation.IsSizeAlwaysRelative = true;
228      annotation.ClipToChartArea = chart.ChartAreas[0].Name;
229      annotation.Tag = classIndex;
230      annotation.AxisX = axisX;
231      annotation.AxisY = axisY;
232      annotation.Alignment = alignment;
233      annotation.X = x;
234      annotation.Y = y;
235      return annotation;
236    }
237
238
239    #region user interaction events
240    private void chart_MouseMove(object sender, MouseEventArgs e) {
241      HitTestResult result = chart.HitTest(e.X, e.Y);
242      if (result.ChartElementType == ChartElementType.LegendItem)
243        this.Cursor = Cursors.Hand;
244      else
245        this.Cursor = Cursors.Default;
246    }
247
248    private void chart_MouseDown(object sender, MouseEventArgs e) {
249      HitTestResult result = chart.HitTest(e.X, e.Y);
250      if (result.ChartElementType == ChartElementType.LegendItem) {
251        if (result.Series != null) ToggleSeries(result.Series);
252      }
253    }
254
255    private void ToggleSeries(Series series) {
256      if (series.Points.Count == 0) {
257        // TODO
258      } else {
259        series.Points.Clear();
260      }
261    }
262
263    private void chart_AnnotationPositionChanging(object sender, AnnotationPositionChangingEventArgs e) {
264      int classIndex = (int)e.Annotation.Tag;
265      //double[] thresholds = Content.Model.Thresholds.ToArray();
266      //thresholds[classIndex] = e.NewLocationY;
267      //Array.Sort(thresholds);
268      //Content.Model.SetThresholdsAndClassValues(thresholds, Content.Model.ClassValues);
269    }
270
271    private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
272      foreach (LegendItem legendItem in e.LegendItems) {
273        var series = chart.Series[legendItem.SeriesName];
274        if (series != null) {
275          bool seriesIsInvisible = series.Points.Count == 0;
276          foreach (LegendCell cell in legendItem.Cells)
277            cell.ForeColor = seriesIsInvisible ? Color.Gray : Color.Black;
278        }
279      }
280    }
281    #endregion
282  }
283}
Note: See TracBrowser for help on using the repository browser.