Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/BoxChart/StringValueBoxChartElementGenerator.cs @ 17460

Last change on this file since 17460 was 16995, checked in by gkronber, 6 years ago

#2520 Update plugin dependencies and references for HL.FLA for new persistence

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