Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1049 was 979, checked in by cbahner, 16 years ago

#318 implemented persistence mechanism for new data row model

File size: 4.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.Xml;
5using HeuristicLab.Core;
6
7namespace HeuristicLab.Visualization{
8  public delegate void DataRowAddedHandler(IDataRow row);
9  public delegate void DataRowRemovedHandler(IDataRow row);
10  public delegate void ModelChangedHandler();
11
12  public class ChartDataRowsModel : ChartDataModelBase, IChartDataRowsModel{
13    private string title;
14    private string xAxisLabel;
15
16    private readonly List<IDataRow> rows = new List<IDataRow>();
17    private readonly List<string> xLabels = new List<string>();
18
19    public List<string> XLabels{
20      get { return xLabels; }
21    }
22
23    public List<IDataRow> Rows{
24      get { return rows; }
25    }
26
27    public string Title {
28      get { return title; }
29      set {
30        title = value;
31        OnModelChanged();
32      }
33    }
34    public string XAxisLabel {
35      get { return xAxisLabel; }
36      set {
37        xAxisLabel = value;
38        OnModelChanged();
39      }
40    }
41
42    public override IView CreateView() {
43      return new LineChart(this);
44    }
45
46    public void AddLabel(string label) {
47      xLabels.Add(label);
48      OnModelChanged();
49    }
50
51    public void AddLabel(string label, int index) {
52      xLabels[index] = label;
53      OnModelChanged();
54    }
55
56    public void AddLabels(string[] labels) {
57      foreach (var s in labels){
58        AddLabel(s);
59      }
60      //OnModelChanged();
61    }
62
63    public void AddLabels(string[] labels, int index) {
64      int i = 0;
65      foreach (var s in labels){
66        AddLabel(s, index + i);
67        i++;
68      }
69      //OnModelChanged();
70    }
71
72    public void ModifyLabel(string label, int index) {
73      xLabels[index] = label;
74      OnModelChanged();
75    }
76
77    public void ModifyLabels(string[] labels, int index) {
78      int i = 0;
79      foreach (var s in labels){
80        ModifyLabel(s, index + i);
81        i++;
82      }
83      //OnModelChanged();
84    }
85
86    public void RemoveLabel(int index) {
87      xLabels.RemoveAt(index);
88      OnModelChanged();
89    }
90
91    public void RemoveLabels(int index, int count) {
92      for (int i = index; i < index + count; i++ ){
93        RemoveLabel(i);
94      }
95      //OnModelChanged();
96    }
97
98    public void AddDataRow(IDataRow row) {
99      rows.Add(row);
100      OnDataRowAdded(row);
101    }
102
103    public void RemoveDataRow(IDataRow row) {
104      rows.Remove(row);
105      OnDataRowRemoved(row);
106    }
107
108    public event ModelChangedHandler ModelChanged;
109
110    protected void OnModelChanged() {
111      if (ModelChanged != null) {
112        ModelChanged();
113      }
114    }
115
116    public event DataRowAddedHandler DataRowAdded;
117
118    protected void OnDataRowAdded(IDataRow row) {
119      if (DataRowAdded != null) {
120        DataRowAdded(row);
121      }
122    }
123
124    public event DataRowRemovedHandler DataRowRemoved;
125
126    protected void OnDataRowRemoved(IDataRow row) {
127      if (DataRowRemoved != null) {
128        DataRowRemoved(row);
129      }
130    }
131
132    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
133      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
134
135      foreach (var row in rows) {
136        XmlNode columnElement = document.CreateNode(XmlNodeType.Element, "row", null);
137
138        XmlAttribute idAttr = document.CreateAttribute("label");
139        idAttr.Value = row.Label.ToString();
140        columnElement.Attributes.Append(idAttr);
141
142        for (int i = 0; i < row.Count; i++) {
143          if (i == 0) {
144            columnElement.InnerText += row[i].ToString(CultureInfo.InvariantCulture.NumberFormat);
145          } else {
146            columnElement.InnerText += ";" + row[i].ToString(CultureInfo.InvariantCulture.NumberFormat);
147          }
148        }
149        node.AppendChild(columnElement);
150      }
151      return node;   
152    }
153
154    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
155      base.Populate(node, restoredObjects);
156
157      foreach (XmlNode dataRow in node.ChildNodes) {
158        XmlAttributeCollection attrs = dataRow.Attributes;
159        XmlAttribute rowIdAttr = (XmlAttribute)attrs.GetNamedItem("label");
160        string rowLabel = rowIdAttr.Value;
161        DataRow row = new DataRow();
162        row.Label = rowLabel;
163
164        string[] tokens = dataRow.InnerText.Split(';');
165        double[] data = new double[tokens.Length];
166        for (int i = 0; i < data.Length; i++) {
167          if (tokens[i].Length != 0) {
168            if (
169              double.TryParse(tokens[i], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out data[i]) ==
170              false) {
171              throw new FormatException("Can't parse " + tokens[i] + " as double value.");
172            }
173          }
174        }
175        row.AddValues(data);
176
177        AddDataRow(row);
178      }
179    }
180  }
181}
Note: See TracBrowser for help on using the repository browser.