ArrowAnnotation.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. //
  5. // Purpose: Arrow annotation classes.
  6. //
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Specialized;
  10. using System.ComponentModel;
  11. using System.ComponentModel.Design;
  12. using System.Data;
  13. using System.Drawing;
  14. using System.Drawing.Design;
  15. using System.Drawing.Text;
  16. using System.Drawing.Drawing2D;
  17. using FastReport.DataVisualization.Charting;
  18. using FastReport.DataVisualization.Charting.Data;
  19. using FastReport.DataVisualization.Charting.ChartTypes;
  20. using FastReport.DataVisualization.Charting.Utilities;
  21. using FastReport.DataVisualization.Charting.Borders3D;
  22. namespace FastReport.DataVisualization.Charting
  23. {
  24. #region Enumeration
  25. /// <summary>
  26. /// Arrow annotation styles.
  27. /// <seealso cref="ArrowAnnotation.ArrowStyle"/>
  28. /// </summary>
  29. [
  30. SRDescription("DescriptionAttributeArrowStyle_ArrowStyle")
  31. ]
  32. public enum ArrowStyle
  33. {
  34. /// <summary>
  35. /// Simple arrow.
  36. /// </summary>
  37. Simple,
  38. /// <summary>
  39. /// Arrow pointing in two directions.
  40. /// </summary>
  41. DoubleArrow,
  42. /// <summary>
  43. /// Arrow with a tail.
  44. /// </summary>
  45. Tailed,
  46. }
  47. #endregion // Enumeration
  48. /// <summary>
  49. /// <b>ArrowAnnotation</b> is a class class that represents an arrow annotation.
  50. /// </summary>
  51. /// <remarks>
  52. /// Arrow annotations can be used to connect to points on the chart or highlight a
  53. /// single chart area. Different arrow styles and sizes may be applied.
  54. /// </remarks>
  55. [
  56. SRDescription("DescriptionAttributeArrowAnnotation_ArrowAnnotation"),
  57. ]
  58. public class ArrowAnnotation : Annotation
  59. {
  60. #region Fields
  61. // Annotation arrow style
  62. private ArrowStyle _arrowStyle = ArrowStyle.Simple;
  63. // Annotation arrow size
  64. private int _arrowSize = 5;
  65. #endregion
  66. #region Construction and Initialization
  67. /// <summary>
  68. /// Default public constructor.
  69. /// </summary>
  70. public ArrowAnnotation()
  71. : base()
  72. {
  73. base.AnchorAlignment = ContentAlignment.TopLeft;
  74. }
  75. #endregion
  76. #region Properties
  77. #region Arrow properties
  78. /// <summary>
  79. /// Gets or sets the arrow style of an arrow annotation.
  80. /// <seealso cref="ArrowSize"/>
  81. /// </summary>
  82. /// <value>
  83. /// <see cref="ArrowStyle"/> of an annotation.
  84. /// </value>
  85. [
  86. SRCategory("CategoryAttributeAppearance"),
  87. Bindable(true),
  88. DefaultValue(ArrowStyle.Simple),
  89. SRDescription("DescriptionAttributeArrowAnnotation_ArrowStyle"),
  90. ParenthesizePropertyNameAttribute(true),
  91. ]
  92. virtual public ArrowStyle ArrowStyle
  93. {
  94. get
  95. {
  96. return _arrowStyle;
  97. }
  98. set
  99. {
  100. _arrowStyle = value;
  101. Invalidate();
  102. }
  103. }
  104. /// <summary>
  105. /// Gets or sets the arrow size in pixels of an arrow annotation.
  106. /// <seealso cref="ArrowStyle"/>
  107. /// </summary>
  108. /// <value>
  109. /// Integer value that represents arrow size (thickness) in pixels.
  110. /// </value>
  111. [
  112. SRCategory("CategoryAttributeAppearance"),
  113. Bindable(true),
  114. DefaultValue(5),
  115. SRDescription("DescriptionAttributeArrowAnnotation_ArrowSize"),
  116. ParenthesizePropertyNameAttribute(true),
  117. ]
  118. virtual public int ArrowSize
  119. {
  120. get
  121. {
  122. return _arrowSize;
  123. }
  124. set
  125. {
  126. if(value <= 0)
  127. {
  128. throw(new ArgumentOutOfRangeException("value", SR.ExceptionAnnotationArrowSizeIsZero));
  129. }
  130. if(value > 100)
  131. {
  132. throw (new ArgumentOutOfRangeException("value", SR.ExceptionAnnotationArrowSizeMustBeLessThen100));
  133. }
  134. _arrowSize = value;
  135. Invalidate();
  136. }
  137. }
  138. #endregion // Arrow properties
  139. #region Anchor
  140. /// <summary>
  141. /// Gets or sets an annotation position's alignment to the anchor point.
  142. /// <seealso cref="Annotation.AnchorX"/>
  143. /// <seealso cref="Annotation.AnchorY"/>
  144. /// <seealso cref="Annotation.AnchorDataPoint"/>
  145. /// <seealso cref="Annotation.AnchorOffsetX"/>
  146. /// <seealso cref="Annotation.AnchorOffsetY"/>
  147. /// </summary>
  148. /// <value>
  149. /// A <see cref="ContentAlignment"/> value that represents the annotation's alignment to
  150. /// the anchor point.
  151. /// </value>
  152. /// <remarks>
  153. /// The annotation must be anchored using either <see cref="Annotation.AnchorDataPoint"/>, or the <see cref="Annotation.AnchorX"/>
  154. /// and <see cref="Annotation.AnchorY"/> properties. Its <see cref="Annotation.X"/> and <see cref="Annotation.Y"/>
  155. /// properties must be set to <b>Double.NaN</b>.
  156. /// </remarks>
  157. [
  158. SRCategory("CategoryAttributeAnchor"),
  159. Browsable(false),
  160. EditorBrowsableAttribute(EditorBrowsableState.Never),
  161. DefaultValue(typeof(ContentAlignment), "TopLeft"),
  162. SRDescription("DescriptionAttributeAnchorAlignment"),
  163. ]
  164. override public ContentAlignment AnchorAlignment
  165. {
  166. get
  167. {
  168. return base.AnchorAlignment;
  169. }
  170. set
  171. {
  172. base.AnchorAlignment = value;
  173. }
  174. }
  175. #endregion // Anchoring
  176. #region Other
  177. /// <summary>
  178. /// Gets or sets an annotation's type name.
  179. /// </summary>
  180. /// <remarks>
  181. /// This property is used to get the name of each annotation type
  182. /// (e.g. Line, Rectangle, Ellipse).
  183. /// <para>
  184. /// This property is for internal use and is hidden at design and run time.
  185. /// </para>
  186. /// </remarks>
  187. [
  188. SRCategory("CategoryAttributeMisc"),
  189. Bindable(true),
  190. Browsable(false),
  191. EditorBrowsableAttribute(EditorBrowsableState.Never),
  192. DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden),
  193. SerializationVisibilityAttribute(SerializationVisibility.Hidden),
  194. SRDescription("DescriptionAttributeAnnotationType"),
  195. ]
  196. public override string AnnotationType
  197. {
  198. get
  199. {
  200. return "Arrow";
  201. }
  202. }
  203. /// <summary>
  204. /// Gets or sets annotation selection points style.
  205. /// </summary>
  206. /// <value>
  207. /// A <see cref="SelectionPointsStyle"/> value that represents annotation
  208. /// selection style.
  209. /// </value>
  210. /// <remarks>
  211. /// This property is for internal use and is hidden at design and run time.
  212. /// </remarks>
  213. [
  214. SRCategory("CategoryAttributeAppearance"),
  215. DefaultValue(SelectionPointsStyle.Rectangle),
  216. ParenthesizePropertyNameAttribute(true),
  217. Browsable(false),
  218. EditorBrowsableAttribute(EditorBrowsableState.Never),
  219. DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden),
  220. SerializationVisibilityAttribute(SerializationVisibility.Hidden),
  221. SRDescription("DescriptionAttributeSelectionPointsStyle"),
  222. ]
  223. override internal SelectionPointsStyle SelectionPointsStyle
  224. {
  225. get
  226. {
  227. return SelectionPointsStyle.TwoPoints;
  228. }
  229. }
  230. #endregion
  231. #endregion
  232. #region Methods
  233. /// <summary>
  234. /// Paints annotation object on specified graphics.
  235. /// </summary>
  236. /// <param name="graphics">
  237. /// A <see cref="ChartGraphics"/> used to paint annotation object.
  238. /// </param>
  239. /// <param name="chart">
  240. /// Reference to the <see cref="Chart"/> control.
  241. /// </param>
  242. override internal void Paint(Chart chart, ChartGraphics graphics)
  243. {
  244. // Get annotation position in relative coordinates
  245. PointF firstPoint = PointF.Empty;
  246. PointF anchorPoint = PointF.Empty;
  247. SizeF size = SizeF.Empty;
  248. GetRelativePosition(out firstPoint, out size, out anchorPoint);
  249. PointF secondPoint = new PointF(firstPoint.X + size.Width, firstPoint.Y + size.Height);
  250. // Create selection rectangle
  251. RectangleF selectionRect = new RectangleF(firstPoint, new SizeF(secondPoint.X - firstPoint.X, secondPoint.Y - firstPoint.Y));
  252. // Check if text position is valid
  253. if( float.IsNaN(firstPoint.X) ||
  254. float.IsNaN(firstPoint.Y) ||
  255. float.IsNaN(secondPoint.X) ||
  256. float.IsNaN(secondPoint.Y) )
  257. {
  258. return;
  259. }
  260. // Get arrow shape path
  261. using (GraphicsPath arrowPathAbs = GetArrowPath(graphics, selectionRect))
  262. {
  263. // Draw arrow shape
  264. if (this.Common.ProcessModePaint)
  265. {
  266. graphics.DrawPathAbs(
  267. arrowPathAbs,
  268. (this.BackColor.IsEmpty) ? Color.White : this.BackColor,
  269. this.BackHatchStyle,
  270. String.Empty,
  271. ChartImageWrapMode.Scaled,
  272. Color.Empty,
  273. ChartImageAlignmentStyle.Center,
  274. this.BackGradientStyle,
  275. this.BackSecondaryColor,
  276. this.LineColor,
  277. this.LineWidth,
  278. this.LineDashStyle,
  279. PenAlignment.Center,
  280. this.ShadowOffset,
  281. this.ShadowColor);
  282. }
  283. // Process hot region
  284. if (this.Common.ProcessModeRegions)
  285. {
  286. // Use callout defined hot region
  287. this.Common.HotRegionsList.AddHotRegion(
  288. graphics,
  289. arrowPathAbs,
  290. false,
  291. ReplaceKeywords(this.ToolTip),
  292. String.Empty,
  293. String.Empty,
  294. String.Empty,
  295. this,
  296. ChartElementType.Annotation);
  297. }
  298. // Paint selection handles
  299. PaintSelectionHandles(graphics, selectionRect, null);
  300. }
  301. }
  302. /// <summary>
  303. /// Get arrow path for the specified annotation position
  304. /// </summary>
  305. /// <param name="graphics"></param>
  306. /// <param name="position"></param>
  307. /// <returns></returns>
  308. private GraphicsPath GetArrowPath(
  309. ChartGraphics graphics,
  310. RectangleF position)
  311. {
  312. // Get absolute position
  313. RectangleF positionAbs = graphics.GetAbsoluteRectangle(position);
  314. PointF firstPoint = positionAbs.Location;
  315. PointF secondPoint = new PointF(positionAbs.Right, positionAbs.Bottom);
  316. // Calculate arrow length
  317. float deltaX = secondPoint.X - firstPoint.X;
  318. float deltaY = secondPoint.Y - firstPoint.Y;
  319. float arrowLength = (float)Math.Sqrt(deltaX * deltaX + deltaY * deltaY);
  320. // Create unrotated graphics path for the arrow started at the annotation location
  321. // and going to the right for the length of the rotated arrow.
  322. GraphicsPath path = new GraphicsPath();
  323. PointF[] points = null;
  324. float pointerRatio = 2.1f;
  325. if(this.ArrowStyle == ArrowStyle.Simple)
  326. {
  327. points = new PointF[] {
  328. firstPoint,
  329. new PointF(firstPoint.X + this.ArrowSize*pointerRatio, firstPoint.Y - this.ArrowSize*pointerRatio),
  330. new PointF(firstPoint.X + this.ArrowSize*pointerRatio, firstPoint.Y - this.ArrowSize),
  331. new PointF(firstPoint.X + arrowLength, firstPoint.Y - this.ArrowSize),
  332. new PointF(firstPoint.X + arrowLength, firstPoint.Y + this.ArrowSize),
  333. new PointF(firstPoint.X + this.ArrowSize*pointerRatio, firstPoint.Y + this.ArrowSize),
  334. new PointF(firstPoint.X + this.ArrowSize*pointerRatio, firstPoint.Y + this.ArrowSize*pointerRatio) };
  335. }
  336. else if(this.ArrowStyle == ArrowStyle.DoubleArrow)
  337. {
  338. points = new PointF[] {
  339. firstPoint,
  340. new PointF(firstPoint.X + this.ArrowSize*pointerRatio, firstPoint.Y - this.ArrowSize*pointerRatio),
  341. new PointF(firstPoint.X + this.ArrowSize*pointerRatio, firstPoint.Y - this.ArrowSize),
  342. new PointF(firstPoint.X + arrowLength - this.ArrowSize*pointerRatio, firstPoint.Y - this.ArrowSize),
  343. new PointF(firstPoint.X + arrowLength - this.ArrowSize*pointerRatio, firstPoint.Y - this.ArrowSize*pointerRatio),
  344. new PointF(firstPoint.X + arrowLength, firstPoint.Y),
  345. new PointF(firstPoint.X + arrowLength - this.ArrowSize*pointerRatio, firstPoint.Y + this.ArrowSize*pointerRatio),
  346. new PointF(firstPoint.X + arrowLength - this.ArrowSize*pointerRatio, firstPoint.Y + this.ArrowSize),
  347. new PointF(firstPoint.X + this.ArrowSize*pointerRatio, firstPoint.Y + this.ArrowSize),
  348. new PointF(firstPoint.X + this.ArrowSize*pointerRatio, firstPoint.Y + this.ArrowSize*pointerRatio) };
  349. }
  350. else if(this.ArrowStyle == ArrowStyle.Tailed)
  351. {
  352. float tailRatio = 2.1f;
  353. points = new PointF[] {
  354. firstPoint,
  355. new PointF(firstPoint.X + this.ArrowSize*pointerRatio, firstPoint.Y - this.ArrowSize*pointerRatio),
  356. new PointF(firstPoint.X + this.ArrowSize*pointerRatio, firstPoint.Y - this.ArrowSize),
  357. new PointF(firstPoint.X + arrowLength, firstPoint.Y - this.ArrowSize*tailRatio),
  358. new PointF(firstPoint.X + arrowLength - this.ArrowSize*tailRatio, firstPoint.Y),
  359. new PointF(firstPoint.X + arrowLength, firstPoint.Y + this.ArrowSize*tailRatio),
  360. new PointF(firstPoint.X + this.ArrowSize*pointerRatio, firstPoint.Y + this.ArrowSize),
  361. new PointF(firstPoint.X + this.ArrowSize*pointerRatio, firstPoint.Y + this.ArrowSize*pointerRatio) };
  362. }
  363. else
  364. {
  365. throw (new InvalidOperationException(SR.ExceptionAnnotationArrowStyleUnknown));
  366. }
  367. path.AddLines(points);
  368. path.CloseAllFigures();
  369. // Calculate arrow angle
  370. float angle = (float)(Math.Atan(deltaY / deltaX) * 180f / Math.PI);
  371. if(deltaX < 0)
  372. {
  373. angle += 180f;
  374. }
  375. // Rotate arrow path around the first point
  376. using( Matrix matrix = new Matrix() )
  377. {
  378. matrix.RotateAt(angle, firstPoint);
  379. path.Transform(matrix);
  380. }
  381. return path;
  382. }
  383. #endregion
  384. }
  385. }