[680] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
[1988] | 3 | using System.Drawing;
|
---|
[864] | 4 | using System.Globalization;
|
---|
[726] | 5 | using System.Xml;
|
---|
| 6 | using HeuristicLab.Core;
|
---|
[1061] | 7 | using System.Text;
|
---|
[1341] | 8 | using HeuristicLab.Visualization.Options;
|
---|
[680] | 9 |
|
---|
[685] | 10 | namespace HeuristicLab.Visualization{
|
---|
[761] | 11 | public delegate void DataRowAddedHandler(IDataRow row);
|
---|
| 12 | public delegate void DataRowRemovedHandler(IDataRow row);
|
---|
| 13 | public delegate void ModelChangedHandler();
|
---|
[685] | 14 |
|
---|
[1886] | 15 | public class ChartDataRowsModel : ItemBase, IChartDataRowsModel {
|
---|
[1182] | 16 | private string title = "Title";
|
---|
[1341] | 17 | private ViewSettings viewSettings = new ViewSettings();
|
---|
[1880] | 18 | private readonly XAxisDescriptor xAxisDescriptor = new XAxisDescriptor();
|
---|
[1988] | 19 | private YAxisDescriptor defaultYAxisDescriptor = new YAxisDescriptor();
|
---|
[1886] | 20 | private readonly List<IDataRow> rows = new List<IDataRow>();
|
---|
[1880] | 21 |
|
---|
| 22 | public ChartDataRowsModel() {
|
---|
| 23 | this.XAxis.XAxisDescriptorChanged += delegate { OnModelChanged(); };
|
---|
[1876] | 24 | }
|
---|
| 25 |
|
---|
[1886] | 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 |
|
---|
[1880] | 38 | public XAxisDescriptor XAxis {
|
---|
| 39 | get { return xAxisDescriptor; }
|
---|
[1190] | 40 | }
|
---|
| 41 |
|
---|
[1350] | 42 | public List<YAxisDescriptor> YAxes {
|
---|
| 43 | get {
|
---|
| 44 | Dictionary<YAxisDescriptor, object> yaxes = new Dictionary<YAxisDescriptor, object>();
|
---|
[1190] | 45 |
|
---|
[1350] | 46 | foreach (IDataRow row in rows) {
|
---|
| 47 | yaxes[row.YAxis] = null;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | return new List<YAxisDescriptor>(yaxes.Keys);
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[1886] | 54 | public YAxisDescriptor DefaultYAxis {
|
---|
| 55 | get { return defaultYAxisDescriptor; }
|
---|
| 56 | }
|
---|
[1350] | 57 |
|
---|
[859] | 58 | public List<IDataRow> Rows{
|
---|
| 59 | get { return rows; }
|
---|
[761] | 60 | }
|
---|
[685] | 61 |
|
---|
[761] | 62 | public void AddDataRow(IDataRow row) {
|
---|
[1350] | 63 | if (row.YAxis == null) {
|
---|
| 64 | row.YAxis = defaultYAxisDescriptor;
|
---|
| 65 | }
|
---|
[761] | 66 | rows.Add(row);
|
---|
| 67 | OnDataRowAdded(row);
|
---|
[697] | 68 | }
|
---|
[726] | 69 |
|
---|
[1986] | 70 | public void AddDataRows(params IDataRow[] rows) {
|
---|
[1985] | 71 | foreach (IDataRow row in rows) {
|
---|
| 72 | AddDataRow(row);
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[761] | 76 | public void RemoveDataRow(IDataRow row) {
|
---|
| 77 | rows.Remove(row);
|
---|
| 78 | OnDataRowRemoved(row);
|
---|
| 79 | }
|
---|
[726] | 80 |
|
---|
[1285] | 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 |
|
---|
[1341] | 94 | public ViewSettings ViewSettings {
|
---|
| 95 | get { return viewSettings; }
|
---|
| 96 | set { viewSettings = value; }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[859] | 99 | public event ModelChangedHandler ModelChanged;
|
---|
| 100 |
|
---|
| 101 | protected void OnModelChanged() {
|
---|
| 102 | if (ModelChanged != null) {
|
---|
| 103 | ModelChanged();
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[761] | 107 | public event DataRowAddedHandler DataRowAdded;
|
---|
[726] | 108 |
|
---|
[761] | 109 | protected void OnDataRowAdded(IDataRow row) {
|
---|
| 110 | if (DataRowAdded != null) {
|
---|
| 111 | DataRowAdded(row);
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
[726] | 114 |
|
---|
[761] | 115 | public event DataRowRemovedHandler DataRowRemoved;
|
---|
[727] | 116 |
|
---|
[761] | 117 | protected void OnDataRowRemoved(IDataRow row) {
|
---|
| 118 | if (DataRowRemoved != null) {
|
---|
| 119 | DataRowRemoved(row);
|
---|
[726] | 120 | }
|
---|
[761] | 121 | }
|
---|
[726] | 122 |
|
---|
[761] | 123 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
[979] | 124 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
[1988] | 125 |
|
---|
| 126 | List<YAxisDescriptor> yAxis = new List<YAxisDescriptor>();
|
---|
| 127 | yAxis.Add(defaultYAxisDescriptor);
|
---|
[1194] | 128 | foreach (IDataRow row in rows) {
|
---|
[1988] | 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 | }
|
---|
[979] | 137 |
|
---|
[1988] | 138 | foreach (YAxisDescriptor axis in yAxis) {
|
---|
| 139 | XmlNode yAxisElement = document.CreateNode(XmlNodeType.Element, "yAxis", null);
|
---|
[979] | 140 |
|
---|
[1988] | 141 | XmlAttribute attrLabel = document.CreateAttribute("label");
|
---|
| 142 | attrLabel.Value = axis.Label;
|
---|
| 143 | yAxisElement.Attributes.Append(attrLabel);
|
---|
[1061] | 144 |
|
---|
[1988] | 145 | if (axis == defaultYAxisDescriptor) {
|
---|
| 146 | XmlAttribute attrDefault = document.CreateAttribute("default");
|
---|
| 147 | attrDefault.Value = "true";
|
---|
| 148 | yAxisElement.Attributes.Append(attrDefault);
|
---|
[979] | 149 | }
|
---|
[1988] | 150 | node.AppendChild(yAxisElement);
|
---|
| 151 |
|
---|
[979] | 152 | }
|
---|
[1385] | 153 |
|
---|
[1881] | 154 | XmlNode labelProviderNode = document.ImportNode(XAxis.LabelProvider.GetLabelProviderXmlNode(), true);
|
---|
[1385] | 155 | node.AppendChild(labelProviderNode);
|
---|
| 156 |
|
---|
[1988] | 157 | return node;
|
---|
[761] | 158 | }
|
---|
| 159 |
|
---|
[726] | 160 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
[979] | 161 | base.Populate(node, restoredObjects);
|
---|
[731] | 162 |
|
---|
[1988] | 163 | List<YAxisDescriptor> yAxis = new List<YAxisDescriptor>();
|
---|
| 164 | foreach (XmlNode dataRow in node.ChildNodes)
|
---|
| 165 | {
|
---|
| 166 | if (dataRow.Name.Equals("yAxis"))
|
---|
| 167 | {
|
---|
| 168 | XmlAttributeCollection attrs = dataRow.Attributes;
|
---|
| 169 |
|
---|
| 170 | XmlAttribute attrYAxisLabel = (XmlAttribute)attrs.GetNamedItem("label");
|
---|
| 171 | string axisLabel = attrYAxisLabel.Value;
|
---|
| 172 | YAxisDescriptor axis = new YAxisDescriptor();
|
---|
| 173 | axis.Label = axisLabel;
|
---|
| 174 | yAxis.Add(axis);
|
---|
| 175 |
|
---|
| 176 | XmlAttribute attrDefault = (XmlAttribute)attrs.GetNamedItem("default");
|
---|
| 177 | if (attrDefault != null) {
|
---|
| 178 | defaultYAxisDescriptor = axis;
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[979] | 183 | foreach (XmlNode dataRow in node.ChildNodes) {
|
---|
[1385] | 184 | if (dataRow.Name.Equals("LabelProvider")) {
|
---|
[1881] | 185 | XAxis.LabelProvider = XAxis.LabelProvider.PopulateLabelProviderXmlNode(dataRow);
|
---|
[1988] | 186 | } else if (dataRow.Name.Equals("row")) {
|
---|
[1385] | 187 | XmlAttributeCollection attrs = dataRow.Attributes;
|
---|
| 188 | XmlAttribute rowIdAttr = (XmlAttribute)attrs.GetNamedItem("label");
|
---|
| 189 | string rowLabel = rowIdAttr.Value;
|
---|
[1988] | 190 | string rowColor = attrs.GetNamedItem("color").Value;
|
---|
| 191 |
|
---|
[1385] | 192 | DataRow row = new DataRow();
|
---|
[1962] | 193 | row.RowSettings.Label = rowLabel;
|
---|
[1988] | 194 | row.RowSettings.Color = Color.FromName(rowColor);
|
---|
[979] | 195 |
|
---|
[1988] | 196 | string yAxisLabel = attrs.GetNamedItem("yAxis").Value;
|
---|
| 197 | foreach (YAxisDescriptor axis in yAxis) {
|
---|
| 198 | if (axis.Label.Equals(yAxisLabel)) {
|
---|
| 199 | row.YAxis = axis;
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[1385] | 203 | string[] tokens = dataRow.InnerText.Split(';');
|
---|
| 204 | double[] data = new double[tokens.Length];
|
---|
| 205 | for (int i = 0; i < data.Length; i++) {
|
---|
| 206 | if (tokens[i].Length != 0) {
|
---|
| 207 | if (
|
---|
| 208 | double.TryParse(tokens[i], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out data[i]) ==
|
---|
| 209 | false) {
|
---|
| 210 | throw new FormatException("Can't parse " + tokens[i] + " as double value.");
|
---|
| 211 | }
|
---|
[979] | 212 | }
|
---|
| 213 | }
|
---|
[1385] | 214 | row.AddValues(data);
|
---|
| 215 | AddDataRow(row);
|
---|
[979] | 216 | }
|
---|
| 217 | }
|
---|
[726] | 218 | }
|
---|
[680] | 219 | }
|
---|
[761] | 220 | }
|
---|