1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Globalization;
|
---|
4 | using System.Xml;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using System.Text;
|
---|
7 | using HeuristicLab.Visualization.LabelProvider;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Visualization{
|
---|
10 | public delegate void DataRowAddedHandler(IDataRow row);
|
---|
11 | public delegate void DataRowRemovedHandler(IDataRow row);
|
---|
12 | public delegate void ModelChangedHandler();
|
---|
13 |
|
---|
14 | public class ChartDataRowsModel : ChartDataModelBase, IChartDataRowsModel{
|
---|
15 | private string title = "Title";
|
---|
16 | //private string xAxisLabel;
|
---|
17 | private ILabelProvider labelProvider = new ContinuousLabelProvider("0.##");
|
---|
18 |
|
---|
19 | public ILabelProvider XAxisLabelProvider {
|
---|
20 | get { return labelProvider; }
|
---|
21 | set{
|
---|
22 | this.labelProvider = value;
|
---|
23 | OnModelChanged();
|
---|
24 | }
|
---|
25 | }
|
---|
26 |
|
---|
27 |
|
---|
28 | private readonly List<IDataRow> rows = new List<IDataRow>();
|
---|
29 | //private readonly List<string> xLabels = new List<string>();
|
---|
30 |
|
---|
31 | //public List<string> XLabels{
|
---|
32 | // get { return xLabels; }
|
---|
33 | //}
|
---|
34 |
|
---|
35 | public List<IDataRow> Rows{
|
---|
36 | get { return rows; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | public string Title {
|
---|
40 | get { return title; }
|
---|
41 | set {
|
---|
42 | title = value;
|
---|
43 | OnModelChanged();
|
---|
44 | }
|
---|
45 | }
|
---|
46 | //public string XAxisLabel {
|
---|
47 | // get { return xAxisLabel; }
|
---|
48 | // set {
|
---|
49 | // xAxisLabel = value;
|
---|
50 | // OnModelChanged();
|
---|
51 | // }
|
---|
52 | //}
|
---|
53 |
|
---|
54 | public override IView CreateView() {
|
---|
55 | return new LineChart(this);
|
---|
56 | }
|
---|
57 |
|
---|
58 | //public void AddLabel(string label) {
|
---|
59 | // xLabels.Add(label);
|
---|
60 | // OnModelChanged();
|
---|
61 | //}
|
---|
62 |
|
---|
63 | //public void AddLabel(string label, int index) {
|
---|
64 | // xLabels[index] = label;
|
---|
65 | // OnModelChanged();
|
---|
66 | //}
|
---|
67 |
|
---|
68 | //public void AddLabels(string[] labels) {
|
---|
69 | // foreach (var s in labels){
|
---|
70 | // AddLabel(s);
|
---|
71 | // }
|
---|
72 | // //OnModelChanged();
|
---|
73 | //}
|
---|
74 |
|
---|
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 | //}
|
---|
83 |
|
---|
84 | //public void ModifyLabel(string label, int index) {
|
---|
85 | // xLabels[index] = label;
|
---|
86 | // OnModelChanged();
|
---|
87 | //}
|
---|
88 |
|
---|
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 | //}
|
---|
97 |
|
---|
98 | //public void RemoveLabel(int index) {
|
---|
99 | // xLabels.RemoveAt(index);
|
---|
100 | // OnModelChanged();
|
---|
101 | //}
|
---|
102 |
|
---|
103 | //public void RemoveLabels(int index, int count) {
|
---|
104 | // for (int i = index; i < index + count; i++ ){
|
---|
105 | // RemoveLabel(i);
|
---|
106 | // }
|
---|
107 | // //OnModelChanged();
|
---|
108 | //}
|
---|
109 |
|
---|
110 | public void AddDataRow(IDataRow row) {
|
---|
111 | rows.Add(row);
|
---|
112 | OnDataRowAdded(row);
|
---|
113 | }
|
---|
114 |
|
---|
115 | public void RemoveDataRow(IDataRow row) {
|
---|
116 | rows.Remove(row);
|
---|
117 | OnDataRowRemoved(row);
|
---|
118 | }
|
---|
119 |
|
---|
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 |
|
---|
133 | public event ModelChangedHandler ModelChanged;
|
---|
134 |
|
---|
135 | protected void OnModelChanged() {
|
---|
136 | if (ModelChanged != null) {
|
---|
137 | ModelChanged();
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | public event DataRowAddedHandler DataRowAdded;
|
---|
142 |
|
---|
143 | protected void OnDataRowAdded(IDataRow row) {
|
---|
144 | if (DataRowAdded != null) {
|
---|
145 | DataRowAdded(row);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | public event DataRowRemovedHandler DataRowRemoved;
|
---|
150 |
|
---|
151 | protected void OnDataRowRemoved(IDataRow row) {
|
---|
152 | if (DataRowRemoved != null) {
|
---|
153 | DataRowRemoved(row);
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
158 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
159 |
|
---|
160 | foreach (IDataRow row in rows) {
|
---|
161 | XmlNode columnElement = document.CreateNode(XmlNodeType.Element, "row", null);
|
---|
162 |
|
---|
163 | XmlAttribute idAttr = document.CreateAttribute("label");
|
---|
164 | idAttr.Value = row.Label;
|
---|
165 | columnElement.Attributes.Append(idAttr);
|
---|
166 |
|
---|
167 | StringBuilder builder = new StringBuilder();
|
---|
168 |
|
---|
169 | for (int i = 0; i < row.Count; i++) {
|
---|
170 | if (i == 0) {
|
---|
171 | builder.Append(row[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
172 | //columnElement.InnerText += row[i].ToString(CultureInfo.InvariantCulture.NumberFormat);
|
---|
173 | } else {
|
---|
174 | builder.Append(";" + row[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
175 | //columnElement.InnerText += ";" + row[i].ToString(CultureInfo.InvariantCulture.NumberFormat);
|
---|
176 | }
|
---|
177 | }
|
---|
178 | columnElement.InnerText += builder.ToString();
|
---|
179 | node.AppendChild(columnElement);
|
---|
180 | }
|
---|
181 | return node;
|
---|
182 | }
|
---|
183 |
|
---|
184 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
185 | base.Populate(node, restoredObjects);
|
---|
186 |
|
---|
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 | }
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|