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
RevLine 
[680]1using System;
2using System.Collections.Generic;
[864]3using System.Globalization;
[726]4using System.Xml;
5using HeuristicLab.Core;
[1061]6using System.Text;
[680]7
[685]8namespace HeuristicLab.Visualization{
[761]9  public delegate void DataRowAddedHandler(IDataRow row);
10  public delegate void DataRowRemovedHandler(IDataRow row);
11  public delegate void ModelChangedHandler();
[685]12
[859]13  public class ChartDataRowsModel : ChartDataModelBase, IChartDataRowsModel{
[761]14    private string title;
15    private string xAxisLabel;
[726]16
[859]17    private readonly List<IDataRow> rows = new List<IDataRow>();
18    private readonly List<string> xLabels = new List<string>();
[726]19
[859]20    public List<string> XLabels{
21      get { return xLabels; }
[685]22    }
23
[859]24    public List<IDataRow> Rows{
25      get { return rows; }
[761]26    }
[685]27
[761]28    public string Title {
29      get { return title; }
30      set {
31        title = value;
32        OnModelChanged();
[728]33      }
[761]34    }
35    public string XAxisLabel {
36      get { return xAxisLabel; }
37      set {
38        xAxisLabel = value;
39        OnModelChanged();
40      }
[685]41    }
42
[859]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);
[761]60      }
[859]61      //OnModelChanged();
[726]62    }
63
[859]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    }
[697]72
[859]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++;
[697]83      }
[859]84      //OnModelChanged();
[697]85    }
86
[859]87    public void RemoveLabel(int index) {
88      xLabels.RemoveAt(index);
89      OnModelChanged();
[761]90    }
[726]91
[859]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
[761]99    public void AddDataRow(IDataRow row) {
100      rows.Add(row);
101      OnDataRowAdded(row);
[697]102    }
[726]103
[761]104    public void RemoveDataRow(IDataRow row) {
105      rows.Remove(row);
106      OnDataRowRemoved(row);
107    }
[726]108
[859]109    public event ModelChangedHandler ModelChanged;
110
111    protected void OnModelChanged() {
112      if (ModelChanged != null) {
113        ModelChanged();
114      }
115    }
116
[761]117    public event DataRowAddedHandler DataRowAdded;
[726]118
[761]119    protected void OnDataRowAdded(IDataRow row) {
120      if (DataRowAdded != null) {
121        DataRowAdded(row);
122      }
123    }
[726]124
[761]125    public event DataRowRemovedHandler DataRowRemoved;
[727]126
[761]127    protected void OnDataRowRemoved(IDataRow row) {
128      if (DataRowRemoved != null) {
129        DataRowRemoved(row);
[726]130      }
[761]131    }
[726]132
[761]133    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
[979]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
[1061]143        StringBuilder builder = new StringBuilder();
144
[979]145        for (int i = 0; i < row.Count; i++) {
146          if (i == 0) {
[1061]147            builder.Append(row[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
148            //columnElement.InnerText += row[i].ToString(CultureInfo.InvariantCulture.NumberFormat);
[979]149          } else {
[1061]150            builder.Append(";" + row[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
151            //columnElement.InnerText += ";" + row[i].ToString(CultureInfo.InvariantCulture.NumberFormat);
[979]152          }
153        }
[1061]154        columnElement.InnerText += builder.ToString();
[979]155        node.AppendChild(columnElement);
156      }
157      return node;   
[761]158    }
159
[726]160    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
[979]161      base.Populate(node, restoredObjects);
[731]162
[979]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      }
[726]185    }
[680]186  }
[761]187}
Note: See TracBrowser for help on using the repository browser.