Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/ChartDataRowsModel.cs @ 1389

Last change on this file since 1389 was 1387, checked in by mstoeger, 15 years ago

Added a getter to the model for the default Y-axis descriptor. (#433)

File size: 7.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.Xml;
5using HeuristicLab.Core;
6using System.Text;
7using HeuristicLab.Visualization.LabelProvider;
8using HeuristicLab.Visualization.Options;
9
10namespace HeuristicLab.Visualization{
11  public delegate void DataRowAddedHandler(IDataRow row);
12  public delegate void DataRowRemovedHandler(IDataRow row);
13  public delegate void ModelChangedHandler();
14
15  public class ChartDataRowsModel : ChartDataModelBase, IChartDataRowsModel{
16    private string title = "Title";
17    //private string xAxisLabel;
18    private ILabelProvider labelProvider = new ContinuousLabelProvider("0.##");
19
20    private ViewSettings viewSettings = new ViewSettings();
21
22    public ILabelProvider XAxisLabelProvider {
23      get { return labelProvider; }
24      set{
25        this.labelProvider = value;
26        OnModelChanged();
27      }
28    }
29
30    public List<YAxisDescriptor> YAxes {
31      get {
32        Dictionary<YAxisDescriptor, object> yaxes = new Dictionary<YAxisDescriptor, object>();
33
34        foreach (IDataRow row in rows) {
35          yaxes[row.YAxis] = null;
36        }
37
38        return new List<YAxisDescriptor>(yaxes.Keys);
39      }
40    }
41
42
43    private readonly List<IDataRow> rows = new List<IDataRow>();
44    //private readonly List<string> xLabels = new List<string>();
45
46    //public List<string> XLabels{
47    //  get { return xLabels; }
48    //}
49
50    public List<IDataRow> Rows{
51      get { return rows; }
52    }
53
54    public string Title {
55      get { return title; }
56      set {
57        title = value;
58        OnModelChanged();
59      }
60    }
61    //public string XAxisLabel {
62    //  get { return xAxisLabel; }
63    //  set {
64    //    xAxisLabel = value;
65    //    OnModelChanged();
66    //  }
67    //}
68
69    public override IView CreateView() {
70      return new LineChart(this);
71    }
72
73    //public void AddLabel(string label) {
74    //  xLabels.Add(label);
75    //  OnModelChanged();
76    //}
77
78    //public void AddLabel(string label, int index) {
79    //  xLabels[index] = label;
80    //  OnModelChanged();
81    //}
82
83    //public void AddLabels(string[] labels) {
84    //  foreach (var s in labels){
85    //    AddLabel(s);
86    //  }
87    //  //OnModelChanged();
88    //}
89
90    //public void AddLabels(string[] labels, int index) {
91    //  int i = 0;
92    //  foreach (var s in labels){
93    //    AddLabel(s, index + i);
94    //    i++;
95    //  }
96    //  //OnModelChanged();
97    //}
98
99    //public void ModifyLabel(string label, int index) {
100    //  xLabels[index] = label;
101    //  OnModelChanged();
102    //}
103
104    //public void ModifyLabels(string[] labels, int index) {
105    //  int i = 0;
106    //  foreach (var s in labels){
107    //    ModifyLabel(s, index + i);
108    //    i++;
109    //  }
110    //  //OnModelChanged();
111    //}
112
113    //public void RemoveLabel(int index) {
114    //  xLabels.RemoveAt(index);
115    //  OnModelChanged();
116    //}
117
118    //public void RemoveLabels(int index, int count) {
119    //  for (int i = index; i < index + count; i++ ){
120    //    RemoveLabel(i);
121    //  }
122    //  //OnModelChanged();
123    //}
124
125    private readonly YAxisDescriptor defaultYAxisDescriptor = new YAxisDescriptor();
126
127    public void AddDataRow(IDataRow row) {
128      if (row.YAxis == null) {
129        row.YAxis = defaultYAxisDescriptor;
130      }
131      rows.Add(row);
132      OnDataRowAdded(row);
133    }
134
135    public void RemoveDataRow(IDataRow row) {
136      rows.Remove(row);
137      OnDataRowRemoved(row);
138    }
139
140    public YAxisDescriptor DefaultYAxis {
141      get { return defaultYAxisDescriptor; }
142    }
143
144    // TODO implement calculation of max data row values
145    public int MaxDataRowValues {
146      get {
147        int max = 0;
148
149        foreach (IDataRow row in rows) {
150          max = Math.Max(max, row.Count);
151        }
152
153        return max;
154      }
155    }
156
157    public ViewSettings ViewSettings {
158      get { return viewSettings; }
159      set { viewSettings = value; }
160    }
161
162    public event ModelChangedHandler ModelChanged;
163
164    protected void OnModelChanged() {
165      if (ModelChanged != null) {
166        ModelChanged();
167      }
168    }
169
170    public event DataRowAddedHandler DataRowAdded;
171
172    protected void OnDataRowAdded(IDataRow row) {
173      if (DataRowAdded != null) {
174        DataRowAdded(row);
175      }
176    }
177
178    public event DataRowRemovedHandler DataRowRemoved;
179
180    protected void OnDataRowRemoved(IDataRow row) {
181      if (DataRowRemoved != null) {
182        DataRowRemoved(row);
183      }
184    }
185
186    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
187      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
188
189      foreach (IDataRow row in rows) {
190        XmlNode columnElement = document.CreateNode(XmlNodeType.Element, "row", null);
191
192        XmlAttribute idAttr = document.CreateAttribute("label");
193        idAttr.Value = row.Label;
194        columnElement.Attributes.Append(idAttr);
195
196        StringBuilder builder = new StringBuilder();
197
198        for (int i = 0; i < row.Count; i++) {
199          if (i == 0) {
200            builder.Append(row[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
201            //columnElement.InnerText += row[i].ToString(CultureInfo.InvariantCulture.NumberFormat);
202          } else {
203            builder.Append(";" + row[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
204            //columnElement.InnerText += ";" + row[i].ToString(CultureInfo.InvariantCulture.NumberFormat);
205          }
206        }
207        columnElement.InnerText += builder.ToString();
208        node.AppendChild(columnElement);
209      }
210
211      XmlNode labelProviderNode = document.ImportNode(labelProvider.GetLabelProviderXmlNode(), true);
212      node.AppendChild(labelProviderNode);
213
214      return node;   
215    }
216
217    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
218      base.Populate(node, restoredObjects);
219
220      foreach (XmlNode dataRow in node.ChildNodes) {
221        if (dataRow.Name.Equals("LabelProvider")) {
222          labelProvider = labelProvider.PopulateLabelProviderXmlNode(dataRow);
223        } else {
224          XmlAttributeCollection attrs = dataRow.Attributes;
225          XmlAttribute rowIdAttr = (XmlAttribute)attrs.GetNamedItem("label");
226          string rowLabel = rowIdAttr.Value;
227          DataRow row = new DataRow();
228          row.Label = rowLabel;
229
230          string[] tokens = dataRow.InnerText.Split(';');
231          double[] data = new double[tokens.Length];
232          for (int i = 0; i < data.Length; i++) {
233            if (tokens[i].Length != 0) {
234              if (
235                double.TryParse(tokens[i], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out data[i]) ==
236                false) {
237                throw new FormatException("Can't parse " + tokens[i] + " as double value.");
238              }
239            }
240          }
241          row.AddValues(data);
242          AddDataRow(row);
243        }
244      }
245    }
246  }
247}
Note: See TracBrowser for help on using the repository browser.