123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- // See the LICENSE file in the project root for more information.
- //
- // Purpose: Image annotation classes.
- //
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Drawing.Design;
- using System.Drawing.Drawing2D;
- using FastReport.DataVisualization.Charting.Utilities;
- namespace FastReport.DataVisualization.Charting
- {
- /// <summary>
- /// <b>ImageAnnotation</b> is a class that represents an image annotation.
- /// </summary>
- [
- SRDescription("DescriptionAttributeImageAnnotation_ImageAnnotation"),
- ]
- public class ImageAnnotation : Annotation
- {
- #region Fields
- // Annotation image name
- private string _imageName = String.Empty;
- // Image wrapping mode
- private ChartImageWrapMode _imageWrapMode = ChartImageWrapMode.Scaled;
- // Image transparent color
- private Color _imageTransparentColor = Color.Empty;
- #endregion
- #region Construction and Initialization
- /// <summary>
- /// Default public constructor.
- /// </summary>
- public ImageAnnotation()
- : base()
- {
- }
- #endregion
- #region Properties
- #region Image properties
- /// <summary>
- /// Gets or sets the name of an annotation's image.
- /// <seealso cref="ImageTransparentColor"/>
- /// </summary>
- /// <value>
- /// A string value representing the name of an annotation's image.
- /// </value>
- /// <remarks>
- /// The name can be a file name, URL for the web control or a name from
- /// the <see cref="NamedImagesCollection"/> class.
- /// </remarks>
- [
- SRCategory("CategoryAttributeImage"),
- Bindable(true),
- DefaultValue(""),
- #if DESIGNER
- Editor(typeof(ImageValueEditor), typeof(UITypeEditor)),
- #endif
- SRDescription("DescriptionAttributeImageAnnotation_Image"),
- ]
- public virtual string Image
- {
- get
- {
- return _imageName;
- }
- set
- {
- _imageName = value;
- this.Invalidate();
- }
- }
- /// <summary>
- /// Gets or sets the drawing mode of the image.
- /// </summary>
- /// <value>
- /// A <see cref="ChartImageWrapMode"/> value that defines the drawing mode of the image.
- /// </value>
- [
- SRCategory("CategoryAttributeImage"),
- Bindable(true),
- DefaultValue(ChartImageWrapMode.Scaled),
- SRDescription("DescriptionAttributeImageWrapMode"),
- ]
- public ChartImageWrapMode ImageWrapMode
- {
- get
- {
- return _imageWrapMode;
- }
- set
- {
- _imageWrapMode = value;
- this.Invalidate();
- }
- }
- /// <summary>
- /// Gets or sets a color which will be replaced with a transparent color while drawing the image.
- /// </summary>
- /// <value>
- /// A <see cref="Color"/> value which will be replaced with a transparent color while drawing the image.
- /// </value>
- [
- SRCategory("CategoryAttributeImage"),
- Bindable(true),
- DefaultValue(typeof(Color), ""),
- SRDescription("DescriptionAttributeImageTransparentColor"),
- TypeConverter(typeof(ColorConverter)),
- #if DESIGNER
- Editor(typeof(ChartColorEditor), typeof(UITypeEditor))
- #endif
- ]
- public Color ImageTransparentColor
- {
- get
- {
- return _imageTransparentColor;
- }
- set
- {
- _imageTransparentColor = value;
- this.Invalidate();
- }
- }
- /// <summary>
- /// Gets or sets an annotation's content alignment.
- /// </summary>
- /// <value>
- /// A <see cref="ContentAlignment"/> value that represents the content alignment.
- /// </value>
- /// <remarks>
- /// This property is used to align text for <see cref="TextAnnotation"/>, <see cref="RectangleAnnotation"/>,
- /// <see cref="EllipseAnnotation"/> and <see cref="CalloutAnnotation"/> objects, and to align
- /// a non-scaled image inside an <see cref="ImageAnnotation"/> object.
- /// </remarks>
- [
- SRCategory("CategoryAttributeImage"),
- DefaultValue(typeof(ContentAlignment), "MiddleCenter"),
- SRDescription("DescriptionAttributeImageAnnotation_Alignment"),
- ]
- override public ContentAlignment Alignment
- {
- get
- {
- return base.Alignment;
- }
- set
- {
- base.Alignment = value;
- Invalidate();
- }
- }
- /// <summary>
- /// Gets or sets an annotation's text style.
- /// <seealso cref="Font"/>
- /// <seealso cref="ForeColor"/>
- /// </summary>
- /// <value>
- /// A <see cref="TextStyle"/> value used to draw an annotation's text.
- /// </value>
- [Browsable(false)]
- [EditorBrowsable(EditorBrowsableState.Never)]
- public override TextStyle TextStyle
- {
- get
- {
- return base.TextStyle;
- }
- set
- {
- base.TextStyle = value;
- }
- }
- #endregion // Image properties
- #region Other
- /// <summary>
- /// Gets or sets an annotation's type name.
- /// </summary>
- /// <remarks>
- /// This property is used to get the name of each annotation type
- /// (e.g. Line, Rectangle, Ellipse).
- /// <para>
- /// This property is for internal use and is hidden at design and run time.
- /// </para>
- /// </remarks>
- [
- SRCategory("CategoryAttributeMisc"),
- Bindable(true),
- Browsable(false),
- EditorBrowsableAttribute(EditorBrowsableState.Never),
- DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden),
- SerializationVisibilityAttribute(SerializationVisibility.Hidden),
- SRDescription("DescriptionAttributeAnnotationType"),
- ]
- public override string AnnotationType
- {
- get
- {
- return "Image";
- }
- }
- /// <summary>
- /// Gets or sets annotation selection points style.
- /// </summary>
- /// <value>
- /// A <see cref="SelectionPointsStyle"/> value that represents annotation
- /// selection style.
- /// </value>
- /// <remarks>
- /// This property is for internal use and is hidden at design and run time.
- /// </remarks>
- [
- SRCategory("CategoryAttributeAppearance"),
- DefaultValue(SelectionPointsStyle.Rectangle),
- ParenthesizePropertyNameAttribute(true),
- Browsable(false),
- EditorBrowsableAttribute(EditorBrowsableState.Never),
- DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden),
- SerializationVisibilityAttribute(SerializationVisibility.Hidden),
- SRDescription("DescriptionAttributeSelectionPointsStyle"),
- ]
- override internal SelectionPointsStyle SelectionPointsStyle
- {
- get
- {
- return SelectionPointsStyle.Rectangle;
- }
- }
- #endregion
- #region Non Applicable Annotation Appearance Attributes (set as Non-Browsable)
- /// <summary>
- /// Not applicable to this type of annotation.
- /// <seealso cref="Font"/>
- /// </summary>
- /// <value>
- /// A <see cref="Color"/> value.
- /// </value>
- [
- SRCategory("CategoryAttributeAppearance"),
- Browsable(false),
- DefaultValue(typeof(Color), "Black"),
- TypeConverter(typeof(ColorConverter)),
- #if DESIGNER
- Editor(typeof(ChartColorEditor), typeof(UITypeEditor))
- #endif
- ]
- override public Color ForeColor
- {
- get
- {
- return base.ForeColor;
- }
- set
- {
- base.ForeColor = value;
- }
- }
- /// <summary>
- /// Not applicable to this type of annotation.
- /// </summary>
- /// <value>
- /// A <see cref="Font"/> object.
- /// </value>
- [
- SRCategory("CategoryAttributeAppearance"),
- Browsable(false),
- DefaultValue(typeof(Font), "Microsoft Sans Serif, 8pt"),
- ]
- override public Font Font
- {
- get
- {
- return base.Font;
- }
- set
- {
- base.Font = value;
- }
- }
- /// <summary>
- /// Not applicable to this annotation type.
- /// </summary>
- [
- SRCategory("CategoryAttributeAppearance"),
- Browsable(false),
- DefaultValue(typeof(Color), ""),
- NotifyParentPropertyAttribute(true),
- TypeConverter(typeof(ColorConverter)),
- #if DESIGNER
- Editor(typeof(ChartColorEditor), typeof(UITypeEditor))
- #endif
- ]
- override public Color BackColor
- {
- get
- {
- return base.BackColor;
- }
- set
- {
- base.BackColor = value;
- }
- }
- /// <summary>
- /// Not applicable to this annotation type.
- /// </summary>
- [
- SRCategory("CategoryAttributeAppearance"),
- Browsable(false),
- DefaultValue(ChartHatchStyle.None),
- NotifyParentPropertyAttribute(true),
- #if DESIGNER
- Editor(typeof(HatchStyleEditor), typeof(UITypeEditor))
- #endif
- ]
- override public ChartHatchStyle BackHatchStyle
- {
- get
- {
- return base.BackHatchStyle;
- }
- set
- {
- base.BackHatchStyle = value;
- }
- }
- /// <summary>
- /// Not applicable to this annotation type.
- /// </summary>
- [
- SRCategory("CategoryAttributeAppearance"),
- Browsable(false),
- DefaultValue(GradientStyle.None),
- NotifyParentPropertyAttribute(true),
- #if DESIGNER
- Editor(typeof(GradientEditor), typeof(UITypeEditor))
- #endif
- ]
- override public GradientStyle BackGradientStyle
- {
- get
- {
- return base.BackGradientStyle;
- }
- set
- {
- base.BackGradientStyle = value;
- }
- }
- /// <summary>
- /// Not applicable to this annotation type.
- /// </summary>
- [
- SRCategory("CategoryAttributeAppearance"),
- Browsable(false),
- DefaultValue(typeof(Color), ""),
- NotifyParentPropertyAttribute(true),
- TypeConverter(typeof(ColorConverter)),
- #if DESIGNER
- Editor(typeof(ChartColorEditor), typeof(UITypeEditor))
- #endif
- ]
- override public Color BackSecondaryColor
- {
- get
- {
- return base.BackSecondaryColor;
- }
- set
- {
- base.BackSecondaryColor = value;
- }
- }
- /// <summary>
- /// Not applicable to this annotation type.
- /// </summary>
- [
- SRCategory("CategoryAttributeAppearance"),
- Browsable(false),
- DefaultValue(typeof(Color), "Black"),
- TypeConverter(typeof(ColorConverter)),
- #if DESIGNER
- Editor(typeof(ChartColorEditor), typeof(UITypeEditor))
- #endif
- ]
- override public Color LineColor
- {
- get
- {
- return base.LineColor;
- }
- set
- {
- base.LineColor = value;
- }
- }
- /// <summary>
- /// Not applicable to this annotation type.
- /// </summary>
- [
- SRCategory("CategoryAttributeAppearance"),
- DefaultValue(1),
- Browsable(false),
- SRDescription("DescriptionAttributeLineWidth"),
- ]
- override public int LineWidth
- {
- get
- {
- return base.LineWidth;
- }
- set
- {
- base.LineWidth = value;
- }
- }
- /// <summary>
- /// Not applicable to this annotation type.
- /// </summary>
- [
- SRCategory("CategoryAttributeAppearance"),
- Browsable(false),
- DefaultValue(ChartDashStyle.Solid),
- SRDescription("DescriptionAttributeLineDashStyle"),
- ]
- override public ChartDashStyle LineDashStyle
- {
- get
- {
- return base.LineDashStyle;
- }
- set
- {
- base.LineDashStyle = value;
- }
- }
- #endregion
- #endregion
- #region Methods
- #region Painting
- /// <summary>
- /// Paints the annotation object on the specified graphics.
- /// </summary>
- /// <param name="graphics">
- /// A <see cref="ChartGraphics"/> object, used to paint the annotation object.
- /// </param>
- /// <param name="chart">
- /// Reference to the <see cref="Chart"/> owner control.
- /// </param>
- override internal void Paint(Chart chart, ChartGraphics graphics)
- {
- // Get annotation position in relative coordinates
- PointF firstPoint = PointF.Empty;
- PointF anchorPoint = PointF.Empty;
- SizeF size = SizeF.Empty;
- GetRelativePosition(out firstPoint, out size, out anchorPoint);
- PointF secondPoint = new PointF(firstPoint.X + size.Width, firstPoint.Y + size.Height);
- // Create selection rectangle
- RectangleF selectionRect = new RectangleF(firstPoint, new SizeF(secondPoint.X - firstPoint.X, secondPoint.Y - firstPoint.Y));
- // Get position
- RectangleF rectanglePosition = new RectangleF(selectionRect.Location, selectionRect.Size);
- if(rectanglePosition.Width < 0)
- {
- rectanglePosition.X = rectanglePosition.Right;
- rectanglePosition.Width = -rectanglePosition.Width;
- }
- if(rectanglePosition.Height < 0)
- {
- rectanglePosition.Y = rectanglePosition.Bottom;
- rectanglePosition.Height = -rectanglePosition.Height;
- }
- // Check if position is valid
- if( float.IsNaN(rectanglePosition.X) ||
- float.IsNaN(rectanglePosition.Y) ||
- float.IsNaN(rectanglePosition.Right) ||
- float.IsNaN(rectanglePosition.Bottom) )
- {
- return;
- }
- if(this.Common.ProcessModePaint)
- {
- // Draw "empty" image at design time
- if(this._imageName.Length == 0 && this.Chart.IsDesignMode() )
- {
- graphics.FillRectangleRel(
- rectanglePosition,
- this.BackColor,
- this.BackHatchStyle,
- this._imageName,
- this._imageWrapMode,
- this._imageTransparentColor,
- GetImageAlignment(this.Alignment),
- this.BackGradientStyle,
- this.BackSecondaryColor,
- this.LineColor,
- this.LineWidth,
- this.LineDashStyle,
- this.ShadowColor,
- this.ShadowOffset,
- PenAlignment.Center);
- // Draw text
- using( Brush textBrush = new SolidBrush(this.ForeColor) )
- {
- using (StringFormat format = new StringFormat(StringFormat.GenericTypographic))
- {
- format.Alignment = StringAlignment.Center;
- format.LineAlignment = StringAlignment.Center;
- format.FormatFlags = StringFormatFlags.LineLimit;
- format.Trimming = StringTrimming.EllipsisCharacter;
- graphics.DrawStringRel(
- "(no image)",
- this.Font,
- textBrush,
- rectanglePosition,
- format);
- }
- }
- }
- else
- {
- // Draw image
- graphics.FillRectangleRel(
- rectanglePosition,
- Color.Transparent,
- this.BackHatchStyle,
- this._imageName,
- this._imageWrapMode,
- this._imageTransparentColor,
- GetImageAlignment(this.Alignment),
- this.BackGradientStyle,
- Color.Transparent,
- Color.Transparent,
- 0,
- this.LineDashStyle,
- this.ShadowColor,
- this.ShadowOffset,
- PenAlignment.Center);
- }
- }
- if(this.Common.ProcessModeRegions)
- {
- // Add hot region
- this.Common.HotRegionsList.AddHotRegion(
- rectanglePosition,
- ReplaceKeywords(this.ToolTip),
- String.Empty,
- String.Empty,
- String.Empty,
- this,
- ChartElementType.Annotation,
- String.Empty);
- }
- // Paint selection handles
- PaintSelectionHandles(graphics, selectionRect, null);
- }
- /// <summary>
- /// Coverts ContentAlignment enumeration to ChartImageAlignmentStyle enumeration.
- /// </summary>
- /// <param name="alignment">Content alignment.</param>
- /// <returns>Image content alignment.</returns>
- private ChartImageAlignmentStyle GetImageAlignment(ContentAlignment alignment)
- {
- if(alignment == ContentAlignment.TopLeft)
- {
- return ChartImageAlignmentStyle.TopLeft;
- }
- else if(alignment == ContentAlignment.TopCenter)
- {
- return ChartImageAlignmentStyle.Top;
- }
- else if(alignment == ContentAlignment.TopRight)
- {
- return ChartImageAlignmentStyle.TopRight;
- }
- else if(alignment == ContentAlignment.MiddleRight)
- {
- return ChartImageAlignmentStyle.Right;
- }
- else if(alignment == ContentAlignment.BottomRight)
- {
- return ChartImageAlignmentStyle.BottomRight;
- }
- else if(alignment == ContentAlignment.BottomCenter)
- {
- return ChartImageAlignmentStyle.Bottom;
- }
- else if(alignment == ContentAlignment.BottomLeft)
- {
- return ChartImageAlignmentStyle.BottomLeft;
- }
- else if(alignment == ContentAlignment.MiddleLeft)
- {
- return ChartImageAlignmentStyle.Left;
- }
- return ChartImageAlignmentStyle.Center;
- }
- #endregion // Painting
- #region Content Size
- /// <summary>
- /// Gets text annotation content size based on the text and font.
- /// </summary>
- /// <returns>Annotation content position.</returns>
- override internal RectangleF GetContentPosition()
- {
- // Check image size
- if(this.Image.Length > 0)
- {
- // Try loading image and getting its size
- try
- {
- if(this.Chart != null)
- {
- ImageLoader imageLoader = this.Common.ImageLoader;
-
- if(imageLoader != null)
- {
- ChartGraphics chartGraphics = this.GetGraphics();
- if (chartGraphics != null)
- {
- SizeF absSize = new SizeF();
- if (imageLoader.GetAdjustedImageSize(this.Image, chartGraphics.Graphics, ref absSize))
- {
- SizeF imageSize = chartGraphics.GetRelativeSize(absSize);
- return new RectangleF(float.NaN, float.NaN, imageSize.Width, imageSize.Height);
- }
- }
- }
- }
- }
- catch(ArgumentException)
- {
- // ArgumentException is thrown by LoadImage in certain situations when it can't load the image
- }
- }
- return new RectangleF(float.NaN, float.NaN, float.NaN, float.NaN);
- }
- #endregion
- #endregion
- }
- }
|