#region License Information /* HeuristicLab * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Drawing; using System.Drawing.Text; namespace HeuristicLab.Visualization { public sealed class LabeledPrimitive : RectangularPrimitiveBase { public string Label { get; set; } public Font Font { get; set; } public RectangularPrimitiveBase Primitive { get; private set; } public Brush TextBrush { get; set; } public LabeledPrimitive(RectangularPrimitiveBase primitive, string label, Font font, Brush textBrush = null) : base(primitive.Chart, primitive.LowerLeft, primitive.UpperRight, primitive.Pen, primitive.Brush) { Label = label; Primitive = primitive; Font = font; TextBrush = textBrush; } public override void Draw(Graphics graphics) { base.Draw(graphics); Primitive.Draw(graphics); } public override bool ContainsPoint(PointD point) { return Primitive.ContainsPoint(point); } public override void PostDraw(Graphics graphics) { base.PostDraw(graphics); graphics.TextRenderingHint = TextRenderingHint.AntiAlias; if (string.IsNullOrEmpty(Label)) return; var labelSize = graphics.MeasureString(Label, Font); var primitiveSize = Chart.TransformWorldToPixel(new SizeD(Primitive.Size.Width, Primitive.Size.Height)); float scale = Math.Min(primitiveSize.Width / labelSize.Width, primitiveSize.Height / labelSize.Height); Font = new Font(Font.FontFamily, Math.Min(12, Font.Size * scale)); labelSize = graphics.MeasureString(Label, Font); var p1 = Chart.TransformWorldToPixel(LowerLeft); var p2 = Chart.TransformWorldToPixel(UpperRight); int width = Math.Abs(p1.X - p2.X); int height = Math.Abs(p1.Y - p2.Y); var brush = TextBrush ?? new SolidBrush(Primitive.Pen.Color); graphics.DrawString(Label, Font, brush, p1.X + (width - labelSize.Width) / 2f, p2.Y + (height - labelSize.Height) / 2f); } public override void Move(PointD point, Offset delta) { Primitive.Move(point, delta); base.Move(point, delta); } public override void Move(Offset delta) { Primitive.Move(delta); base.Move(delta); } } }