Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2296 was 1996, checked in by mstoeger, 15 years ago

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

File size: 4.5 KB
RevLine 
[680]1using System;
2using System.Collections.Generic;
[726]3using System.Xml;
4using HeuristicLab.Core;
[1341]5using HeuristicLab.Visualization.Options;
[680]6
[685]7namespace HeuristicLab.Visualization{
[761]8  public delegate void DataRowAddedHandler(IDataRow row);
9  public delegate void DataRowRemovedHandler(IDataRow row);
10  public delegate void ModelChangedHandler();
[685]11
[1886]12  public class ChartDataRowsModel : ItemBase, IChartDataRowsModel {
[1182]13    private string title = "Title";
[1341]14    private ViewSettings viewSettings = new ViewSettings();
[1996]15    private XAxisDescriptor xAxisDescriptor;
[1988]16    private YAxisDescriptor defaultYAxisDescriptor = new YAxisDescriptor();
[1886]17    private readonly List<IDataRow> rows = new List<IDataRow>();
[1880]18
19    public ChartDataRowsModel() {
[1996]20      this.XAxis = new XAxisDescriptor();
[1876]21    }
22
[1886]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
[1880]35    public XAxisDescriptor XAxis {
36      get { return xAxisDescriptor; }
[1996]37      private set {
38        this.xAxisDescriptor = value;
39        this.xAxisDescriptor.XAxisDescriptorChanged += delegate { OnModelChanged(); };
40      }
[1190]41    }
42
[1350]43    public List<YAxisDescriptor> YAxes {
44      get {
45        Dictionary<YAxisDescriptor, object> yaxes = new Dictionary<YAxisDescriptor, object>();
[1190]46
[1350]47        foreach (IDataRow row in rows) {
48          yaxes[row.YAxis] = null;
49        }
50
51        return new List<YAxisDescriptor>(yaxes.Keys);
52      }
53    }
54
[1886]55    public YAxisDescriptor DefaultYAxis {
56      get { return defaultYAxisDescriptor; }
57    }
[1350]58
[859]59    public List<IDataRow> Rows{
60      get { return rows; }
[761]61    }
[685]62
[761]63    public void AddDataRow(IDataRow row) {
[1350]64      if (row.YAxis == null) {
65        row.YAxis = defaultYAxisDescriptor;
66      }
[761]67      rows.Add(row);
68      OnDataRowAdded(row);
[697]69    }
[726]70
[1986]71    public void AddDataRows(params IDataRow[] rows) {
[1985]72      foreach (IDataRow row in rows) {
73        AddDataRow(row);
74      }
75    }
76
[761]77    public void RemoveDataRow(IDataRow row) {
78      rows.Remove(row);
79      OnDataRowRemoved(row);
80    }
[726]81
[1285]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
[1341]95    public ViewSettings ViewSettings {
96      get { return viewSettings; }
97      set { viewSettings = value; }
98    }
99
[859]100    public event ModelChangedHandler ModelChanged;
101
102    protected void OnModelChanged() {
103      if (ModelChanged != null) {
104        ModelChanged();
105      }
106    }
107
[761]108    public event DataRowAddedHandler DataRowAdded;
[726]109
[761]110    protected void OnDataRowAdded(IDataRow row) {
111      if (DataRowAdded != null) {
112        DataRowAdded(row);
113      }
114    }
[726]115
[761]116    public event DataRowRemovedHandler DataRowRemoved;
[727]117
[761]118    protected void OnDataRowRemoved(IDataRow row) {
119      if (DataRowRemoved != null) {
120        DataRowRemoved(row);
[726]121      }
[761]122    }
[726]123
[761]124    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
[979]125      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
[1993]126
127      XmlSupport.SetAttribute("Title", title, node);
[979]128
[1993]129      foreach (IDataRow row in rows) {
[1996]130        node.AppendChild(PersistenceManager.Persist("DataRow", row, document, persistedObjects));
[1993]131      }
[1996]132      node.AppendChild(PersistenceManager.Persist("DefaultYAxis", this.DefaultYAxis, document, persistedObjects));
133      node.AppendChild(PersistenceManager.Persist("XAxis", this.XAxis, document, persistedObjects));
[1989]134
[1988]135      return node;
[761]136    }
137
[726]138    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
[979]139      base.Populate(node, restoredObjects);
[731]140
[1993]141      title = XmlSupport.GetAttribute("Title", "", node);
[1988]142
[1996]143      foreach (XmlNode dataRowNode in node.SelectNodes("DataRow")) {
144        IDataRow dataRow = (IDataRow)PersistenceManager.Restore(dataRowNode, restoredObjects);
145        AddDataRow(dataRow);
[1988]146      }
147
[1996]148      XmlNode defaultYAxisNode = node.SelectSingleNode("DefaultYAxis");
149      if (defaultYAxisNode != null)
150        this.defaultYAxisDescriptor = (YAxisDescriptor)PersistenceManager.Restore(defaultYAxisNode, restoredObjects);
[1989]151
[1996]152      XmlNode xAxisNode = node.SelectSingleNode("XAxis");
153      if (xAxisNode != null)
154        this.XAxis = (XAxisDescriptor)PersistenceManager.Restore(xAxisNode, restoredObjects);
[726]155    }
[680]156  }
[761]157}
Note: See TracBrowser for help on using the repository browser.