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 | public event ModelChangedHandler ModelChanged;
|
---|
121 |
|
---|
122 | protected void OnModelChanged() {
|
---|
123 | if (ModelChanged != null) {
|
---|
124 | ModelChanged();
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | public event DataRowAddedHandler DataRowAdded;
|
---|
129 |
|
---|
130 | protected void OnDataRowAdded(IDataRow row) {
|
---|
131 | if (DataRowAdded != null) {
|
---|
132 | DataRowAdded(row);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | public event DataRowRemovedHandler DataRowRemoved;
|
---|
137 |
|
---|
138 | protected void OnDataRowRemoved(IDataRow row) {
|
---|
139 | if (DataRowRemoved != null) {
|
---|
140 | DataRowRemoved(row);
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
145 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
146 |
|
---|
147 | foreach (IDataRow row in rows) {
|
---|
148 | XmlNode columnElement = document.CreateNode(XmlNodeType.Element, "row", null);
|
---|
149 |
|
---|
150 | XmlAttribute idAttr = document.CreateAttribute("label");
|
---|
151 | idAttr.Value = row.Label;
|
---|
152 | columnElement.Attributes.Append(idAttr);
|
---|
153 |
|
---|
154 | StringBuilder builder = new StringBuilder();
|
---|
155 |
|
---|
156 | for (int i = 0; i < row.Count; i++) {
|
---|
157 | if (i == 0) {
|
---|
158 | builder.Append(row[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
159 | //columnElement.InnerText += row[i].ToString(CultureInfo.InvariantCulture.NumberFormat);
|
---|
160 | } else {
|
---|
161 | builder.Append(";" + row[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
162 | //columnElement.InnerText += ";" + row[i].ToString(CultureInfo.InvariantCulture.NumberFormat);
|
---|
163 | }
|
---|
164 | }
|
---|
165 | columnElement.InnerText += builder.ToString();
|
---|
166 | node.AppendChild(columnElement);
|
---|
167 | }
|
---|
168 | return node;
|
---|
169 | }
|
---|
170 |
|
---|
171 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
172 | base.Populate(node, restoredObjects);
|
---|
173 |
|
---|
174 | foreach (XmlNode dataRow in node.ChildNodes) {
|
---|
175 | XmlAttributeCollection attrs = dataRow.Attributes;
|
---|
176 | XmlAttribute rowIdAttr = (XmlAttribute)attrs.GetNamedItem("label");
|
---|
177 | string rowLabel = rowIdAttr.Value;
|
---|
178 | DataRow row = new DataRow();
|
---|
179 | row.Label = rowLabel;
|
---|
180 |
|
---|
181 | string[] tokens = dataRow.InnerText.Split(';');
|
---|
182 | double[] data = new double[tokens.Length];
|
---|
183 | for (int i = 0; i < data.Length; i++) {
|
---|
184 | if (tokens[i].Length != 0) {
|
---|
185 | if (
|
---|
186 | double.TryParse(tokens[i], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out data[i]) ==
|
---|
187 | false) {
|
---|
188 | throw new FormatException("Can't parse " + tokens[i] + " as double value.");
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|
192 | row.AddValues(data);
|
---|
193 |
|
---|
194 | AddDataRow(row);
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 | }
|
---|