ShapePoint.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.ComponentModel;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Text;
  6. using FastReport.Utils;
  7. using System.Drawing;
  8. using System.Drawing.Drawing2D;
  9. using System.IO;
  10. namespace FastReport.Map
  11. {
  12. /// <summary>
  13. /// Represents a map point.
  14. /// </summary>
  15. public partial class ShapePoint : ShapeBase
  16. {
  17. #region Fields
  18. private PointD point;
  19. private RectangleF largestBoundsRect;
  20. #endregion // Fields
  21. #region Properties
  22. /// <summary>
  23. /// Gets or sets the X-coordinate of the point.
  24. /// </summary>
  25. public double X
  26. {
  27. get { return point.X; }
  28. set { point.X = value; }
  29. }
  30. /// <summary>
  31. /// Gets or sets the Y-coordinate of the point.
  32. /// </summary>
  33. public double Y
  34. {
  35. get { return point.Y; }
  36. set { point.Y = value; }
  37. }
  38. #endregion // Properties
  39. #region Private Methods
  40. private PointF GetPoint()
  41. {
  42. float left = Map.AbsLeft + Map.Padding.Left + Map.OffsetXG;
  43. float top = Map.AbsTop + Map.Padding.Top + Map.OffsetYG;
  44. double minX = Layer.Box.MinX;
  45. double maxY = Layer.Box.MaxY;
  46. float scale = Map.ScaleG;
  47. return new PointF(
  48. left + CoordinateConverter.GetX(point.X + ShapeOffsetX, minX, scale),
  49. top + CoordinateConverter.GetY(point.Y + ShapeOffsetY, maxY, scale, Map.MercatorProjection));
  50. }
  51. #endregion
  52. #region Public Methods
  53. /// <inheritdoc/>
  54. public override void Assign(Base source)
  55. {
  56. base.Assign(source);
  57. ShapePoint src = source as ShapePoint;
  58. X = src.X;
  59. Y = src.Y;
  60. }
  61. /// <inheritdoc/>
  62. public override void Draw(FRPaintEventArgs e)
  63. {
  64. PointF point = GetPoint();
  65. point.X *= e.ScaleX;
  66. point.Y *= e.ScaleY;
  67. ShapeStyle style = UseCustomStyle ? CustomStyle : Layer.DefaultShapeStyle;
  68. float size = style.PointSize;
  69. Pen pen = e.Cache.GetPen(style.BorderColor, 1, DashStyle.Solid);
  70. Brush brush = e.Cache.GetBrush(style.FillColor);
  71. // get color & size from the layer's color & size ranges
  72. if (!IsValueEmpty)
  73. {
  74. brush = e.Cache.GetBrush(Layer.ColorRanges.GetColor(Value));
  75. float rangeSize = Layer.SizeRanges.GetSize(Value);
  76. if (rangeSize != 0)
  77. size = rangeSize;
  78. }
  79. DrawDesign(e, ref brush, ref size);
  80. if (Map.Zoom >= Layer.LabelsVisibleAtZoom)
  81. {
  82. e.Graphics.FillAndDrawEllipse(pen, brush, point.X - size / 2, point.Y - size / 2, size, size);
  83. }
  84. largestBoundsRect = new RectangleF(point.X - 100, point.Y - 20, 200, 20);
  85. }
  86. /// <inheritdoc/>
  87. public override void DrawLabel(FRPaintEventArgs e)
  88. {
  89. ShapeStyle style = UseCustomStyle ? CustomStyle : Layer.DefaultShapeStyle;
  90. string text = "";
  91. if (Layer.LabelKind != MapLabelKind.Value)
  92. text = SpatialData.GetValue(Layer.LabelColumn);
  93. if (!IsValueEmpty)
  94. {
  95. if (Layer.LabelKind == MapLabelKind.NameAndValue || Layer.LabelKind == MapLabelKind.Value)
  96. text += "\r\n" + Value.ToString(Layer.LabelFormat);
  97. }
  98. Font font = e.Cache.GetFont(style.Font.FontFamily,
  99. IsPrinting ? style.Font.Size : style.Font.Size * e.ScaleX * 96f / DrawUtils.ScreenDpi,
  100. style.Font.Style);
  101. RectangleF textBounds = largestBoundsRect;
  102. textBounds.Offset(CenterOffsetX * e.ScaleX, CenterOffsetY * e.ScaleY);
  103. StringFormat format = e.Cache.GetStringFormat(StringAlignment.Center, StringAlignment.Center, StringTrimming.None, StringFormatFlags.NoClip, 0, 0);
  104. SolidBrush brush = e.Cache.GetBrush(style.TextColor);
  105. e.Graphics.DrawString(text, font, brush, textBounds, format);
  106. }
  107. internal void Load(Stream stream)
  108. {
  109. point.Load(stream);
  110. }
  111. internal void Save(Stream stream)
  112. {
  113. point.Save(stream);
  114. }
  115. /// <inheritdoc/>
  116. public override void Serialize(FRWriter writer)
  117. {
  118. ShapePoint c = writer.DiffObject as ShapePoint;
  119. base.Serialize(writer);
  120. if (X != c.X)
  121. writer.WriteDouble("X", X);
  122. if (Y != c.Y)
  123. writer.WriteDouble("Y", Y);
  124. }
  125. /// <inheritdoc/>
  126. public override bool HitTest(PointF point)
  127. {
  128. PointF pt = GetPoint();
  129. RectangleF bounds = new RectangleF(pt.X - 16, pt.Y - 16, 16, 16);
  130. if (bounds.Contains(point))
  131. return true;
  132. return false;
  133. }
  134. #endregion // Public Methods
  135. /// <summary>
  136. /// Initializes a new instance of the <see cref="ShapePoint"/> class.
  137. /// </summary>
  138. public ShapePoint()
  139. {
  140. point = new PointD();
  141. BaseName = "Point";
  142. }
  143. }
  144. }