Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/LabelProvider/StringLabelProvider.cs @ 1389

Last change on this file since 1389 was 1385, checked in by gkragl, 16 years ago

Implemented persistence mechanism for XAxisLabelProvider (#434)

File size: 1.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.Visualization.LabelProvider;
4using System.Xml;
5
6namespace HeuristicLab.Visualization.LabelProvider {
7  public class StringLabelProvider : ILabelProvider {
8    private readonly Dictionary<int, string> labels = new Dictionary<int, string>();
9
10    public void ClearLabels() {
11      labels.Clear();
12    }
13
14    public void SetLabel(int index, string label) {
15      labels[index] = label;
16    }
17
18    public string GetLabel(double value) {
19      int index = (int)Math.Round(value);
20      double delta = Math.Abs(index - value);
21
22      string label;
23
24      if (delta < 1e-10 && labels.TryGetValue(index, out label))
25        return label;
26      else
27        return string.Empty;
28    }
29
30    public XmlNode GetLabelProviderXmlNode() {
31      XmlDocument Xdoc = new XmlDocument();
32
33      XmlNode lblProvInfo = Xdoc.CreateNode(XmlNodeType.Element, "LabelProvider", null);
34      lblProvInfo.InnerText = "StringLabelProvider";
35
36      foreach (KeyValuePair<int, string> pair in labels)
37      {
38        XmlNode strLbl = Xdoc.CreateNode(XmlNodeType.Element, "String", null);
39
40        XmlAttribute idStrLbl = Xdoc.CreateAttribute("id");
41        idStrLbl.Value = pair.Key.ToString();
42        strLbl.Attributes.Append(idStrLbl);
43
44        strLbl.InnerText = pair.Value;
45        lblProvInfo.AppendChild(strLbl);
46      }
47      return lblProvInfo;
48    }
49
50    public ILabelProvider PopulateLabelProviderXmlNode(XmlNode node) {
51      var labelProvider = new StringLabelProvider();
52
53      foreach (XmlNode strLbl in node.SelectNodes("//String"))
54      {
55        labelProvider.SetLabel(int.Parse(strLbl.Attributes[0].Value), strLbl.InnerText);
56      }
57      return labelProvider;
58    }
59  }
60}
Note: See TracBrowser for help on using the repository browser.