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 HeuristicLab.Visualization.Options;
|
---|
8 | using HeuristicLab.Visualization.Test;
|
---|
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 | XmlSupport.SetAttribute("Title", title, node);
|
---|
127 |
|
---|
128 | List<YAxisDescriptor> yAxes = new List<YAxisDescriptor>();
|
---|
129 | yAxes.Add(defaultYAxisDescriptor);
|
---|
130 | foreach (IDataRow row in rows) {
|
---|
131 | if (!yAxes.Contains(row.YAxis)) {
|
---|
132 | yAxes.Add(row.YAxis);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | foreach (YAxisDescriptor yAxis in yAxes) {
|
---|
137 | XmlNode yAxisElement = document.CreateElement("YAxis");
|
---|
138 |
|
---|
139 | XmlSupport.SetAttribute("Label", yAxis.Label, yAxisElement);
|
---|
140 | XmlSupport.SetAttribute("GridColor", yAxis.GridColor.ToArgb().ToString(), yAxisElement);
|
---|
141 | XmlSupport.SetAttribute("Position", yAxis.Position.ToString(), yAxisElement);
|
---|
142 | XmlSupport.SetAttribute("ShowGrid", yAxis.ShowGrid ? "true" : "false", yAxisElement);
|
---|
143 | XmlSupport.SetAttribute("ShowYAxis", yAxis.ShowYAxis ? "true" : "false", yAxisElement);
|
---|
144 | XmlSupport.SetAttribute("ShowYAxisLabel", yAxis.ShowYAxisLabel ? "true" : "false", yAxisElement);
|
---|
145 | XmlSupport.SetAttribute("ClipChangeable", yAxis.ClipChangeable ? "true" : "false", yAxisElement);
|
---|
146 |
|
---|
147 | if (yAxis == defaultYAxisDescriptor)
|
---|
148 | XmlSupport.SetAttribute("Default", "true", yAxisElement);
|
---|
149 |
|
---|
150 | node.AppendChild(yAxisElement);
|
---|
151 | }
|
---|
152 |
|
---|
153 | XmlNode xAxisElement = document.CreateElement("XAxis");
|
---|
154 | XmlSupport.SetAttribute("Color", xAxisDescriptor.Color.ToArgb().ToString(), xAxisElement);
|
---|
155 | XmlSupport.SetAttribute("GridColor", xAxisDescriptor.GridColor.ToArgb().ToString(), xAxisElement);
|
---|
156 | XmlSupport.SetAttribute("Label", xAxisDescriptor.Label, xAxisElement);
|
---|
157 | XmlSupport.SetAttribute("ShowGrid", xAxisDescriptor.ShowGrid ? "true" : "false", xAxisElement);
|
---|
158 | XmlSupport.SetAttribute("ShowLabel", xAxisDescriptor.ShowLabel ? "true" : "false", xAxisElement);
|
---|
159 | node.AppendChild(xAxisElement);
|
---|
160 |
|
---|
161 | foreach (IDataRow row in rows) {
|
---|
162 | node.AppendChild(row.ToXml(document));
|
---|
163 | }
|
---|
164 |
|
---|
165 | node.AppendChild(XAxis.LabelProvider.GetLabelProviderXmlNode(document));
|
---|
166 |
|
---|
167 | return node;
|
---|
168 | }
|
---|
169 |
|
---|
170 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
171 | base.Populate(node, restoredObjects);
|
---|
172 |
|
---|
173 | title = XmlSupport.GetAttribute("Title", "", node);
|
---|
174 |
|
---|
175 | List<YAxisDescriptor> yAxes = new List<YAxisDescriptor>();
|
---|
176 | foreach (XmlNode yAxisNode in node.SelectNodes("YAxis")) {
|
---|
177 | YAxisDescriptor yAxis = new YAxisDescriptor();
|
---|
178 | yAxis.Label = XmlSupport.GetAttribute("Label", "", yAxisNode);
|
---|
179 | yAxis.GridColor = Color.FromArgb(int.Parse(XmlSupport.GetAttribute("GridColor", Color.LightBlue.ToArgb().ToString(), yAxisNode)));
|
---|
180 | yAxis.Position = (AxisPosition)Enum.Parse(typeof(AxisPosition), XmlSupport.GetAttribute("Position", "Left", yAxisNode));
|
---|
181 | yAxis.ShowGrid = XmlSupport.GetAttribute("ShowGrid", "true", yAxisNode) == "true";
|
---|
182 | yAxis.ShowYAxis = XmlSupport.GetAttribute("ShowYAxis", "true", yAxisNode) == "true";
|
---|
183 | yAxis.ShowYAxisLabel = XmlSupport.GetAttribute("ShowYAxisLabel", "true", yAxisNode) == "true";
|
---|
184 | yAxis.ClipChangeable = XmlSupport.GetAttribute("ClipChangeable", "true", yAxisNode) == "true";
|
---|
185 | yAxes.Add(yAxis);
|
---|
186 |
|
---|
187 | if (XmlSupport.GetAttribute("Default", null, yAxisNode) != null)
|
---|
188 | defaultYAxisDescriptor = yAxis;
|
---|
189 | }
|
---|
190 |
|
---|
191 | XmlNode xAxisElement = node.SelectSingleNode("XAxis");
|
---|
192 | if (xAxisElement != null) {
|
---|
193 | xAxisDescriptor.Color = Color.FromArgb(int.Parse(XmlSupport.GetAttribute("Color", Color.Blue.ToArgb().ToString(), xAxisElement)));
|
---|
194 | xAxisDescriptor.GridColor = Color.FromArgb(int.Parse(XmlSupport.GetAttribute("GridColor", Color.LightBlue.ToArgb().ToString(), xAxisElement)));
|
---|
195 | xAxisDescriptor.Label = XmlSupport.GetAttribute("Label", "", xAxisElement);
|
---|
196 | xAxisDescriptor.ShowGrid = XmlSupport.GetAttribute("ShowGrid", "true", xAxisElement) == "true";
|
---|
197 | xAxisDescriptor.ShowLabel = XmlSupport.GetAttribute("ShowLabel", "true", xAxisElement) == "true";
|
---|
198 | }
|
---|
199 |
|
---|
200 | XmlNode xAxisLabelProviderNode = node.SelectSingleNode("LabelProvider");
|
---|
201 | xAxisDescriptor.LabelProvider = xAxisDescriptor.LabelProvider.PopulateLabelProviderXmlNode(xAxisLabelProviderNode);
|
---|
202 |
|
---|
203 | foreach (XmlNode dataRow in node.SelectNodes("Row")) {
|
---|
204 | string rowLabel = XmlSupport.GetAttribute("Label", "", dataRow);
|
---|
205 |
|
---|
206 | DataRow row = new DataRow();
|
---|
207 | row.RowSettings.Label = rowLabel;
|
---|
208 | row.RowSettings.Color = Color.FromArgb(Int32.Parse(XmlSupport.GetAttribute("Color", Color.Black.ToArgb().ToString(), dataRow)));
|
---|
209 | row.RowSettings.LineType = (DataRowType)Enum.Parse(typeof(DataRowType), XmlSupport.GetAttribute("LineType", "Normal", dataRow));
|
---|
210 | row.RowSettings.Thickness = Int32.Parse(XmlSupport.GetAttribute("Thickness", "2", dataRow));
|
---|
211 | row.RowSettings.ShowMarkers = XmlSupport.GetAttribute("ShowMarkers", "true", dataRow) == "true";
|
---|
212 | row.RowSettings.Style = (DrawingStyle)Enum.Parse(typeof (DrawingStyle), XmlSupport.GetAttribute("Style", DrawingStyle.Solid.ToString(), dataRow));
|
---|
213 |
|
---|
214 | foreach (YAxisDescriptor yAxis in yAxes) {
|
---|
215 | if (yAxis.Label.Equals(XmlSupport.GetAttribute("YAxis", dataRow)))
|
---|
216 | row.YAxis = yAxis;
|
---|
217 | }
|
---|
218 |
|
---|
219 | string[] tokens = dataRow.InnerText.Split(';');
|
---|
220 | foreach (string token in tokens) {
|
---|
221 | double value = double.Parse(token, CultureInfo.InvariantCulture);
|
---|
222 | row.AddValue(value);
|
---|
223 | }
|
---|
224 |
|
---|
225 | AddDataRow(row);
|
---|
226 | }
|
---|
227 | }
|
---|
228 | }
|
---|
229 | }
|
---|