1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Drawing;
|
---|
4 | using System.Globalization;
|
---|
5 | using System.Xml;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using System.Text;
|
---|
8 | using HeuristicLab.Visualization.Options;
|
---|
9 |
|
---|
10 | namespace 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 |
|
---|
158 |
|
---|
159 | XmlNode modelProperties = document.CreateNode(XmlNodeType.Element, "modelProperties", null);
|
---|
160 |
|
---|
161 | XmlAttribute attrTitle = document.CreateAttribute("title");
|
---|
162 | attrTitle.Value = Title;
|
---|
163 | modelProperties.Attributes.Append(attrTitle);
|
---|
164 |
|
---|
165 |
|
---|
166 |
|
---|
167 | node.AppendChild(modelProperties);
|
---|
168 |
|
---|
169 | return node;
|
---|
170 | }
|
---|
171 |
|
---|
172 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
173 | base.Populate(node, restoredObjects);
|
---|
174 |
|
---|
175 | List<YAxisDescriptor> yAxis = new List<YAxisDescriptor>();
|
---|
176 | foreach (XmlNode dataRow in node.ChildNodes)
|
---|
177 | {
|
---|
178 | if (dataRow.Name.Equals("yAxis"))
|
---|
179 | {
|
---|
180 | XmlAttributeCollection attrs = dataRow.Attributes;
|
---|
181 |
|
---|
182 | XmlAttribute attrYAxisLabel = (XmlAttribute)attrs.GetNamedItem("label");
|
---|
183 | string axisLabel = attrYAxisLabel.Value;
|
---|
184 | YAxisDescriptor axis = new YAxisDescriptor();
|
---|
185 | axis.Label = axisLabel;
|
---|
186 | yAxis.Add(axis);
|
---|
187 |
|
---|
188 | XmlAttribute attrDefault = (XmlAttribute)attrs.GetNamedItem("default");
|
---|
189 | if (attrDefault != null) {
|
---|
190 | defaultYAxisDescriptor = axis;
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | foreach (XmlNode dataRow in node.ChildNodes) {
|
---|
196 | if (dataRow.Name.Equals("modelProperties")) {
|
---|
197 | XmlAttributeCollection attrs = dataRow.Attributes;
|
---|
198 | XmlAttribute attrYAxisLabel = (XmlAttribute)attrs.GetNamedItem("title");
|
---|
199 | Title = attrYAxisLabel.Value;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | foreach (XmlNode dataRow in node.ChildNodes) {
|
---|
204 | if (dataRow.Name.Equals("LabelProvider")) {
|
---|
205 | XAxis.LabelProvider = XAxis.LabelProvider.PopulateLabelProviderXmlNode(dataRow);
|
---|
206 | } else if (dataRow.Name.Equals("row")) {
|
---|
207 | XmlAttributeCollection attrs = dataRow.Attributes;
|
---|
208 | XmlAttribute rowIdAttr = (XmlAttribute)attrs.GetNamedItem("label");
|
---|
209 | string rowLabel = rowIdAttr.Value;
|
---|
210 |
|
---|
211 | string rowColor = attrs.GetNamedItem("color").Value;
|
---|
212 | DataRow row = new DataRow();
|
---|
213 | row.RowSettings.Label = rowLabel;
|
---|
214 | row.RowSettings.Color = Color.FromArgb(Int32.Parse(rowColor));
|
---|
215 |
|
---|
216 | string rowThickness = attrs.GetNamedItem("thickness").Value;
|
---|
217 | int thick;
|
---|
218 | Int32.TryParse(rowThickness, out thick);
|
---|
219 | row.RowSettings.Thickness = thick;
|
---|
220 |
|
---|
221 | string yAxisLabel = attrs.GetNamedItem("yAxis").Value;
|
---|
222 | foreach (YAxisDescriptor axis in yAxis) {
|
---|
223 | if (axis.Label.Equals(yAxisLabel)) {
|
---|
224 | row.YAxis = axis;
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | string[] tokens = dataRow.InnerText.Split(';');
|
---|
229 | double[] data = new double[tokens.Length];
|
---|
230 | for (int i = 0; i < data.Length; i++) {
|
---|
231 | if (tokens[i].Length != 0) {
|
---|
232 | if (
|
---|
233 | double.TryParse(tokens[i], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out data[i]) ==
|
---|
234 | false) {
|
---|
235 | throw new FormatException("Can't parse " + tokens[i] + " as double value.");
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|
239 | row.AddValues(data);
|
---|
240 | AddDataRow(row);
|
---|
241 | }
|
---|
242 | }
|
---|
243 | }
|
---|
244 | }
|
---|
245 | }
|
---|