[680] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
[864] | 3 | using System.Globalization;
|
---|
[726] | 4 | using System.Xml;
|
---|
| 5 | using HeuristicLab.Core;
|
---|
[1061] | 6 | using System.Text;
|
---|
[1194] | 7 | using HeuristicLab.Visualization.LabelProvider;
|
---|
[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 |
|
---|
[859] | 15 | public class ChartDataRowsModel : ChartDataModelBase, IChartDataRowsModel{
|
---|
[1182] | 16 | private string title = "Title";
|
---|
[1462] | 17 | private string xAxisLabel = "";
|
---|
| 18 | private bool showXAxisLabel = true;
|
---|
[1194] | 19 | private ILabelProvider labelProvider = new ContinuousLabelProvider("0.##");
|
---|
[726] | 20 |
|
---|
[1341] | 21 | private ViewSettings viewSettings = new ViewSettings();
|
---|
| 22 |
|
---|
[1190] | 23 | public ILabelProvider XAxisLabelProvider {
|
---|
| 24 | get { return labelProvider; }
|
---|
[1191] | 25 | set{
|
---|
| 26 | this.labelProvider = value;
|
---|
| 27 | OnModelChanged();
|
---|
| 28 | }
|
---|
[1190] | 29 | }
|
---|
| 30 |
|
---|
[1350] | 31 | public List<YAxisDescriptor> YAxes {
|
---|
| 32 | get {
|
---|
| 33 | Dictionary<YAxisDescriptor, object> yaxes = new Dictionary<YAxisDescriptor, object>();
|
---|
[1190] | 34 |
|
---|
[1350] | 35 | foreach (IDataRow row in rows) {
|
---|
| 36 | yaxes[row.YAxis] = null;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | return new List<YAxisDescriptor>(yaxes.Keys);
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 |
|
---|
[859] | 44 | private readonly List<IDataRow> rows = new List<IDataRow>();
|
---|
[1190] | 45 | //private readonly List<string> xLabels = new List<string>();
|
---|
[726] | 46 |
|
---|
[1190] | 47 | //public List<string> XLabels{
|
---|
| 48 | // get { return xLabels; }
|
---|
| 49 | //}
|
---|
[685] | 50 |
|
---|
[859] | 51 | public List<IDataRow> Rows{
|
---|
| 52 | get { return rows; }
|
---|
[761] | 53 | }
|
---|
[685] | 54 |
|
---|
[761] | 55 | public string Title {
|
---|
| 56 | get { return title; }
|
---|
| 57 | set {
|
---|
| 58 | title = value;
|
---|
| 59 | OnModelChanged();
|
---|
[728] | 60 | }
|
---|
[761] | 61 | }
|
---|
[685] | 62 |
|
---|
[1462] | 63 | public string XAxisLabel {
|
---|
| 64 | get { return xAxisLabel; }
|
---|
| 65 | set {
|
---|
| 66 | xAxisLabel = value;
|
---|
| 67 | OnModelChanged();
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public bool ShowXAxisLabel {
|
---|
| 72 | get { return showXAxisLabel; }
|
---|
| 73 | set {
|
---|
| 74 | showXAxisLabel = value;
|
---|
| 75 | OnModelChanged();
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[859] | 79 | public override IView CreateView() {
|
---|
| 80 | return new LineChart(this);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[1190] | 83 | //public void AddLabel(string label) {
|
---|
| 84 | // xLabels.Add(label);
|
---|
| 85 | // OnModelChanged();
|
---|
| 86 | //}
|
---|
[859] | 87 |
|
---|
[1190] | 88 | //public void AddLabel(string label, int index) {
|
---|
| 89 | // xLabels[index] = label;
|
---|
| 90 | // OnModelChanged();
|
---|
| 91 | //}
|
---|
[859] | 92 |
|
---|
[1190] | 93 | //public void AddLabels(string[] labels) {
|
---|
| 94 | // foreach (var s in labels){
|
---|
| 95 | // AddLabel(s);
|
---|
| 96 | // }
|
---|
| 97 | // //OnModelChanged();
|
---|
| 98 | //}
|
---|
[726] | 99 |
|
---|
[1190] | 100 | //public void AddLabels(string[] labels, int index) {
|
---|
| 101 | // int i = 0;
|
---|
| 102 | // foreach (var s in labels){
|
---|
| 103 | // AddLabel(s, index + i);
|
---|
| 104 | // i++;
|
---|
| 105 | // }
|
---|
| 106 | // //OnModelChanged();
|
---|
| 107 | //}
|
---|
[697] | 108 |
|
---|
[1190] | 109 | //public void ModifyLabel(string label, int index) {
|
---|
| 110 | // xLabels[index] = label;
|
---|
| 111 | // OnModelChanged();
|
---|
| 112 | //}
|
---|
[859] | 113 |
|
---|
[1190] | 114 | //public void ModifyLabels(string[] labels, int index) {
|
---|
| 115 | // int i = 0;
|
---|
| 116 | // foreach (var s in labels){
|
---|
| 117 | // ModifyLabel(s, index + i);
|
---|
| 118 | // i++;
|
---|
| 119 | // }
|
---|
| 120 | // //OnModelChanged();
|
---|
| 121 | //}
|
---|
[697] | 122 |
|
---|
[1190] | 123 | //public void RemoveLabel(int index) {
|
---|
| 124 | // xLabels.RemoveAt(index);
|
---|
| 125 | // OnModelChanged();
|
---|
| 126 | //}
|
---|
[726] | 127 |
|
---|
[1190] | 128 | //public void RemoveLabels(int index, int count) {
|
---|
| 129 | // for (int i = index; i < index + count; i++ ){
|
---|
| 130 | // RemoveLabel(i);
|
---|
| 131 | // }
|
---|
| 132 | // //OnModelChanged();
|
---|
| 133 | //}
|
---|
[859] | 134 |
|
---|
[1350] | 135 | private readonly YAxisDescriptor defaultYAxisDescriptor = new YAxisDescriptor();
|
---|
| 136 |
|
---|
[761] | 137 | public void AddDataRow(IDataRow row) {
|
---|
[1350] | 138 | if (row.YAxis == null) {
|
---|
| 139 | row.YAxis = defaultYAxisDescriptor;
|
---|
| 140 | }
|
---|
[761] | 141 | rows.Add(row);
|
---|
| 142 | OnDataRowAdded(row);
|
---|
[697] | 143 | }
|
---|
[726] | 144 |
|
---|
[761] | 145 | public void RemoveDataRow(IDataRow row) {
|
---|
| 146 | rows.Remove(row);
|
---|
| 147 | OnDataRowRemoved(row);
|
---|
| 148 | }
|
---|
[726] | 149 |
|
---|
[1387] | 150 | public YAxisDescriptor DefaultYAxis {
|
---|
| 151 | get { return defaultYAxisDescriptor; }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[1285] | 154 | // TODO implement calculation of max data row values
|
---|
| 155 | public int MaxDataRowValues {
|
---|
| 156 | get {
|
---|
| 157 | int max = 0;
|
---|
| 158 |
|
---|
| 159 | foreach (IDataRow row in rows) {
|
---|
| 160 | max = Math.Max(max, row.Count);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | return max;
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[1341] | 167 | public ViewSettings ViewSettings {
|
---|
| 168 | get { return viewSettings; }
|
---|
| 169 | set { viewSettings = value; }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[859] | 172 | public event ModelChangedHandler ModelChanged;
|
---|
| 173 |
|
---|
| 174 | protected void OnModelChanged() {
|
---|
| 175 | if (ModelChanged != null) {
|
---|
| 176 | ModelChanged();
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[761] | 180 | public event DataRowAddedHandler DataRowAdded;
|
---|
[726] | 181 |
|
---|
[761] | 182 | protected void OnDataRowAdded(IDataRow row) {
|
---|
| 183 | if (DataRowAdded != null) {
|
---|
| 184 | DataRowAdded(row);
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
[726] | 187 |
|
---|
[761] | 188 | public event DataRowRemovedHandler DataRowRemoved;
|
---|
[727] | 189 |
|
---|
[761] | 190 | protected void OnDataRowRemoved(IDataRow row) {
|
---|
| 191 | if (DataRowRemoved != null) {
|
---|
| 192 | DataRowRemoved(row);
|
---|
[726] | 193 | }
|
---|
[761] | 194 | }
|
---|
[726] | 195 |
|
---|
[761] | 196 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
[979] | 197 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
| 198 |
|
---|
[1194] | 199 | foreach (IDataRow row in rows) {
|
---|
[979] | 200 | XmlNode columnElement = document.CreateNode(XmlNodeType.Element, "row", null);
|
---|
| 201 |
|
---|
| 202 | XmlAttribute idAttr = document.CreateAttribute("label");
|
---|
[1194] | 203 | idAttr.Value = row.Label;
|
---|
[979] | 204 | columnElement.Attributes.Append(idAttr);
|
---|
| 205 |
|
---|
[1061] | 206 | StringBuilder builder = new StringBuilder();
|
---|
| 207 |
|
---|
[979] | 208 | for (int i = 0; i < row.Count; i++) {
|
---|
| 209 | if (i == 0) {
|
---|
[1061] | 210 | builder.Append(row[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
| 211 | //columnElement.InnerText += row[i].ToString(CultureInfo.InvariantCulture.NumberFormat);
|
---|
[979] | 212 | } else {
|
---|
[1061] | 213 | builder.Append(";" + row[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
|
---|
| 214 | //columnElement.InnerText += ";" + row[i].ToString(CultureInfo.InvariantCulture.NumberFormat);
|
---|
[979] | 215 | }
|
---|
| 216 | }
|
---|
[1061] | 217 | columnElement.InnerText += builder.ToString();
|
---|
[979] | 218 | node.AppendChild(columnElement);
|
---|
| 219 | }
|
---|
[1385] | 220 |
|
---|
| 221 | XmlNode labelProviderNode = document.ImportNode(labelProvider.GetLabelProviderXmlNode(), true);
|
---|
| 222 | node.AppendChild(labelProviderNode);
|
---|
| 223 |
|
---|
[979] | 224 | return node;
|
---|
[761] | 225 | }
|
---|
| 226 |
|
---|
[726] | 227 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
[979] | 228 | base.Populate(node, restoredObjects);
|
---|
[731] | 229 |
|
---|
[979] | 230 | foreach (XmlNode dataRow in node.ChildNodes) {
|
---|
[1385] | 231 | if (dataRow.Name.Equals("LabelProvider")) {
|
---|
| 232 | labelProvider = labelProvider.PopulateLabelProviderXmlNode(dataRow);
|
---|
| 233 | } else {
|
---|
| 234 | XmlAttributeCollection attrs = dataRow.Attributes;
|
---|
| 235 | XmlAttribute rowIdAttr = (XmlAttribute)attrs.GetNamedItem("label");
|
---|
| 236 | string rowLabel = rowIdAttr.Value;
|
---|
| 237 | DataRow row = new DataRow();
|
---|
| 238 | row.Label = rowLabel;
|
---|
[979] | 239 |
|
---|
[1385] | 240 | string[] tokens = dataRow.InnerText.Split(';');
|
---|
| 241 | double[] data = new double[tokens.Length];
|
---|
| 242 | for (int i = 0; i < data.Length; i++) {
|
---|
| 243 | if (tokens[i].Length != 0) {
|
---|
| 244 | if (
|
---|
| 245 | double.TryParse(tokens[i], NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out data[i]) ==
|
---|
| 246 | false) {
|
---|
| 247 | throw new FormatException("Can't parse " + tokens[i] + " as double value.");
|
---|
| 248 | }
|
---|
[979] | 249 | }
|
---|
| 250 | }
|
---|
[1385] | 251 | row.AddValues(data);
|
---|
| 252 | AddDataRow(row);
|
---|
[979] | 253 | }
|
---|
| 254 | }
|
---|
[726] | 255 | }
|
---|
[680] | 256 | }
|
---|
[761] | 257 | }
|
---|