using System; using System.Drawing; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HEAL.Attic; namespace HeuristicLab.Analysis.FitnessLandscape.BoxCharts { [StorableType("E04F26F6-E8CF-4EF1-96AC-B3FFF38E4ACE")] public class StringValueBoxChartElementGenerator : EmptyBoxChartElementGenerator { public ValueParameter ValueNameParameter { get { return (ValueParameter)Parameters["ValueName"]; } } public ValueParameter FontSizeParameter { get { return (ValueParameter) Parameters["FontSize"]; } } protected string ValueName { get { return ValueNameParameter.Value.Value; } } protected int FontSize { get { return FontSizeParameter.Value.Value; }} #region Construction & Cloning [StorableConstructor] protected StringValueBoxChartElementGenerator(StorableConstructorFlag _) : base(_) { } protected StringValueBoxChartElementGenerator(StringValueBoxChartElementGenerator original, Cloner cloner) : base(original, cloner) { RegisterEvents(); } public StringValueBoxChartElementGenerator() { Parameters.Add(new ValueParameter("ValueName", "The name of the result of parameter ot be visualized.", new StringValue("Value"))); Parameters.Add(new ValueParameter("FontSize", "The font size to be used for the text.", new IntValue(11))); RegisterEvents(); if (GetType() == typeof(StringValueBoxChartElementGenerator)) UpdateName(); } public override IDeepCloneable Clone(Cloner cloner) { return new StringValueBoxChartElementGenerator(this, cloner); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { RegisterEvents(); } #endregion private void RegisterEvents() { ValueNameParameter.ToStringChanged += new EventHandler(Parameters_Changed); FontSizeParameter.ToStringChanged += new EventHandler(Parameters_Changed); } private void Parameters_Changed(object sender, EventArgs eventArgs) { UpdateName(); } protected override string CalculateName() { return string.Format("{0} str {1}", base.CalculateName(), ValueName); } private string GetValue(IRun run) { IItem value; run.Results.TryGetValue(ValueName, out value); if (value != null) return value.ToString(); run.Parameters.TryGetValue(ValueName, out value); if (value != null) return value.ToString(); return ""; } public override void Draw(IRun run, Graphics g) { var value = GetValue(run); using(var font = new Font("Helvetica", FontSize)) { var size = g.MeasureString(value, font); g.DrawString(value, font, Brushes.Black, g.ClipBounds.Left+(g.ClipBounds.Width - size.Width)/2, g.ClipBounds.Top+(g.ClipBounds.Height - size.Height)/2); } } } }