RectangleAnnotation.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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: Rectangle, Ellipse and 3DBorder annotation classes.
  6. //
  7. using System;
  8. using System.ComponentModel;
  9. using System.Drawing;
  10. using System.Drawing.Design;
  11. using System.Drawing.Drawing2D;
  12. using FastReport.DataVisualization.Charting.Borders3D;
  13. using FastReport.DataVisualization.Charting.Utilities;
  14. namespace FastReport.DataVisualization.Charting
  15. {
  16. /// <summary>
  17. /// <b>RectangleAnnotation</b> is a class that represents a rectangle annotation.
  18. /// </summary>
  19. /// <remarks>
  20. /// A rectangle annotation can also display text inside the rectangle.
  21. /// </remarks>
  22. [
  23. SRDescription("DescriptionAttributeRectangleAnnotation_RectangleAnnotation"),
  24. ]
  25. public class RectangleAnnotation : TextAnnotation
  26. {
  27. #region Fields
  28. // Indicates that annotion rectangle should be drawn
  29. internal bool isRectVisible = true;
  30. #endregion
  31. #region Construction and Initialization
  32. /// <summary>
  33. /// Default public constructor.
  34. /// </summary>
  35. public RectangleAnnotation()
  36. : base()
  37. {
  38. }
  39. #endregion
  40. #region Properties
  41. #region Applicable Annotation Appearance Attributes (set as Browsable)
  42. /// <summary>
  43. /// Gets or sets the color of an annotation line.
  44. /// <seealso cref="LineWidth"/>
  45. /// <seealso cref="LineDashStyle"/>
  46. /// </summary>
  47. /// <value>
  48. /// A <see cref="Color"/> value used to draw an annotation line.
  49. /// </value>
  50. [
  51. SRCategory("CategoryAttributeAppearance"),
  52. Browsable(true),
  53. DefaultValue(typeof(Color), "Black"),
  54. SRDescription("DescriptionAttributeLineColor"),
  55. TypeConverter(typeof(ColorConverter)),
  56. #if DESIGNER
  57. Editor(typeof(ChartColorEditor), typeof(UITypeEditor))
  58. #endif
  59. ]
  60. override public Color LineColor
  61. {
  62. get
  63. {
  64. return base.LineColor;
  65. }
  66. set
  67. {
  68. base.LineColor = value;
  69. }
  70. }
  71. /// <summary>
  72. /// Gets or sets the width of an annotation line.
  73. /// <seealso cref="LineColor"/>
  74. /// <seealso cref="LineDashStyle"/>
  75. /// </summary>
  76. /// <value>
  77. /// An integer value defining the width of an annotation line in pixels.
  78. /// </value>
  79. [
  80. SRCategory("CategoryAttributeAppearance"),
  81. Browsable(true),
  82. DefaultValue(1),
  83. SRDescription("DescriptionAttributeLineWidth"),
  84. ]
  85. override public int LineWidth
  86. {
  87. get
  88. {
  89. return base.LineWidth;
  90. }
  91. set
  92. {
  93. base.LineWidth = value;
  94. }
  95. }
  96. /// <summary>
  97. /// Gets or sets the style of an annotation line.
  98. /// <seealso cref="LineWidth"/>
  99. /// <seealso cref="LineColor"/>
  100. /// </summary>
  101. /// <value>
  102. /// A <see cref="ChartDashStyle"/> value used to draw an annotation line.
  103. /// </value>
  104. [
  105. SRCategory("CategoryAttributeAppearance"),
  106. Browsable(true),
  107. DefaultValue(ChartDashStyle.Solid),
  108. SRDescription("DescriptionAttributeLineDashStyle"),
  109. ]
  110. override public ChartDashStyle LineDashStyle
  111. {
  112. get
  113. {
  114. return base.LineDashStyle;
  115. }
  116. set
  117. {
  118. base.LineDashStyle = value;
  119. }
  120. }
  121. /// <summary>
  122. /// Gets or sets the background color of an annotation.
  123. /// <seealso cref="BackSecondaryColor"/>
  124. /// <seealso cref="BackHatchStyle"/>
  125. /// <seealso cref="BackGradientStyle"/>
  126. /// </summary>
  127. /// <value>
  128. /// A <see cref="Color"/> value used for the background of an annotation.
  129. /// </value>
  130. [
  131. SRCategory("CategoryAttributeAppearance"),
  132. Browsable(true),
  133. DefaultValue(typeof(Color), ""),
  134. SRDescription("DescriptionAttributeBackColor"),
  135. NotifyParentPropertyAttribute(true),
  136. TypeConverter(typeof(ColorConverter)),
  137. #if DESIGNER
  138. Editor(typeof(ChartColorEditor), typeof(UITypeEditor))
  139. #endif
  140. ]
  141. override public Color BackColor
  142. {
  143. get
  144. {
  145. return base.BackColor;
  146. }
  147. set
  148. {
  149. base.BackColor = value;
  150. }
  151. }
  152. /// <summary>
  153. /// Gets or sets the background hatch style of an annotation.
  154. /// <seealso cref="BackSecondaryColor"/>
  155. /// <seealso cref="BackColor"/>
  156. /// <seealso cref="BackGradientStyle"/>
  157. /// </summary>
  158. /// <value>
  159. /// A <see cref="ChartHatchStyle"/> value used for the background of an annotation.
  160. /// </value>
  161. /// <remarks>
  162. /// Two colors are used to draw the hatching, <see cref="BackColor"/> and <see cref="BackSecondaryColor"/>.
  163. /// </remarks>
  164. [
  165. SRCategory("CategoryAttributeAppearance"),
  166. Browsable(true),
  167. DefaultValue(ChartHatchStyle.None),
  168. NotifyParentPropertyAttribute(true),
  169. SRDescription("DescriptionAttributeBackHatchStyle"),
  170. #if DESIGNER
  171. Editor(typeof(HatchStyleEditor), typeof(UITypeEditor))
  172. #endif
  173. ]
  174. override public ChartHatchStyle BackHatchStyle
  175. {
  176. get
  177. {
  178. return base.BackHatchStyle;
  179. }
  180. set
  181. {
  182. base.BackHatchStyle = value;
  183. }
  184. }
  185. /// <summary>
  186. /// Gets or sets the background gradient style of an annotation.
  187. /// <seealso cref="BackSecondaryColor"/>
  188. /// <seealso cref="BackColor"/>
  189. /// <seealso cref="BackHatchStyle"/>
  190. /// </summary>
  191. /// <value>
  192. /// A <see cref="GradientStyle"/> value used for the background of an annotation.
  193. /// </value>
  194. /// <remarks>
  195. /// Two colors are used to draw the gradient, <see cref="BackColor"/> and <see cref="BackSecondaryColor"/>.
  196. /// </remarks>
  197. [
  198. SRCategory("CategoryAttributeAppearance"),
  199. Browsable(true),
  200. DefaultValue(GradientStyle.None),
  201. NotifyParentPropertyAttribute(true),
  202. SRDescription("DescriptionAttributeBackGradientStyle"),
  203. #if DESIGNER
  204. Editor(typeof(GradientEditor), typeof(UITypeEditor))
  205. #endif
  206. ]
  207. override public GradientStyle BackGradientStyle
  208. {
  209. get
  210. {
  211. return base.BackGradientStyle;
  212. }
  213. set
  214. {
  215. base.BackGradientStyle = value;
  216. }
  217. }
  218. /// <summary>
  219. /// Gets or sets the secondary background color of an annotation.
  220. /// <seealso cref="BackColor"/>
  221. /// <seealso cref="BackHatchStyle"/>
  222. /// <seealso cref="BackGradientStyle"/>
  223. /// </summary>
  224. /// <value>
  225. /// A <see cref="Color"/> value used for the secondary color of an annotation background with
  226. /// hatching or gradient fill.
  227. /// </value>
  228. /// <remarks>
  229. /// This color is used with <see cref="BackColor"/> when <see cref="BackHatchStyle"/> or
  230. /// <see cref="BackGradientStyle"/> are used.
  231. /// </remarks>
  232. [
  233. SRCategory("CategoryAttributeAppearance"),
  234. Browsable(true),
  235. DefaultValue(typeof(Color), ""),
  236. NotifyParentPropertyAttribute(true),
  237. SRDescription("DescriptionAttributeBackSecondaryColor"),
  238. TypeConverter(typeof(ColorConverter)),
  239. #if DESIGNER
  240. Editor(typeof(ChartColorEditor), typeof(UITypeEditor))
  241. #endif
  242. ]
  243. override public Color BackSecondaryColor
  244. {
  245. get
  246. {
  247. return base.BackSecondaryColor;
  248. }
  249. set
  250. {
  251. base.BackSecondaryColor = value;
  252. }
  253. }
  254. #endregion
  255. #region Other
  256. /// <summary>
  257. /// Gets or sets an annotation's type name.
  258. /// </summary>
  259. /// <remarks>
  260. /// This property is used to get the name of each annotation type
  261. /// (e.g. Line, Rectangle, Ellipse).
  262. /// <para>
  263. /// This property is for internal use and is hidden at design and run time.
  264. /// </para>
  265. /// </remarks>
  266. [
  267. SRCategory("CategoryAttributeMisc"),
  268. Bindable(true),
  269. Browsable(false),
  270. EditorBrowsableAttribute(EditorBrowsableState.Never),
  271. DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden),
  272. SerializationVisibilityAttribute(SerializationVisibility.Hidden),
  273. SRDescription("DescriptionAttributeAnnotationType"),
  274. ]
  275. public override string AnnotationType
  276. {
  277. get
  278. {
  279. return "Rectangle";
  280. }
  281. }
  282. /// <summary>
  283. /// Gets or sets an annotation's selection points style.
  284. /// </summary>
  285. /// <value>
  286. /// A <see cref="SelectionPointsStyle"/> value that represents the annotation
  287. /// selection style.
  288. /// </value>
  289. /// <remarks>
  290. /// This property is for internal use and is hidden at design and run time.
  291. /// </remarks>
  292. [
  293. SRCategory("CategoryAttributeAppearance"),
  294. DefaultValue(SelectionPointsStyle.Rectangle),
  295. ParenthesizePropertyNameAttribute(true),
  296. Browsable(false),
  297. EditorBrowsableAttribute(EditorBrowsableState.Never),
  298. DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden),
  299. SerializationVisibilityAttribute(SerializationVisibility.Hidden),
  300. SRDescription("DescriptionAttributeSelectionPointsStyle"),
  301. ]
  302. override internal SelectionPointsStyle SelectionPointsStyle
  303. {
  304. get
  305. {
  306. return SelectionPointsStyle.Rectangle;
  307. }
  308. }
  309. #endregion
  310. #endregion
  311. #region Methods
  312. /// <summary>
  313. /// Paints an annotation object on the specified graphics.
  314. /// </summary>
  315. /// <param name="graphics">
  316. /// A <see cref="ChartGraphics"/> object, used to paint an annotation object.
  317. /// </param>
  318. /// <param name="chart">
  319. /// Reference to the <see cref="Chart"/> control.
  320. /// </param>
  321. override internal void Paint(Chart chart, ChartGraphics graphics)
  322. {
  323. // Get annotation position in relative coordinates
  324. PointF firstPoint = PointF.Empty;
  325. PointF anchorPoint = PointF.Empty;
  326. SizeF size = SizeF.Empty;
  327. GetRelativePosition(out firstPoint, out size, out anchorPoint);
  328. PointF secondPoint = new PointF(firstPoint.X + size.Width, firstPoint.Y + size.Height);
  329. // Create selection rectangle
  330. RectangleF selectionRect = new RectangleF(firstPoint, new SizeF(secondPoint.X - firstPoint.X, secondPoint.Y - firstPoint.Y));
  331. // Get text position
  332. RectangleF rectanglePosition = new RectangleF(selectionRect.Location, selectionRect.Size);
  333. if(rectanglePosition.Width < 0)
  334. {
  335. rectanglePosition.X = rectanglePosition.Right;
  336. rectanglePosition.Width = -rectanglePosition.Width;
  337. }
  338. if(rectanglePosition.Height < 0)
  339. {
  340. rectanglePosition.Y = rectanglePosition.Bottom;
  341. rectanglePosition.Height = -rectanglePosition.Height;
  342. }
  343. // Check if position is valid
  344. if( float.IsNaN(rectanglePosition.X) ||
  345. float.IsNaN(rectanglePosition.Y) ||
  346. float.IsNaN(rectanglePosition.Right) ||
  347. float.IsNaN(rectanglePosition.Bottom) )
  348. {
  349. return;
  350. }
  351. if(this.isRectVisible &&
  352. this.Common.ProcessModePaint)
  353. {
  354. // Draw rectangle
  355. graphics.FillRectangleRel(
  356. rectanglePosition,
  357. this.BackColor,
  358. this.BackHatchStyle,
  359. String.Empty,
  360. ChartImageWrapMode.Scaled,
  361. Color.Empty,
  362. ChartImageAlignmentStyle.Center,
  363. this.BackGradientStyle,
  364. this.BackSecondaryColor,
  365. this.LineColor,
  366. this.LineWidth,
  367. this.LineDashStyle,
  368. this.ShadowColor,
  369. this.ShadowOffset,
  370. PenAlignment.Center,
  371. this.isEllipse,
  372. 1,
  373. false);
  374. }
  375. // Call base class to paint text, selection handles and process hot regions
  376. base.Paint(chart, graphics);
  377. }
  378. #endregion
  379. }
  380. /// <summary>
  381. /// <b>EllipseAnnotation</b> is a class that represents an ellipse annotation.
  382. /// </summary>
  383. /// <remarks>
  384. /// An ellipse annotation can also display text inside the ellipse.
  385. /// </remarks>
  386. [
  387. SRDescription("DescriptionAttributeEllipseAnnotation_EllipseAnnotation"),
  388. ]
  389. public class EllipseAnnotation : RectangleAnnotation
  390. {
  391. #region Construction and Initialization
  392. /// <summary>
  393. /// Default public constructor.
  394. /// </summary>
  395. public EllipseAnnotation()
  396. : base()
  397. {
  398. this.isEllipse = true;
  399. }
  400. #endregion
  401. #region Properties
  402. /// <summary>
  403. /// Gets or sets an annotation's type name.
  404. /// </summary>
  405. /// <remarks>
  406. /// This property is used to get the name of each annotation type
  407. /// (e.g. Line, Rectangle, Ellipse).
  408. /// <para>
  409. /// This property is for internal use and is hidden at design and run time.
  410. /// </para>
  411. /// </remarks>
  412. [
  413. SRCategory("CategoryAttributeMisc"),
  414. Bindable(true),
  415. Browsable(false),
  416. EditorBrowsableAttribute(EditorBrowsableState.Never),
  417. DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden),
  418. SerializationVisibilityAttribute(SerializationVisibility.Hidden),
  419. SRDescription("DescriptionAttributeAnnotationType"),
  420. ]
  421. public override string AnnotationType
  422. {
  423. get
  424. {
  425. return "Ellipse";
  426. }
  427. }
  428. #endregion
  429. }
  430. /// <summary>
  431. /// <b>Border3DAnnotation</b> is a class that represents an annotation with a 3D border.
  432. /// </summary>
  433. /// <remarks>
  434. /// A Border3D annotation can also display inner text.
  435. /// </remarks>
  436. [
  437. SRDescription("DescriptionAttributeBorder3DAnnotation_Border3DAnnotation"),
  438. ]
  439. public class Border3DAnnotation : RectangleAnnotation
  440. {
  441. #region Fields
  442. // 3D border properties
  443. private BorderSkin _borderSkin;
  444. #endregion
  445. #region Construction and Initialization
  446. /// <summary>
  447. /// Default public constructor.
  448. /// </summary>
  449. public Border3DAnnotation()
  450. : base()
  451. {
  452. this.isRectVisible = false;
  453. this._borderSkin = new BorderSkin(this);
  454. this._borderSkin.PageColor = Color.Transparent;
  455. this._borderSkin.SkinStyle = BorderSkinStyle.Raised;
  456. // Change default appearance styles
  457. this.lineColor = Color.Empty;
  458. }
  459. #endregion
  460. #region Properties
  461. /// <summary>
  462. /// Gets or sets an annotation's type name.
  463. /// </summary>
  464. /// <remarks>
  465. /// This property is used to get the name of each annotation type
  466. /// (e.g. Line, Rectangle, Ellipse).
  467. /// <para>
  468. /// This property is for internal use and is hidden at design and run time.
  469. /// </para>
  470. /// </remarks>
  471. [
  472. SRCategory("CategoryAttributeMisc"),
  473. Bindable(true),
  474. Browsable(false),
  475. EditorBrowsableAttribute(EditorBrowsableState.Never),
  476. DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden),
  477. SerializationVisibilityAttribute(SerializationVisibility.Hidden),
  478. SRDescription("DescriptionAttributeAnnotationType"),
  479. ]
  480. public override string AnnotationType
  481. {
  482. get
  483. {
  484. return "Border3D";
  485. }
  486. }
  487. /// <summary>
  488. /// Gets or sets the skin style of the 3D border.
  489. /// </summary>
  490. /// <value>
  491. /// A <see cref="BorderSkin"/>
  492. /// </value>
  493. [
  494. SRCategory("CategoryAttributeAppearance"),
  495. Bindable(true),
  496. DefaultValue(null),
  497. SRDescription("DescriptionAttributeBorderSkin"),
  498. NotifyParentPropertyAttribute(true),
  499. TypeConverterAttribute(typeof(LegendConverter)),
  500. DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
  501. ]
  502. public BorderSkin BorderSkin
  503. {
  504. get
  505. {
  506. return _borderSkin;
  507. }
  508. set
  509. {
  510. _borderSkin = value;
  511. this.Invalidate();
  512. }
  513. }
  514. #endregion
  515. #region Methods
  516. /// <summary>
  517. /// Paints the annotation object on the specified graphics.
  518. /// </summary>
  519. /// <param name="graphics">
  520. /// A <see cref="ChartGraphics"/>
  521. /// </param>
  522. /// <param name="chart">
  523. /// Reference to the <see cref="Chart"/> control that owns the annotation.
  524. /// </param>
  525. override internal void Paint(Chart chart, ChartGraphics graphics)
  526. {
  527. // Get annotation position in relative coordinates
  528. PointF firstPoint = PointF.Empty;
  529. PointF anchorPoint = PointF.Empty;
  530. SizeF size = SizeF.Empty;
  531. GetRelativePosition(out firstPoint, out size, out anchorPoint);
  532. PointF secondPoint = new PointF(firstPoint.X + size.Width, firstPoint.Y + size.Height);
  533. // Create selection rectangle
  534. RectangleF selectionRect = new RectangleF(firstPoint, new SizeF(secondPoint.X - firstPoint.X, secondPoint.Y - firstPoint.Y));
  535. // Get text position
  536. RectangleF rectanglePosition = new RectangleF(selectionRect.Location, selectionRect.Size);
  537. if(rectanglePosition.Width < 0)
  538. {
  539. rectanglePosition.X = rectanglePosition.Right;
  540. rectanglePosition.Width = -rectanglePosition.Width;
  541. }
  542. if(rectanglePosition.Height < 0)
  543. {
  544. rectanglePosition.Y = rectanglePosition.Bottom;
  545. rectanglePosition.Height = -rectanglePosition.Height;
  546. }
  547. // Check if position is valid
  548. if( float.IsNaN(rectanglePosition.X) ||
  549. float.IsNaN(rectanglePosition.Y) ||
  550. float.IsNaN(rectanglePosition.Right) ||
  551. float.IsNaN(rectanglePosition.Bottom) )
  552. {
  553. return;
  554. }
  555. if(this.Common.ProcessModePaint)
  556. {
  557. // Do not draw border if size is less that 10 pixels
  558. RectangleF absRectanglePosition = graphics.GetAbsoluteRectangle(rectanglePosition);
  559. if(absRectanglePosition.Width > 30f &&
  560. absRectanglePosition.Height > 30f)
  561. {
  562. // Draw rectangle
  563. graphics.Draw3DBorderRel(
  564. _borderSkin,
  565. rectanglePosition,
  566. this.BackColor,
  567. this.BackHatchStyle,
  568. String.Empty,
  569. ChartImageWrapMode.Scaled,
  570. Color.Empty,
  571. ChartImageAlignmentStyle.Center,
  572. this.BackGradientStyle,
  573. this.BackSecondaryColor,
  574. this.LineColor,
  575. this.LineWidth,
  576. this.LineDashStyle);
  577. }
  578. }
  579. // Call base class to paint text, selection handles and process hot regions
  580. base.Paint(chart, graphics);
  581. }
  582. /// <summary>
  583. /// Gets text spacing on four different sides in relative coordinates.
  584. /// </summary>
  585. /// <param name="annotationRelative">Indicates that spacing is in annotation relative coordinates.</param>
  586. /// <returns>Rectangle with text spacing values.</returns>
  587. internal override RectangleF GetTextSpacing(out bool annotationRelative)
  588. {
  589. annotationRelative = false;
  590. RectangleF rect = new RectangleF(3f, 3f, 3f, 3f);
  591. if(GetGraphics() != null)
  592. {
  593. rect = GetGraphics().GetRelativeRectangle(rect);
  594. }
  595. if(_borderSkin.SkinStyle != BorderSkinStyle.None &&
  596. this.GetGraphics() != null &&
  597. this.Chart != null &&
  598. this.Chart.chartPicture != null &&
  599. this.Common != null)
  600. {
  601. IBorderType border3D = this.Common.BorderTypeRegistry.GetBorderType(_borderSkin.SkinStyle.ToString());
  602. if(border3D != null)
  603. {
  604. // Adjust are position to the border size
  605. RectangleF rectangle = new RectangleF(0f, 0f, 100f, 100f);
  606. border3D.AdjustAreasPosition(this.GetGraphics(), ref rectangle);
  607. rect = new RectangleF(
  608. rectangle.X + 1,
  609. rectangle.Y + 1,
  610. 100f - rectangle.Right + 2,
  611. 100f - rectangle.Bottom + 2);
  612. }
  613. }
  614. return rect;
  615. }
  616. #endregion
  617. }
  618. }