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.Options;
|
---|
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 |
|
---|
17 | private ViewSettings viewSettings = new ViewSettings();
|
---|
18 |
|
---|
19 | private readonly XAxisDescriptor xAxisDescriptor = new XAxisDescriptor();
|
---|
20 |
|
---|
21 | public ChartDataRowsModel() {
|
---|
22 | this.XAxis.XAxisDescriptorChanged += delegate { OnModelChanged(); };
|
---|
23 | }
|
---|
24 |
|
---|
25 | public XAxisDescriptor XAxis {
|
---|
26 | get { return xAxisDescriptor; }
|
---|
27 | }
|
---|
28 |
|
---|
29 | public List<YAxisDescriptor> YAxes {
|
---|
30 | get {
|
---|
31 | Dictionary<YAxisDescriptor, object> yaxes = new Dictionary<YAxisDescriptor, object>();
|
---|
32 |
|
---|
33 | foreach (IDataRow row in rows) {
|
---|
34 | yaxes[row.YAxis] = null;
|
---|
35 | }
|
---|
36 |
|
---|
37 | return new List<YAxisDescriptor>(yaxes.Keys);
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 |
|
---|
42 | private readonly List<IDataRow> rows = new List<IDataRow>();
|
---|
43 |
|
---|
44 | public List<IDataRow> Rows{
|
---|
45 | get { return rows; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public string Title {
|
---|
49 | get { return title; }
|
---|
50 | set {
|
---|
51 | title = value;
|
---|
52 | OnModelChanged();
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public override IView CreateView() {
|
---|
57 | return new LineChart(this);
|
---|
58 | }
|
---|
59 |
|
---|
60 | private readonly YAxisDescriptor defaultYAxisDescriptor = new YAxisDescriptor();
|
---|
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 RemoveDataRow(IDataRow row) {
|
---|
71 | rows.Remove(row);
|
---|
72 | OnDataRowRemoved(row);
|
---|
73 | }
|
---|
74 |
|
---|
75 | public YAxisDescriptor DefaultYAxis {
|
---|
76 | get { return defaultYAxisDescriptor; }
|
---|
77 | }
|
---|
78 |
|
---|
79 | // TODO implement calculation of max data row values
|
---|
80 | public int MaxDataRowValues {
|
---|
81 | get {
|
---|
82 | int max = 0;
|
---|
83 |
|
---|
84 | foreach (IDataRow row in rows) {
|
---|
85 | max = Math.Max(max, row.Count);
|
---|
86 | }
|
---|
87 |
|
---|
88 | return max;
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | public ViewSettings ViewSettings {
|
---|
93 | get { return viewSettings; }
|
---|
94 | set { viewSettings = value; }
|
---|
95 | }
|
---|
96 |
|
---|
97 | public event ModelChangedHandler ModelChanged;
|
---|
98 |
|
---|
99 | protected void OnModelChanged() {
|
---|
100 | if (ModelChanged != null) {
|
---|
101 | ModelChanged();
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | public event DataRowAddedHandler DataRowAdded;
|
---|
106 |
|
---|
107 | protected void OnDataRowAdded(IDataRow row) {
|
---|
108 | if (DataRowAdded != null) {
|
---|
109 | DataRowAdded(row);
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | public event DataRowRemovedHandler DataRowRemoved;
|
---|
114 |
|
---|
115 | protected void OnDataRowRemoved(IDataRow row) {
|
---|
116 | if (DataRowRemoved != null) {
|
---|
117 | DataRowRemoved(row);
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
122 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
123 |
|
---|
124 | foreach (IDataRow row in rows) {
|
---|
125 | XmlNode columnElement = document.CreateNode(XmlNodeType.Element, "row", null);
|
---|
126 |
|
---|
127 | XmlAttribute idAttr = document.CreateAttribute("label");
|
---|
128 | idAttr.Value = row.Label;
|
---|
129 | columnElement.Attributes.Append(idAttr);
|
---|
130 |
|
---|
131 | StringBuilder builder = new StringBuilder();
|
---|
132 |
|
---|
133 | for (int i = 0; i < row.Count; i++) {
|
---|
134 | if (i == 0) {
|
---|
135 | builder.Append(row[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
136 | //columnElement.InnerText += row[i].ToString(CultureInfo.InvariantCulture.NumberFormat);
|
---|
137 | } else {
|
---|
138 | builder.Append(";" + row[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
139 | //columnElement.InnerText += ";" + row[i].ToString(CultureInfo.InvariantCulture.NumberFormat);
|
---|
140 | }
|
---|
141 | }
|
---|
142 | columnElement.InnerText += builder.ToString();
|
---|
143 | node.AppendChild(columnElement);
|
---|
144 | }
|
---|
145 |
|
---|
146 | XmlNode labelProviderNode = document.ImportNode(XAxis.XAxisLabelProvider.GetLabelProviderXmlNode(), true);
|
---|
147 | node.AppendChild(labelProviderNode);
|
---|
148 |
|
---|
149 | return node;
|
---|
150 | }
|
---|
151 |
|
---|
152 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
153 | base.Populate(node, restoredObjects);
|
---|
154 |
|
---|
155 | foreach (XmlNode dataRow in node.ChildNodes) {
|
---|
156 | if (dataRow.Name.Equals("LabelProvider")) {
|
---|
157 | XAxis.XAxisLabelProvider = XAxis.XAxisLabelProvider.PopulateLabelProviderXmlNode(dataRow);
|
---|
158 | } else {
|
---|
159 | XmlAttributeCollection attrs = dataRow.Attributes;
|
---|
160 | XmlAttribute rowIdAttr = (XmlAttribute)attrs.GetNamedItem("label");
|
---|
161 | string rowLabel = rowIdAttr.Value;
|
---|
162 | DataRow row = new DataRow();
|
---|
163 | row.Label = rowLabel;
|
---|
164 |
|
---|
165 | string[] tokens = dataRow.InnerText.Split(';');
|
---|
166 | double[] data = new double[tokens.Length];
|
---|
167 | for (int i = 0; i < data.Length; i++) {
|
---|
168 | if (tokens[i].Length != 0) {
|
---|
169 | if (
|
---|
170 | double.TryParse(tokens[i], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out data[i]) ==
|
---|
171 | false) {
|
---|
172 | throw new FormatException("Can't parse " + tokens[i] + " as double value.");
|
---|
173 | }
|
---|
174 | }
|
---|
175 | }
|
---|
176 | row.AddValues(data);
|
---|
177 | AddDataRow(row);
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|