Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1389 was 1387, checked in by mstoeger, 16 years ago

Added a getter to the model for the default Y-axis descriptor. (#433)

File size: 7.0 KB
RevLine 
[680]1using System;
2using System.Collections.Generic;
[864]3using System.Globalization;
[726]4using System.Xml;
5using HeuristicLab.Core;
[1061]6using System.Text;
[1194]7using HeuristicLab.Visualization.LabelProvider;
[1341]8using HeuristicLab.Visualization.Options;
[680]9
[685]10namespace HeuristicLab.Visualization{
[761]11  public delegate void DataRowAddedHandler(IDataRow row);
12  public delegate void DataRowRemovedHandler(IDataRow row);
13  public delegate void ModelChangedHandler();
[685]14
[859]15  public class ChartDataRowsModel : ChartDataModelBase, IChartDataRowsModel{
[1182]16    private string title = "Title";
[1190]17    //private string xAxisLabel;
[1194]18    private ILabelProvider labelProvider = new ContinuousLabelProvider("0.##");
[726]19
[1341]20    private ViewSettings viewSettings = new ViewSettings();
21
[1190]22    public ILabelProvider XAxisLabelProvider {
23      get { return labelProvider; }
[1191]24      set{
25        this.labelProvider = value;
26        OnModelChanged();
27      }
[1190]28    }
29
[1350]30    public List<YAxisDescriptor> YAxes {
31      get {
32        Dictionary<YAxisDescriptor, object> yaxes = new Dictionary<YAxisDescriptor, object>();
[1190]33
[1350]34        foreach (IDataRow row in rows) {
35          yaxes[row.YAxis] = null;
36        }
37
38        return new List<YAxisDescriptor>(yaxes.Keys);
39      }
40    }
41
42
[859]43    private readonly List<IDataRow> rows = new List<IDataRow>();
[1190]44    //private readonly List<string> xLabels = new List<string>();
[726]45
[1190]46    //public List<string> XLabels{
47    //  get { return xLabels; }
48    //}
[685]49
[859]50    public List<IDataRow> Rows{
51      get { return rows; }
[761]52    }
[685]53
[761]54    public string Title {
55      get { return title; }
56      set {
57        title = value;
58        OnModelChanged();
[728]59      }
[761]60    }
[1190]61    //public string XAxisLabel {
62    //  get { return xAxisLabel; }
63    //  set {
64    //    xAxisLabel = value;
65    //    OnModelChanged();
66    //  }
67    //}
[685]68
[859]69    public override IView CreateView() {
70      return new LineChart(this);
71    }
72
[1190]73    //public void AddLabel(string label) {
74    //  xLabels.Add(label);
75    //  OnModelChanged();
76    //}
[859]77
[1190]78    //public void AddLabel(string label, int index) {
79    //  xLabels[index] = label;
80    //  OnModelChanged();
81    //}
[859]82
[1190]83    //public void AddLabels(string[] labels) {
84    //  foreach (var s in labels){
85    //    AddLabel(s);
86    //  }
87    //  //OnModelChanged();
88    //}
[726]89
[1190]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    //}
[697]98
[1190]99    //public void ModifyLabel(string label, int index) {
100    //  xLabels[index] = label;
101    //  OnModelChanged();
102    //}
[859]103
[1190]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    //}
[697]112
[1190]113    //public void RemoveLabel(int index) {
114    //  xLabels.RemoveAt(index);
115    //  OnModelChanged();
116    //}
[726]117
[1190]118    //public void RemoveLabels(int index, int count) {
119    //  for (int i = index; i < index + count; i++ ){
120    //    RemoveLabel(i);
121    //  }
122    //  //OnModelChanged();
123    //}
[859]124
[1350]125    private readonly YAxisDescriptor defaultYAxisDescriptor = new YAxisDescriptor();
126
[761]127    public void AddDataRow(IDataRow row) {
[1350]128      if (row.YAxis == null) {
129        row.YAxis = defaultYAxisDescriptor;
130      }
[761]131      rows.Add(row);
132      OnDataRowAdded(row);
[697]133    }
[726]134
[761]135    public void RemoveDataRow(IDataRow row) {
136      rows.Remove(row);
137      OnDataRowRemoved(row);
138    }
[726]139
[1387]140    public YAxisDescriptor DefaultYAxis {
141      get { return defaultYAxisDescriptor; }
142    }
143
[1285]144    // TODO implement calculation of max data row values
145    public int MaxDataRowValues {
146      get {
147        int max = 0;
148
149        foreach (IDataRow row in rows) {
150          max = Math.Max(max, row.Count);
151        }
152
153        return max;
154      }
155    }
156
[1341]157    public ViewSettings ViewSettings {
158      get { return viewSettings; }
159      set { viewSettings = value; }
160    }
161
[859]162    public event ModelChangedHandler ModelChanged;
163
164    protected void OnModelChanged() {
165      if (ModelChanged != null) {
166        ModelChanged();
167      }
168    }
169
[761]170    public event DataRowAddedHandler DataRowAdded;
[726]171
[761]172    protected void OnDataRowAdded(IDataRow row) {
173      if (DataRowAdded != null) {
174        DataRowAdded(row);
175      }
176    }
[726]177
[761]178    public event DataRowRemovedHandler DataRowRemoved;
[727]179
[761]180    protected void OnDataRowRemoved(IDataRow row) {
181      if (DataRowRemoved != null) {
182        DataRowRemoved(row);
[726]183      }
[761]184    }
[726]185
[761]186    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
[979]187      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
188
[1194]189      foreach (IDataRow row in rows) {
[979]190        XmlNode columnElement = document.CreateNode(XmlNodeType.Element, "row", null);
191
192        XmlAttribute idAttr = document.CreateAttribute("label");
[1194]193        idAttr.Value = row.Label;
[979]194        columnElement.Attributes.Append(idAttr);
195
[1061]196        StringBuilder builder = new StringBuilder();
197
[979]198        for (int i = 0; i < row.Count; i++) {
199          if (i == 0) {
[1061]200            builder.Append(row[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
201            //columnElement.InnerText += row[i].ToString(CultureInfo.InvariantCulture.NumberFormat);
[979]202          } else {
[1061]203            builder.Append(";" + row[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
204            //columnElement.InnerText += ";" + row[i].ToString(CultureInfo.InvariantCulture.NumberFormat);
[979]205          }
206        }
[1061]207        columnElement.InnerText += builder.ToString();
[979]208        node.AppendChild(columnElement);
209      }
[1385]210
211      XmlNode labelProviderNode = document.ImportNode(labelProvider.GetLabelProviderXmlNode(), true);
212      node.AppendChild(labelProviderNode);
213
[979]214      return node;   
[761]215    }
216
[726]217    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
[979]218      base.Populate(node, restoredObjects);
[731]219
[979]220      foreach (XmlNode dataRow in node.ChildNodes) {
[1385]221        if (dataRow.Name.Equals("LabelProvider")) {
222          labelProvider = labelProvider.PopulateLabelProviderXmlNode(dataRow);
223        } else {
224          XmlAttributeCollection attrs = dataRow.Attributes;
225          XmlAttribute rowIdAttr = (XmlAttribute)attrs.GetNamedItem("label");
226          string rowLabel = rowIdAttr.Value;
227          DataRow row = new DataRow();
228          row.Label = rowLabel;
[979]229
[1385]230          string[] tokens = dataRow.InnerText.Split(';');
231          double[] data = new double[tokens.Length];
232          for (int i = 0; i < data.Length; i++) {
233            if (tokens[i].Length != 0) {
234              if (
235                double.TryParse(tokens[i], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out data[i]) ==
236                false) {
237                throw new FormatException("Can't parse " + tokens[i] + " as double value.");
238              }
[979]239            }
240          }
[1385]241          row.AddValues(data);
242          AddDataRow(row);
[979]243        }
244      }
[726]245    }
[680]246  }
[761]247}
Note: See TracBrowser for help on using the repository browser.