Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1173 was 1061, checked in by cbahner, 15 years ago

#320 optimization of persistence (save) + tests

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