Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2989_MovingPeaksBenchmark/HeuristicLab.DataPreprocessing.Views/3.4/LineChartView.cs @ 17607

Last change on this file since 17607 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Analysis;
26using HeuristicLab.Analysis.Views;
27using HeuristicLab.Collections;
28using HeuristicLab.Data;
29using HeuristicLab.MainForm;
30using HeuristicLab.Visualization.ChartControlsExtensions;
31
32namespace HeuristicLab.DataPreprocessing.Views {
33  [View("Line Chart View")]
34  [Content(typeof(LineChartContent), true)]
35  public partial class LineChartView : PreprocessingChartView {
36    protected Dictionary<string, DataRow> allInOneDataRows;
37    protected DataTable allInOneDataTable;
38    protected DataTableView allInOneDataTableView;
39
40    public new LineChartContent Content {
41      get { return (LineChartContent)base.Content; }
42      set { base.Content = value; }
43    }
44
45    public LineChartView() {
46      InitializeComponent();
47      sizeGroupBox.Visible = false;
48
49      allInOneDataRows = new Dictionary<string, DataRow>();
50      allInOneDataTable = new DataTable();
51    }
52
53    protected override void InitData() {
54      base.InitData();
55
56      allInOneDataRows.Clear();
57      foreach (var x in Content.VariableItemList.Select((v, i) => new { variable = v.Value, i })) {
58        var row = Content.CreateDataRow(x.variable, DataRowVisualProperties.DataRowChartType.Line);
59        row.VisualProperties.Color = Colors[x.i % Colors.Length];
60        allInOneDataRows.Add(x.variable, row);
61      }
62
63      allInOneDataTable.Rows.Clear();
64      var rows = Content.VariableItemList.CheckedItems.Select(v => allInOneDataRows[v.Value.Value]);
65      allInOneDataTable.Rows.AddRange(rows);
66    }
67
68    protected override int GetNumberOfVisibleDataTables() {
69      return Content.AllInOneMode ? 1 : base.GetNumberOfVisibleDataTables();
70    }
71    protected override IEnumerable<DataTableView> GetVisibleDataTables() {
72      if (Content.AllInOneMode) {
73        if (allInOneDataTableView == null)
74          allInOneDataTableView = new DataTableView() { Content = allInOneDataTable, ShowChartOnly = true };
75        return new[] { allInOneDataTableView };
76      }
77      return base.GetVisibleDataTables();
78    }
79    protected override DataTable CreateDataTable(string variableName) {
80      var dt = new DataTable();
81      var row = Content.CreateDataRow(variableName, DataRowVisualProperties.DataRowChartType.Line);
82      dt.Rows.Add(row);
83
84      var validValues = row.Values.Where(x => !double.IsNaN(x) && !double.IsInfinity(x)).ToList();
85      if (validValues.Any()) {
86        try {
87          double axisMin, axisMax, axisInterval;
88          ChartUtil.CalculateOptimalAxisInterval(validValues.Min(), validValues.Max(), out axisMin, out axisMax, out axisInterval);
89          dt.VisualProperties.YAxisMinimumAuto = false;
90          dt.VisualProperties.YAxisMaximumAuto = false;
91          dt.VisualProperties.YAxisMinimumFixedValue = axisMin;
92          dt.VisualProperties.YAxisMaximumFixedValue = axisMax;
93        } catch (ArgumentOutOfRangeException) { }
94      }
95      return dt;
96    }
97
98    private void allInOneCheckBox_CheckedChanged(object sender, EventArgs e) {
99      Content.AllInOneMode = allInOneCheckBox.Checked;
100
101      sizeGroupBox.Visible = !allInOneCheckBox.Checked;
102
103      GenerateLayout();
104    }
105
106    protected override void OnContentChanged() {
107      base.OnContentChanged();
108      if (Content != null) {
109        allInOneCheckBox.Checked = Content.AllInOneMode;
110      }
111    }
112
113    protected override void CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> checkedItems) {
114      base.CheckedItemsChanged(sender, checkedItems);
115
116      foreach (IndexedItem<StringValue> item in checkedItems.Items) {
117        string variableName = item.Value.Value;
118
119        if (IsVariableChecked(variableName)) {
120          // ToDo: avoid clearing all rows, but how?
121          allInOneDataTable.Rows.Clear();
122          var rows = Content.VariableItemList.CheckedItems.Select(r => allInOneDataRows[r.Value.Value]);
123          allInOneDataTable.Rows.AddRange(rows);
124        } else {
125          allInOneDataTable.Rows.Remove(variableName);
126        }
127      }
128    }
129
130    #region Add/Remove/Update Variable, Reset
131    protected override void AddVariable(string name) {
132      base.AddVariable(name);
133      var row = Content.CreateDataRow(name, DataRowVisualProperties.DataRowChartType.Line);
134      allInOneDataTable.Rows.Add(row);
135    }
136
137    // remove variable from data table and item list
138    protected override void RemoveVariable(string name) {
139      base.RemoveVariable(name);
140      allInOneDataTable.Rows.Remove(name);
141    }
142
143    protected override void UpdateVariable(string name) {
144      base.UpdateVariable(name);
145      allInOneDataTable.Rows.Remove(name);
146      var newRow = Content.CreateDataRow(name, DataRowVisualProperties.DataRowChartType.Line);
147      allInOneDataTable.Rows.Add(newRow);
148    }
149    #endregion
150  }
151}
Note: See TracBrowser for help on using the repository browser.