Free cookie consent management tool by TermsFeed Policy Generator

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

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

#318 implemented persistence mechanism for new data row model

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