Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/3.2/ChartDataRowsModel.cs @ 1988

Last change on this file since 1988 was 1988, checked in by cbahner, 15 years ago

#639 added color and y axis assignment to persistance

File size: 6.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Globalization;
5using System.Xml;
6using HeuristicLab.Core;
7using System.Text;
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 : ItemBase, IChartDataRowsModel {
16    private string title = "Title";
17    private ViewSettings viewSettings = new ViewSettings();
18    private readonly XAxisDescriptor xAxisDescriptor = new XAxisDescriptor();
19    private YAxisDescriptor defaultYAxisDescriptor = new YAxisDescriptor();
20    private readonly List<IDataRow> rows = new List<IDataRow>();
21
22    public ChartDataRowsModel() {
23      this.XAxis.XAxisDescriptorChanged += delegate { OnModelChanged(); };
24    }
25
26    public override IView CreateView() {
27      return new LineChart(this);
28    }
29
30    public string Title {
31      get { return title; }
32      set {
33        title = value;
34        OnModelChanged();
35      }
36    }
37
38    public XAxisDescriptor XAxis {
39      get { return xAxisDescriptor; }
40    }
41
42    public List<YAxisDescriptor> YAxes {
43      get {
44        Dictionary<YAxisDescriptor, object> yaxes = new Dictionary<YAxisDescriptor, object>();
45
46        foreach (IDataRow row in rows) {
47          yaxes[row.YAxis] = null;
48        }
49
50        return new List<YAxisDescriptor>(yaxes.Keys);
51      }
52    }
53
54    public YAxisDescriptor DefaultYAxis {
55      get { return defaultYAxisDescriptor; }
56    }
57
58    public List<IDataRow> Rows{
59      get { return rows; }
60    }
61
62    public void AddDataRow(IDataRow row) {
63      if (row.YAxis == null) {
64        row.YAxis = defaultYAxisDescriptor;
65      }
66      rows.Add(row);
67      OnDataRowAdded(row);
68    }
69
70    public void AddDataRows(params IDataRow[] rows) {
71      foreach (IDataRow row in rows) {
72        AddDataRow(row);
73      }
74    }
75
76    public void RemoveDataRow(IDataRow row) {
77      rows.Remove(row);
78      OnDataRowRemoved(row);
79    }
80
81    // TODO implement calculation of max data row values
82    public int MaxDataRowValues {
83      get {
84        int max = 0;
85
86        foreach (IDataRow row in rows) {
87          max = Math.Max(max, row.Count);
88        }
89
90        return max;
91      }
92    }
93
94    public ViewSettings ViewSettings {
95      get { return viewSettings; }
96      set { viewSettings = value; }
97    }
98
99    public event ModelChangedHandler ModelChanged;
100
101    protected void OnModelChanged() {
102      if (ModelChanged != null) {
103        ModelChanged();
104      }
105    }
106
107    public event DataRowAddedHandler DataRowAdded;
108
109    protected void OnDataRowAdded(IDataRow row) {
110      if (DataRowAdded != null) {
111        DataRowAdded(row);
112      }
113    }
114
115    public event DataRowRemovedHandler DataRowRemoved;
116
117    protected void OnDataRowRemoved(IDataRow row) {
118      if (DataRowRemoved != null) {
119        DataRowRemoved(row);
120      }
121    }
122
123    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
124      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
125     
126      List<YAxisDescriptor> yAxis = new List<YAxisDescriptor>();
127      yAxis.Add(defaultYAxisDescriptor);
128      foreach (IDataRow row in rows) {
129        XmlNode rowElement = row.ToXml(row, document);
130       
131        if (!yAxis.Contains(row.YAxis)) {
132          yAxis.Add(row.YAxis);
133        }
134       
135        node.AppendChild(rowElement);
136      }
137
138      foreach (YAxisDescriptor axis in yAxis) {
139        XmlNode yAxisElement = document.CreateNode(XmlNodeType.Element, "yAxis", null);
140
141        XmlAttribute attrLabel = document.CreateAttribute("label");
142        attrLabel.Value = axis.Label;
143        yAxisElement.Attributes.Append(attrLabel);
144
145        if (axis == defaultYAxisDescriptor) {
146          XmlAttribute attrDefault = document.CreateAttribute("default");
147          attrDefault.Value = "true";
148          yAxisElement.Attributes.Append(attrDefault);
149        }
150        node.AppendChild(yAxisElement);
151       
152      }
153
154      XmlNode labelProviderNode = document.ImportNode(XAxis.LabelProvider.GetLabelProviderXmlNode(), true);
155      node.AppendChild(labelProviderNode);
156
157      return node;
158    }
159
160    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
161      base.Populate(node, restoredObjects);
162
163      List<YAxisDescriptor> yAxis = new List<YAxisDescriptor>();
164      foreach (XmlNode dataRow in node.ChildNodes)
165      {
166        if (dataRow.Name.Equals("yAxis"))
167        {
168          XmlAttributeCollection attrs = dataRow.Attributes;
169
170          XmlAttribute attrYAxisLabel = (XmlAttribute)attrs.GetNamedItem("label");
171          string axisLabel = attrYAxisLabel.Value;
172          YAxisDescriptor axis = new YAxisDescriptor();
173          axis.Label = axisLabel;
174          yAxis.Add(axis);
175
176          XmlAttribute attrDefault = (XmlAttribute)attrs.GetNamedItem("default");
177          if (attrDefault != null) {
178            defaultYAxisDescriptor = axis;
179          }
180        }
181      }
182
183      foreach (XmlNode dataRow in node.ChildNodes) {
184        if (dataRow.Name.Equals("LabelProvider")) {
185          XAxis.LabelProvider = XAxis.LabelProvider.PopulateLabelProviderXmlNode(dataRow);
186        } else if (dataRow.Name.Equals("row")) {
187          XmlAttributeCollection attrs = dataRow.Attributes;
188          XmlAttribute rowIdAttr = (XmlAttribute)attrs.GetNamedItem("label");
189          string rowLabel = rowIdAttr.Value;
190          string rowColor = attrs.GetNamedItem("color").Value;
191
192          DataRow row = new DataRow();
193          row.RowSettings.Label = rowLabel;
194          row.RowSettings.Color = Color.FromName(rowColor);
195
196          string yAxisLabel = attrs.GetNamedItem("yAxis").Value;
197          foreach (YAxisDescriptor axis in yAxis) {
198            if (axis.Label.Equals(yAxisLabel)) {
199              row.YAxis = axis;
200            }
201          }
202
203          string[] tokens = dataRow.InnerText.Split(';');
204          double[] data = new double[tokens.Length];
205          for (int i = 0; i < data.Length; i++) {
206            if (tokens[i].Length != 0) {
207              if (
208                double.TryParse(tokens[i], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out data[i]) ==
209                false) {
210                throw new FormatException("Can't parse " + tokens[i] + " as double value.");
211              }
212            }
213          }
214          row.AddValues(data);
215          AddDataRow(row);
216        }
217      }
218    }
219  }
220}
Note: See TracBrowser for help on using the repository browser.