Axis.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /* The Axis module seeks to provide a simple facade to a lot of complex logic.
  2. *
  3. * Axes have many functions:
  4. * - Unit/Pixel conversions
  5. * - Configuring axis limits and boundaries
  6. * - Axis labels (XLabel, YLabel, Title, etc)
  7. * - Adding multiple axes
  8. * - Grid lines
  9. * - Tick marks
  10. * - Tick labels
  11. *
  12. */
  13. using ScottPlot.Drawing;
  14. using ScottPlot.Ticks;
  15. using System;
  16. using System.Drawing;
  17. namespace ScottPlot.Renderable
  18. {
  19. /// <summary>
  20. /// An Axis stores dimensions (axis limits and pixel/unit conversion methods) and can render
  21. /// itself including axis label, tick marks, tick labels, and grid lines
  22. /// </summary>
  23. public class Axis : IRenderable//刻度标记
  24. {
  25. /// <summary>
  26. /// Axis dimensions and methods for pixel/unit conversions
  27. /// </summary>
  28. public readonly AxisDimensions Dims = new AxisDimensions();
  29. /// <summary>
  30. /// Plottables with this axis index will use pixel/unit conversions from this axis
  31. /// </summary>
  32. public int AxisIndex = 0;
  33. //public int ChannelPosition = 0;
  34. public bool IsVisible { get; set; } = true;
  35. private Edge _Edge;
  36. public Edge Edge
  37. {
  38. get => _Edge;
  39. set
  40. {
  41. _Edge = value;
  42. AxisLine.Edge = value;
  43. AxisLabel.Edge = value;
  44. AxisTicks.Edge = value;
  45. bool isVertical = (value == Edge.Left || value == Edge.Right);
  46. AxisTicks.TickCollection.Orientation = isVertical ? AxisOrientation.Vertical : AxisOrientation.Horizontal;
  47. Dims.IsInverted = isVertical;
  48. }
  49. }
  50. public bool IsHorizontal => Edge == Edge.Top || Edge == Edge.Bottom;
  51. public bool IsVertical => Edge == Edge.Left || Edge == Edge.Right;
  52. public double[] TickPositionsMajor { get; set; }
  53. // private renderable components
  54. private readonly AxisLabel AxisLabel = new AxisLabel();
  55. private readonly AxisTicks AxisTicks = new AxisTicks();
  56. private readonly AxisLine AxisLine = new AxisLine();
  57. /// <summary>
  58. /// Define the size limits for this axis (in pixel units).
  59. /// </summary>
  60. public void SetSizeLimit(float? min = null, float? max = null, float? pad = null)
  61. {
  62. PixelSizeMinimum = min ?? PixelSizeMinimum;
  63. PixelSizeMaximum = max ?? PixelSizeMaximum;
  64. PixelSizePadding = pad ?? PixelSizePadding;
  65. }
  66. // private styling variables
  67. private float PixelSize; // how large this axis is
  68. private float PixelOffset; // distance from the data area
  69. private float PixelSizeMinimum = 5;
  70. private float PixelSizeMaximum = float.PositiveInfinity;
  71. private float PixelSizePadding = 3;
  72. /// <summary>
  73. /// Define how many pixels away from the data area this axis will be.
  74. /// TightenLayout() populates this value (based on other PixelSize values) to stack axes beside each other.
  75. /// </summary>
  76. public void SetOffset(float pixels) => PixelOffset = pixels;
  77. /// <summary>
  78. /// Define how large this axis is in pixels.
  79. /// RecalculateAxisSize() populates this value.
  80. /// </summary>
  81. public void SetSize(float pixels) => PixelSize = pixels;
  82. public float GetSize() => IsVisible ? PixelSize : 0;
  83. public override string ToString() => $"{Edge} axis from {Dims.Min} to {Dims.Max}";
  84. /// <summary>
  85. /// Use the latest configuration (size, font settings, axis limits) to determine tick mark positions
  86. /// </summary>
  87. public void RecalculateTickPositions(PlotDimensions dims) =>
  88. AxisTicks.TickCollection.Recalculate(dims, AxisTicks.TickLabelFont, Dims);
  89. /// <summary>
  90. /// Render all components of this axis onto the given Bitmap
  91. /// </summary>
  92. public void Render(PlotDimensions dims, Bitmap bmp, bool lowQuality = false)
  93. {
  94. if (IsVisible == false)
  95. return;
  96. //AxisLabel.PixelSizePadding = PixelSizePadding;
  97. AxisTicks.PixelOffset = PixelOffset;//标记的偏移
  98. //AxisLabel.PixelOffset = PixelOffset;
  99. //AxisLabel.PixelSize = PixelSize;//标记的大小
  100. AxisLine.PixelOffset = PixelOffset;
  101. using (var gfx = GDI.Graphics(bmp, dims, lowQuality, false))
  102. {
  103. AxisTicks.Render(dims, bmp, Dims.AxisColor, TickPositionsMajor, lowQuality);//绘制网格线和刻度
  104. //AxisLabel.Render(dims, bmp, lowQuality);
  105. AxisLine.Render(dims, bmp, lowQuality);//外框线
  106. }
  107. }
  108. /// <summary>
  109. /// DateTime format assumes axis represents DateTime.ToOATime() units and displays tick labels accordingly.
  110. /// </summary>
  111. public void DateTimeFormat(bool enable) => AxisTicks.TickCollection.LabelFormat =
  112. enable
  113. ? ScottPlot.Ticks.TickLabelFormat.DateTime
  114. : ScottPlot.Ticks.TickLabelFormat.Numeric;
  115. /// <summary>
  116. /// Configure the label of this axis
  117. /// </summary>
  118. public string Label(string label = null, Color? color = null, float? size = null, bool? bold = null, string fontName = null)
  119. {
  120. AxisLabel.IsVisible = true;
  121. AxisLabel.ImageLabel = null;
  122. AxisLabel.Label = label ?? AxisLabel.Label;
  123. AxisLabel.Font.Color = color ?? AxisLabel.Font.Color;
  124. AxisLabel.Font.Size = size ?? AxisLabel.Font.Size;
  125. AxisLabel.Font.Bold = bold ?? AxisLabel.Font.Bold;
  126. AxisLabel.Font.Name = fontName ?? AxisLabel.Font.Name;
  127. return AxisLabel.Label;
  128. }
  129. /// <summary>
  130. /// Display a custom image as the axis label instead of text
  131. /// </summary>
  132. /// <param name="img">The image to display where the label should go</param>
  133. /// <param name="padInside">pixels of padding between the inner image edge and the data area</param>
  134. /// <param name="padOutside">pixels of padding between the outer image edge and the figure edge</param>
  135. public void ImageLabel(Bitmap img, float padInside = 5, float padOutside = 5)
  136. {
  137. IsVisible = true;
  138. AxisLabel.ImageLabel = img;
  139. AxisLabel.ImagePaddingToDataArea = padInside;
  140. AxisLabel.ImagePaddingToFigureEdge = padOutside;
  141. }
  142. /// <summary>
  143. /// Set color of every component of this axis (label, line, tick marks, and tick labels)
  144. /// </summary>
  145. public void Color(Color color)
  146. {
  147. Label(color: color);
  148. TickLabelStyle(color: color);
  149. AxisTicks.MajorTickColor = color;
  150. AxisTicks.MinorTickColor = color;
  151. AxisLine.Color = color;
  152. }
  153. /// <summary>
  154. /// Use a custom function to generate tick label strings
  155. /// </summary>
  156. public void TickLabelFormat(Func<double, string> tickFormatter)
  157. {
  158. AxisTicks.TickCollection.ManualTickFormatter = tickFormatter;
  159. }
  160. /// <summary>
  161. /// Manually define the string format to use for translating tick positions to tick labels
  162. /// </summary>
  163. public void TickLabelFormat(string format, bool dateTimeFormat)
  164. {
  165. if (dateTimeFormat)
  166. {
  167. AxisTicks.TickCollection.dateTimeFormatString = format;
  168. DateTimeFormat(true);
  169. }
  170. else
  171. {
  172. AxisTicks.TickCollection.numericFormatString = format;
  173. DateTimeFormat(false);
  174. }
  175. }
  176. /// <summary>
  177. /// Customize string settings for the tick labels
  178. /// </summary>
  179. public void TickLabelNotation(
  180. bool? multiplier = null,
  181. bool? offset = null,
  182. bool? exponential = null,
  183. bool? invertSign = null,
  184. int? radix = null,
  185. string prefix = null)
  186. {
  187. AxisTicks.TickCollection.useMultiplierNotation = multiplier ?? AxisTicks.TickCollection.useMultiplierNotation;
  188. AxisTicks.TickCollection.useOffsetNotation = offset ?? AxisTicks.TickCollection.useOffsetNotation;
  189. AxisTicks.TickCollection.useExponentialNotation = exponential ?? AxisTicks.TickCollection.useExponentialNotation;
  190. AxisTicks.TickCollection.LabelUsingInvertedSign = invertSign ?? AxisTicks.TickCollection.LabelUsingInvertedSign;
  191. AxisTicks.TickCollection.radix = radix ?? AxisTicks.TickCollection.radix;
  192. AxisTicks.TickCollection.prefix = prefix ?? AxisTicks.TickCollection.prefix;
  193. }
  194. /// <summary>
  195. /// Define a manual spacing between major ticks (and major grid lines)
  196. /// </summary>
  197. public void ManualTickSpacing(double manualSpacing)
  198. {
  199. // TODO: cutt X and Y out of this
  200. AxisTicks.TickCollection.manualSpacingX = manualSpacing;
  201. AxisTicks.TickCollection.manualSpacingY = manualSpacing;
  202. }
  203. /// <summary>
  204. /// Define a manual spacing between major ticks (and major grid lines) for axes configured to display using DateTime format
  205. /// </summary>
  206. public void ManualTickSpacing(double manualSpacing, DateTimeUnit manualSpacingDateTimeUnit)
  207. {
  208. ManualTickSpacing(manualSpacing);
  209. AxisTicks.TickCollection.manualDateTimeSpacingUnitX = manualSpacingDateTimeUnit;
  210. }
  211. /// <summary>
  212. /// Manually define major tick (and grid) positions and labels
  213. /// </summary>
  214. public void ManualTickPositions(double[] positions, string[] labels)
  215. {
  216. AxisTicks.TickCollection.manualTickPositions = positions;
  217. AxisTicks.TickCollection.manualTickLabels = labels;
  218. }
  219. /// <summary>
  220. /// Ruler mode draws long tick marks and offsets tick labels for a ruler appearance
  221. /// </summary>
  222. public void RulerMode(bool enable) => AxisTicks.RulerMode = enable;
  223. /// <summary>
  224. /// Enable this to snap major ticks (and grid lines) to the nearest pixel to avoid anti-aliasing artifacts
  225. /// </summary>
  226. /// <param name="enable"></param>
  227. public void PixelSnap(bool enable) => AxisTicks.SnapPx = enable;
  228. /// <summary>
  229. /// Set style of the tick mark lines
  230. /// </summary>
  231. public void TickMarkColor(Color color)
  232. {
  233. AxisTicks.MajorTickColor = color;
  234. AxisTicks.MinorTickColor = color;
  235. }
  236. /// <summary>
  237. /// Set the culture to use for unit-to-string tick mark conversion
  238. /// </summary>
  239. public void SetCulture(System.Globalization.CultureInfo culture) => AxisTicks.TickCollection.Culture = culture;
  240. /// <summary>
  241. /// Manually define culture to use for unit-to-string tick mark conversion
  242. /// </summary>
  243. public void SetCulture(
  244. string shortDatePattern = null, string decimalSeparator = null, string numberGroupSeparator = null,
  245. int? decimalDigits = null, int? numberNegativePattern = null, int[] numberGroupSizes = null) =>
  246. AxisTicks.TickCollection.SetCulture(shortDatePattern, decimalSeparator, numberGroupSeparator,
  247. decimalDigits, numberNegativePattern, numberGroupSizes);
  248. /// <summary>
  249. /// Customize styling of the tick labels
  250. /// </summary>
  251. public void TickLabelStyle(
  252. Color? color = null,
  253. string fontName = null,
  254. float? fontSize = null,
  255. bool? fontBold = null,
  256. float? rotation = null)
  257. {
  258. AxisTicks.TickLabelFont.Color = color ?? AxisTicks.TickLabelFont.Color;
  259. AxisTicks.TickLabelFont.Name = fontName ?? AxisTicks.TickLabelFont.Name;
  260. AxisTicks.TickLabelFont.Size = fontSize ?? AxisTicks.TickLabelFont.Size;
  261. AxisTicks.TickLabelFont.Bold = fontBold ?? AxisTicks.TickLabelFont.Bold;
  262. AxisTicks.TickLabelRotation = rotation ?? AxisTicks.TickLabelRotation;
  263. }
  264. /// <summary>
  265. /// Set visibility of all ticks
  266. /// </summary>
  267. public void Ticks(bool enable)
  268. {
  269. AxisTicks.MajorTickVisible = enable;
  270. AxisTicks.TickLabelVisible = enable;
  271. AxisTicks.MinorTickVisible = enable;
  272. }
  273. /// <summary>
  274. /// Set visibility of individual tick components
  275. /// </summary>
  276. public void Ticks(bool major, bool minor = true, bool majorLabels = true)
  277. {
  278. AxisTicks.MajorTickVisible = major;
  279. AxisTicks.TickLabelVisible = major && majorLabels;
  280. AxisTicks.MinorTickVisible = minor;
  281. }
  282. /// <summary>
  283. /// This value defines the packing density of tick labels.
  284. /// A density of 1.0 means labels fit tightly based on measured maximum label size.
  285. /// Higher densities place more ticks but tick labels may oberlap.
  286. /// </summary>
  287. public void TickDensity(double ratio = 1.0)
  288. {
  289. AxisTicks.TickCollection.TickDensity = (float)ratio;
  290. }
  291. /// <summary>
  292. /// Define the smallest distance between major ticks, grid lines, and tick labels in coordinate units.
  293. /// This only works for numeric tick systems (DateTime ticks are not supported).
  294. /// </summary>
  295. public void MinimumTickSpacing(double spacing)
  296. {
  297. AxisTicks.TickCollection.MinimumTickSpacing = spacing;
  298. }
  299. /// <summary>
  300. /// Sets whether minor ticks are evenly spaced or log-distributed between major tick positions
  301. /// </summary>
  302. public void MinorLogScale(bool enable)
  303. {
  304. AxisTicks.TickCollection.MinorTickDistribution =enable ? MinorTickDistribution.EvenAndLog : MinorTickDistribution.even;
  305. Dims.MinorTickDistribution = AxisTicks.TickCollection.MinorTickDistribution;
  306. }
  307. public MinorTickDistribution GetMinorLogScale() => AxisTicks.TickCollection.MinorTickDistribution;
  308. /// <summary>
  309. /// Configure the line drawn along the edge of the axis
  310. /// </summary>
  311. public void Line(bool? visible = null, Color? color = null, float? width = null)
  312. {
  313. AxisLine.IsVisible = visible ?? AxisLine.IsVisible;
  314. AxisLine.Color = color ?? AxisLine.Color;
  315. AxisLine.Width = width ?? AxisLine.Width;
  316. }
  317. /// <summary>
  318. /// Set the minimum size and padding of the axis
  319. /// </summary>
  320. public void Layout(float? padding = null, float? minimumSize = null, float? maximumSize = null)
  321. {
  322. PixelSizePadding = padding ?? PixelSizePadding;
  323. PixelSizeMinimum = minimumSize ?? PixelSizeMinimum;
  324. PixelSizeMaximum = maximumSize ?? PixelSizeMaximum;
  325. }
  326. /// <summary>
  327. /// Configure visibility and styling of the major grid
  328. /// </summary>
  329. public void MajorGrid(
  330. bool? enable = null,
  331. Color? color = null,
  332. float? lineWidth = null,
  333. LineStyle? lineStyle = null)
  334. {
  335. AxisTicks.MajorGridVisible = enable ?? AxisTicks.MajorGridVisible;
  336. AxisTicks.MajorGridColor = color ?? AxisTicks.MajorGridColor;
  337. AxisTicks.MajorGridWidth = lineWidth ?? AxisTicks.MajorGridWidth;
  338. AxisTicks.MajorGridStyle = lineStyle ?? AxisTicks.MajorGridStyle;
  339. }
  340. /// <summary>
  341. /// Configure visibility and styling of the minor grid
  342. /// </summary>
  343. public void MinorGrid(
  344. bool? enable = null,
  345. Color? color = null,
  346. float? lineWidth = null,
  347. LineStyle? lineStyle = null,
  348. bool? logScale = null)
  349. {
  350. AxisTicks.MinorGridVisible = enable ?? AxisTicks.MinorGridVisible;
  351. AxisTicks.MinorGridColor = color ?? AxisTicks.MinorGridColor;
  352. AxisTicks.MinorGridWidth = lineWidth ?? AxisTicks.MinorGridWidth;
  353. AxisTicks.MinorGridStyle = lineStyle ?? AxisTicks.MinorGridStyle;
  354. if (logScale.HasValue)
  355. AxisTicks.TickCollection.MinorTickDistribution = logScale.Value
  356. ? MinorTickDistribution.log
  357. : MinorTickDistribution.even;
  358. }
  359. public void MinorTick(Color? color = null)
  360. {
  361. AxisTicks.MinorTickColor = color ?? AxisTicks.MinorTickColor;
  362. }
  363. /// <summary>
  364. /// Disable all visibility and set size to 0px
  365. /// </summary>
  366. public void Hide()
  367. {
  368. AxisLine.IsVisible = false;
  369. AxisTicks.MajorTickVisible = false;
  370. AxisTicks.MinorTickVisible = false;
  371. PixelSizeMinimum = 0;
  372. PixelSizeMaximum = 0;
  373. PixelSizePadding = 0;
  374. }
  375. /// <summary>
  376. /// 小刻度开关
  377. /// </summary>
  378. /// <param name="isShow"></param>
  379. public void ConfigMinorTick(Boolean isShow)
  380. {
  381. AxisTicks.MinorTickVisible = isShow;
  382. }
  383. /// <summary>
  384. /// Set visibility for major tick grid lines
  385. /// </summary>
  386. public void Grid(bool enable) => AxisTicks.MajorGridVisible = enable;
  387. public double[] GetTickPositionsMajor()
  388. {
  389. return AxisTicks.TickCollection.tickPositionsMajor;
  390. }
  391. /// <summary>
  392. /// Set pixel size based on the latest axis label, tick marks, and tick label
  393. /// </summary>
  394. public void RecalculateAxisSize()
  395. {
  396. using (var tickFont = GDI.Font(AxisTicks.TickLabelFont))
  397. using (var titleFont = GDI.Font(AxisLabel.Font))
  398. {
  399. PixelSize = 0;
  400. if (AxisLabel.IsVisible)
  401. PixelSize += AxisLabel.Measure().Height;
  402. if (AxisTicks.TickLabelVisible)
  403. {
  404. // determine how many pixels the largest tick label occupies
  405. float maxHeight = AxisTicks.TickCollection.LargestLabelHeight;
  406. float maxWidth = AxisTicks.TickCollection.LargestLabelWidth * 1.2f;
  407. // calculate the width and height of the rotated label
  408. float largerEdgeLength = Math.Max(maxWidth, maxHeight);
  409. float shorterEdgeLength = Math.Min(maxWidth, maxHeight);
  410. float differenceInEdgeLengths = largerEdgeLength - shorterEdgeLength;
  411. double radians = AxisTicks.TickLabelRotation * Math.PI / 180;
  412. double fraction = IsHorizontal ? Math.Sin(radians) : Math.Cos(radians);
  413. double rotatedSize = shorterEdgeLength + differenceInEdgeLengths * fraction;
  414. // add the rotated label size to the size of this axis
  415. PixelSize += (float)rotatedSize;
  416. }
  417. if (AxisTicks.MajorTickVisible)
  418. PixelSize += AxisTicks.MajorTickLength;
  419. PixelSize = Math.Max(PixelSize, PixelSizeMinimum);
  420. PixelSize = Math.Min(PixelSize, PixelSizeMaximum);
  421. PixelSize += PixelSizePadding;
  422. }
  423. }
  424. }
  425. }