Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1385 was 1385, checked in by gkragl, 15 years ago

Implemented persistence mechanism for XAxisLabelProvider (#434)

File size: 6.9 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    // TODO implement calculation of max data row values
141    public int MaxDataRowValues {
142      get {
143        int max = 0;
144
145        foreach (IDataRow row in rows) {
146          max = Math.Max(max, row.Count);
147        }
148
149        return max;
150      }
151    }
152
153    public ViewSettings ViewSettings {
154      get { return viewSettings; }
155      set { viewSettings = value; }
156    }
157
158    public event ModelChangedHandler ModelChanged;
159
160    protected void OnModelChanged() {
161      if (ModelChanged != null) {
162        ModelChanged();
163      }
164    }
165
166    public event DataRowAddedHandler DataRowAdded;
167
168    protected void OnDataRowAdded(IDataRow row) {
169      if (DataRowAdded != null) {
170        DataRowAdded(row);
171      }
172    }
173
174    public event DataRowRemovedHandler DataRowRemoved;
175
176    protected void OnDataRowRemoved(IDataRow row) {
177      if (DataRowRemoved != null) {
178        DataRowRemoved(row);
179      }
180    }
181
182    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
183      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
184
185      foreach (IDataRow row in rows) {
186        XmlNode columnElement = document.CreateNode(XmlNodeType.Element, "row", null);
187
188        XmlAttribute idAttr = document.CreateAttribute("label");
189        idAttr.Value = row.Label;
190        columnElement.Attributes.Append(idAttr);
191
192        StringBuilder builder = new StringBuilder();
193
194        for (int i = 0; i < row.Count; i++) {
195          if (i == 0) {
196            builder.Append(row[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
197            //columnElement.InnerText += row[i].ToString(CultureInfo.InvariantCulture.NumberFormat);
198          } else {
199            builder.Append(";" + row[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
200            //columnElement.InnerText += ";" + row[i].ToString(CultureInfo.InvariantCulture.NumberFormat);
201          }
202        }
203        columnElement.InnerText += builder.ToString();
204        node.AppendChild(columnElement);
205      }
206
207      XmlNode labelProviderNode = document.ImportNode(labelProvider.GetLabelProviderXmlNode(), true);
208      node.AppendChild(labelProviderNode);
209
210      return node;   
211    }
212
213    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
214      base.Populate(node, restoredObjects);
215
216      foreach (XmlNode dataRow in node.ChildNodes) {
217        if (dataRow.Name.Equals("LabelProvider")) {
218          labelProvider = labelProvider.PopulateLabelProviderXmlNode(dataRow);
219        } else {
220          XmlAttributeCollection attrs = dataRow.Attributes;
221          XmlAttribute rowIdAttr = (XmlAttribute)attrs.GetNamedItem("label");
222          string rowLabel = rowIdAttr.Value;
223          DataRow row = new DataRow();
224          row.Label = rowLabel;
225
226          string[] tokens = dataRow.InnerText.Split(';');
227          double[] data = new double[tokens.Length];
228          for (int i = 0; i < data.Length; i++) {
229            if (tokens[i].Length != 0) {
230              if (
231                double.TryParse(tokens[i], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out data[i]) ==
232                false) {
233                throw new FormatException("Can't parse " + tokens[i] + " as double value.");
234              }
235            }
236          }
237          row.AddValues(data);
238          AddDataRow(row);
239        }
240      }
241    }
242  }
243}
Note: See TracBrowser for help on using the repository browser.