Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing Enhancements/HeuristicLab.DataPreprocessing.Views/3.4/LineChartView.cs @ 14983

Last change on this file since 14983 was 14983, checked in by pfleck, 7 years ago

#2709 Adapted DataTable/ScatterPlotControl to the recent (re-) merge of the -View and -Control.

File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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
34  [View("Line Chart View")]
35  [Content(typeof(LineChartContent), true)]
36  public partial class LineChartView : PreprocessingChartView {
37
38    protected Dictionary<string, DataRow> allInOneDataRows;
39    protected DataTable allInOneDataTable;
40    protected DataTableView allInOneDataTableView;
41
42    public LineChartView() {
43      InitializeComponent();
44      sizeGroupBox.Visible = false;
45
46      allInOneDataRows = new Dictionary<string, DataRow>();
47      allInOneDataTable = new DataTable();
48    }
49
50    public new LineChartContent Content {
51      get { return (LineChartContent)base.Content; }
52      set { base.Content = value; }
53    }
54
55    protected override void InitData() {
56      base.InitData();
57
58      allInOneDataRows.Clear();
59      foreach (var x in Content.VariableItemList.Select((v, i) => new { variable = v.Value, i })) {
60        var row = Content.CreateDataRow(x.variable, DataRowVisualProperties.DataRowChartType.Line);
61        row.VisualProperties.Color = Colors[x.i % Colors.Length];
62        allInOneDataRows.Add(x.variable, row);
63      }
64
65      allInOneDataTable.Rows.Clear();
66      var rows = Content.VariableItemList.CheckedItems.Select(v => allInOneDataRows[v.Value.Value]);
67      allInOneDataTable.Rows.AddRange(rows);
68    }
69
70    protected override int GetNumberOfVisibleDataTables() {
71      return Content.AllInOneMode ? 1 : base.GetNumberOfVisibleDataTables();
72    }
73    protected override IEnumerable<DataTableView> GetVisibleDataTables() {
74      if (Content.AllInOneMode) {
75        if (allInOneDataTableView == null)
76          allInOneDataTableView = new DataTableView() { Content = allInOneDataTable };
77        return new[] { allInOneDataTableView };
78      }
79      return base.GetVisibleDataTables();
80    }
81    protected override DataTable CreateDataTable(string variableName) {
82      var dt = new DataTable();
83      var row = Content.CreateDataRow(variableName, DataRowVisualProperties.DataRowChartType.Line);
84      dt.Rows.Add(row);
85
86      var validValues = row.Values.Where(x => !double.IsNaN(x) && !double.IsInfinity(x)).ToList();
87      if (validValues.Any()) {
88        try {
89          double axisMin, axisMax, axisInterval;
90          ChartUtil.CalculateOptimalAxisInterval(validValues.Min(), validValues.Max(), out axisMin, out axisMax, out axisInterval);
91          dt.VisualProperties.YAxisMinimumAuto = false;
92          dt.VisualProperties.YAxisMaximumAuto = false;
93          dt.VisualProperties.YAxisMinimumFixedValue = axisMin;
94          dt.VisualProperties.YAxisMaximumFixedValue = axisMax;
95        } catch (ArgumentOutOfRangeException) { }
96      }
97      return dt;
98    }
99
100    private void allInOneCheckBox_CheckedChanged(object sender, EventArgs e) {
101      Content.AllInOneMode = allInOneCheckBox.Checked;
102
103      sizeGroupBox.Visible = !allInOneCheckBox.Checked;
104
105      GenerateLayout();
106    }
107
108    protected override void OnContentChanged() {
109      base.OnContentChanged();
110      if (Content != null) {
111        allInOneCheckBox.Checked = Content.AllInOneMode;
112      }
113    }
114
115    protected override void CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> checkedItems) {
116      base.CheckedItemsChanged(sender, checkedItems);
117
118      foreach (IndexedItem<StringValue> item in checkedItems.Items) {
119        string variableName = item.Value.Value;
120
121        if (IsVariableChecked(variableName)) {
122          // ToDo: avoid clearing all rows, but how?
123          allInOneDataTable.Rows.Clear();
124          var rows = Content.VariableItemList.CheckedItems.Select(r => allInOneDataRows[r.Value.Value]);
125          allInOneDataTable.Rows.AddRange(rows);
126        } else {
127          allInOneDataTable.Rows.Remove(variableName);
128        }
129      }
130    }
131
132    #region Add/Remove/Update Variable, Reset
133    protected override void AddVariable(string name) {
134      base.AddVariable(name);
135      var row = Content.CreateDataRow(name, DataRowVisualProperties.DataRowChartType.Line);
136      allInOneDataTable.Rows.Add(row);
137    }
138
139    // remove variable from data table and item list
140    protected override void RemoveVariable(string name) {
141      base.RemoveVariable(name);
142      allInOneDataTable.Rows.Remove(name);
143    }
144
145    protected override void UpdateVariable(string name) {
146      base.UpdateVariable(name);
147      allInOneDataTable.Rows.Remove(name);
148      var newRow = Content.CreateDataRow(name, DataRowVisualProperties.DataRowChartType.Line);
149      allInOneDataTable.Rows.Add(newRow);
150    }
151    #endregion
152  }
153}
Note: See TracBrowser for help on using the repository browser.