Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1307 was 1285, checked in by mstoeger, 16 years ago

Implemented multiple Y-Axes. (#433) Panning & Zooming is broken.

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