Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/BoxChart/StringValueBoxChartElementGenerator.cs @ 10657

Last change on this file since 10657 was 7128, checked in by epitzer, 13 years ago

#1696 Integrate fitness landscape analysis plugins from Heureka! repository.

File size: 3.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Drawing.Drawing2D;
5using System.Linq;
6using System.Text;
7using HeuristicLab.Common;
8using HeuristicLab.Core;
9using HeuristicLab.Data;
10using HeuristicLab.Optimization;
11using HeuristicLab.Parameters;
12using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
13
14namespace HeuristicLab.Analysis.FitnessLandscape.BoxCharts {
15
16  [StorableClass]
17  public class StringValueBoxChartElementGenerator : EmptyBoxChartElementGenerator {
18
19    public ValueParameter<StringValue> ValueNameParameter {
20      get { return (ValueParameter<StringValue>)Parameters["ValueName"]; }
21    }
22    public ValueParameter<IntValue> FontSizeParameter {
23      get { return (ValueParameter<IntValue>) Parameters["FontSize"]; }
24    }
25
26    protected string ValueName { get { return ValueNameParameter.Value.Value; } }
27    protected int FontSize { get { return FontSizeParameter.Value.Value; }}
28
29    #region Construction & Cloning
30
31    [StorableConstructor]
32    protected StringValueBoxChartElementGenerator(bool deserializing) : base(deserializing) { }
33    protected StringValueBoxChartElementGenerator(StringValueBoxChartElementGenerator original, Cloner cloner) : base(original, cloner) {
34      RegisterEvents();
35    }
36    public StringValueBoxChartElementGenerator() {
37      Parameters.Add(new ValueParameter<StringValue>("ValueName", "The name of the result of parameter ot be visualized.", new StringValue("Value")));
38      Parameters.Add(new ValueParameter<IntValue>("FontSize", "The font size to be used for the text.", new IntValue(11)));
39      RegisterEvents();
40      if (GetType() == typeof(StringValueBoxChartElementGenerator))
41        UpdateName();
42    }
43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new StringValueBoxChartElementGenerator(this, cloner);
45    }
46    [StorableHook(HookType.AfterDeserialization)]
47    private void AfterDeserialization() {
48      RegisterEvents();
49    }
50    #endregion
51
52    private void RegisterEvents() {
53      ValueNameParameter.ToStringChanged += new EventHandler(Parameters_Changed);
54      FontSizeParameter.ToStringChanged += new EventHandler(Parameters_Changed);
55    }
56
57    private void Parameters_Changed(object sender, EventArgs eventArgs) {
58      UpdateName();
59    }
60
61    protected override string CalculateName() {
62      return string.Format("{0} str {1}", base.CalculateName(), ValueName);
63    }
64
65    private string GetValue(IRun run) {
66      IItem value;
67      run.Results.TryGetValue(ValueName, out value);
68      if (value != null)
69        return value.ToString();
70      run.Parameters.TryGetValue(ValueName, out value);
71      if (value != null)
72        return value.ToString();
73      return "";
74    }
75
76    public override void Draw(IRun run, Graphics g) {
77      var value = GetValue(run);
78      using(var font = new Font("Helvetica", FontSize)) {
79        var size = g.MeasureString(value, font);
80        g.DrawString(value, font, Brushes.Black,
81          g.ClipBounds.Left+(g.ClipBounds.Width - size.Width)/2,
82          g.ClipBounds.Top+(g.ClipBounds.Height - size.Height)/2);
83      }
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.