Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Visualization/3.2/ChartDataRowsModel.cs @ 4121

Last change on this file since 4121 was 1996, checked in by mstoeger, 16 years ago

implemented IStorable and made use of the PersistenceManager wherever possible. #639

File size: 4.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Xml;
4using HeuristicLab.Core;
5using HeuristicLab.Visualization.Options;
6
7namespace HeuristicLab.Visualization{
8  public delegate void DataRowAddedHandler(IDataRow row);
9  public delegate void DataRowRemovedHandler(IDataRow row);
10  public delegate void ModelChangedHandler();
11
12  public class ChartDataRowsModel : ItemBase, IChartDataRowsModel {
13    private string title = "Title";
14    private ViewSettings viewSettings = new ViewSettings();
15    private XAxisDescriptor xAxisDescriptor;
16    private YAxisDescriptor defaultYAxisDescriptor = new YAxisDescriptor();
17    private readonly List<IDataRow> rows = new List<IDataRow>();
18
19    public ChartDataRowsModel() {
20      this.XAxis = new XAxisDescriptor();
21    }
22
23    public override IView CreateView() {
24      return new LineChart(this);
25    }
26
27    public string Title {
28      get { return title; }
29      set {
30        title = value;
31        OnModelChanged();
32      }
33    }
34
35    public XAxisDescriptor XAxis {
36      get { return xAxisDescriptor; }
37      private set {
38        this.xAxisDescriptor = value;
39        this.xAxisDescriptor.XAxisDescriptorChanged += delegate { OnModelChanged(); };
40      }
41    }
42
43    public List<YAxisDescriptor> YAxes {
44      get {
45        Dictionary<YAxisDescriptor, object> yaxes = new Dictionary<YAxisDescriptor, object>();
46
47        foreach (IDataRow row in rows) {
48          yaxes[row.YAxis] = null;
49        }
50
51        return new List<YAxisDescriptor>(yaxes.Keys);
52      }
53    }
54
55    public YAxisDescriptor DefaultYAxis {
56      get { return defaultYAxisDescriptor; }
57    }
58
59    public List<IDataRow> Rows{
60      get { return rows; }
61    }
62
63    public void AddDataRow(IDataRow row) {
64      if (row.YAxis == null) {
65        row.YAxis = defaultYAxisDescriptor;
66      }
67      rows.Add(row);
68      OnDataRowAdded(row);
69    }
70
71    public void AddDataRows(params IDataRow[] rows) {
72      foreach (IDataRow row in rows) {
73        AddDataRow(row);
74      }
75    }
76
77    public void RemoveDataRow(IDataRow row) {
78      rows.Remove(row);
79      OnDataRowRemoved(row);
80    }
81
82    // TODO implement calculation of max data row values
83    public int MaxDataRowValues {
84      get {
85        int max = 0;
86
87        foreach (IDataRow row in rows) {
88          max = Math.Max(max, row.Count);
89        }
90
91        return max;
92      }
93    }
94
95    public ViewSettings ViewSettings {
96      get { return viewSettings; }
97      set { viewSettings = value; }
98    }
99
100    public event ModelChangedHandler ModelChanged;
101
102    protected void OnModelChanged() {
103      if (ModelChanged != null) {
104        ModelChanged();
105      }
106    }
107
108    public event DataRowAddedHandler DataRowAdded;
109
110    protected void OnDataRowAdded(IDataRow row) {
111      if (DataRowAdded != null) {
112        DataRowAdded(row);
113      }
114    }
115
116    public event DataRowRemovedHandler DataRowRemoved;
117
118    protected void OnDataRowRemoved(IDataRow row) {
119      if (DataRowRemoved != null) {
120        DataRowRemoved(row);
121      }
122    }
123
124    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
125      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
126
127      XmlSupport.SetAttribute("Title", title, node);
128
129      foreach (IDataRow row in rows) {
130        node.AppendChild(PersistenceManager.Persist("DataRow", row, document, persistedObjects));
131      }
132      node.AppendChild(PersistenceManager.Persist("DefaultYAxis", this.DefaultYAxis, document, persistedObjects));
133      node.AppendChild(PersistenceManager.Persist("XAxis", this.XAxis, document, persistedObjects));
134
135      return node;
136    }
137
138    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
139      base.Populate(node, restoredObjects);
140
141      title = XmlSupport.GetAttribute("Title", "", node);
142
143      foreach (XmlNode dataRowNode in node.SelectNodes("DataRow")) {
144        IDataRow dataRow = (IDataRow)PersistenceManager.Restore(dataRowNode, restoredObjects);
145        AddDataRow(dataRow);
146      }
147
148      XmlNode defaultYAxisNode = node.SelectSingleNode("DefaultYAxis");
149      if (defaultYAxisNode != null)
150        this.defaultYAxisDescriptor = (YAxisDescriptor)PersistenceManager.Restore(defaultYAxisNode, restoredObjects);
151
152      XmlNode xAxisNode = node.SelectSingleNode("XAxis");
153      if (xAxisNode != null)
154        this.XAxis = (XAxisDescriptor)PersistenceManager.Restore(xAxisNode, restoredObjects);
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.