Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1500 was 1462, checked in by mstoeger, 15 years ago

Display of X- and Y-Axis-Labels (#556)

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