AxisScaleSegments.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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. using System;
  5. using System.Collections;
  6. using System.ComponentModel;
  7. using System.Drawing;
  8. using System.Drawing.Drawing2D;
  9. using FastReport.DataVisualization.Charting.Utilities;
  10. namespace FastReport.DataVisualization.Charting
  11. {
  12. /// <summary>
  13. /// <b>AxisScaleSegment</b> class represents a single segment of the axis with
  14. /// it's own scale and intervals.
  15. /// </summary>
  16. [
  17. SRDescription("DescriptionAttributeAxisScaleSegment_AxisScaleSegment"),
  18. ]
  19. internal class AxisScaleSegment
  20. {
  21. #region Fields
  22. // Associated axis
  23. internal Axis axis = null;
  24. // Axis segment position in percent of the axis size
  25. private double _position = 0.0;
  26. // Axis segment size in percent of the axis size
  27. private double _size = 0.0;
  28. // Axis segment spacing in percent of the axis size
  29. private double _spacing = 0.0;
  30. // Axis segment scale minimum value
  31. private double _scaleMinimum = 0.0;
  32. // Axis segment scale maximum value
  33. private double _scaleMaximum = 0.0;
  34. // Axis segment interval offset.
  35. private double _intervalOffset = 0;
  36. // Axis segment interval.
  37. private double _interval = 0;
  38. // Axis segment interval units type.
  39. private DateTimeIntervalType _intervalType = DateTimeIntervalType.Auto;
  40. // Axis segment interval offset units type.
  41. private DateTimeIntervalType _intervalOffsetType = DateTimeIntervalType.Auto;
  42. // Object associated with the segment
  43. private object _tag = null;
  44. // Stack used to save/load axis settings
  45. private Stack _oldAxisSettings = new Stack();
  46. #endregion // Fields
  47. #region Constructor
  48. /// <summary>
  49. /// Default object constructor.
  50. /// </summary>
  51. public AxisScaleSegment()
  52. {
  53. }
  54. #endregion // Constructor
  55. #region Properties
  56. /// <summary>
  57. /// Axis segment position in axis size percentage.
  58. /// </summary>
  59. [
  60. SRCategory("CategoryAttributeMisc"),
  61. DefaultValue(0.0),
  62. SRDescription("DescriptionAttributeAxisScaleSegment_Position"),
  63. ]
  64. public double Position
  65. {
  66. get
  67. {
  68. return this._position;
  69. }
  70. set
  71. {
  72. if(value < 0.0 || value > 100.0)
  73. {
  74. throw (new ArgumentOutOfRangeException("value", SR.ExceptionAxisScaleSegmentsPositionInvalid));
  75. }
  76. this._position = value;
  77. }
  78. }
  79. /// <summary>
  80. /// Axis segment size in axis size percentage.
  81. /// </summary>
  82. [
  83. SRCategory("CategoryAttributeMisc"),
  84. DefaultValue(0.0),
  85. SRDescription("DescriptionAttributeAxisScaleSegment_Size"),
  86. ]
  87. public double Size
  88. {
  89. get
  90. {
  91. return this._size;
  92. }
  93. set
  94. {
  95. if(value < 0.0 || value > 100.0)
  96. {
  97. throw (new ArgumentOutOfRangeException("value", SR.ExceptionAxisScaleSegmentsSizeInvalid));
  98. }
  99. this._size = value;
  100. }
  101. }
  102. /// <summary>
  103. /// Axis segment spacing in axis size percentage.
  104. /// </summary>
  105. [
  106. SRCategory("CategoryAttributeMisc"),
  107. DefaultValue(0.0),
  108. SRDescription("DescriptionAttributeAxisScaleSegment_Spacing"),
  109. ]
  110. public double Spacing
  111. {
  112. get
  113. {
  114. return this._spacing;
  115. }
  116. set
  117. {
  118. if(value < 0.0 || value > 100.0)
  119. {
  120. throw (new ArgumentOutOfRangeException("value", SR.ExceptionAxisScaleSegmentsSpacingInvalid));
  121. }
  122. this._spacing = value;
  123. }
  124. }
  125. /// <summary>
  126. /// Axis segment scale maximum value.
  127. /// </summary>
  128. [
  129. SRCategory("CategoryAttributeMisc"),
  130. DefaultValue(0.0),
  131. SRDescription("DescriptionAttributeAxisScaleSegment_ScaleMaximum"),
  132. ]
  133. public double ScaleMaximum
  134. {
  135. get
  136. {
  137. return this._scaleMaximum;
  138. }
  139. set
  140. {
  141. this._scaleMaximum = value;
  142. }
  143. }
  144. /// <summary>
  145. /// Axis segment scale minimum value.
  146. /// </summary>
  147. [
  148. SRCategory("CategoryAttributeMisc"),
  149. DefaultValue(0.0),
  150. SRDescription("DescriptionAttributeAxisScaleSegment_ScaleMinimum"),
  151. ]
  152. public double ScaleMinimum
  153. {
  154. get
  155. {
  156. return this._scaleMinimum;
  157. }
  158. set
  159. {
  160. this._scaleMinimum = value;
  161. }
  162. }
  163. /// <summary>
  164. /// Axis segment interval size.
  165. /// </summary>
  166. [
  167. SRCategory("CategoryAttributeInterval"),
  168. DefaultValue(0.0),
  169. SRDescription("DescriptionAttributeAxisScaleSegment_Interval"),
  170. TypeConverter(typeof(AxisIntervalValueConverter)),
  171. ]
  172. public double Interval
  173. {
  174. get
  175. {
  176. return this._interval;
  177. }
  178. set
  179. {
  180. // Axis interval properties must be set
  181. if(double.IsNaN(value))
  182. {
  183. this._interval = 0;
  184. }
  185. else
  186. {
  187. this._interval = value;
  188. }
  189. }
  190. }
  191. /// <summary>
  192. /// Axis segment interval offset.
  193. /// </summary>
  194. [
  195. SRCategory("CategoryAttributeInterval"),
  196. DefaultValue(0.0),
  197. SRDescription("DescriptionAttributeAxisScaleSegment_IntervalOffset"),
  198. TypeConverter(typeof(AxisIntervalValueConverter))
  199. ]
  200. public double IntervalOffset
  201. {
  202. get
  203. {
  204. return _intervalOffset;
  205. }
  206. }
  207. /// <summary>
  208. /// Axis segment interval type.
  209. /// </summary>
  210. [
  211. SRCategory("CategoryAttributeInterval"),
  212. DefaultValue(DateTimeIntervalType.Auto),
  213. SRDescription("DescriptionAttributeAxisScaleSegment_IntervalType"),
  214. ]
  215. public DateTimeIntervalType IntervalType
  216. {
  217. get
  218. {
  219. return this._intervalType;
  220. }
  221. set
  222. {
  223. // Axis interval properties must be set
  224. if(value == DateTimeIntervalType.NotSet)
  225. {
  226. this._intervalType = DateTimeIntervalType.Auto;
  227. }
  228. else
  229. {
  230. _intervalType = value;
  231. }
  232. }
  233. }
  234. /// <summary>
  235. /// Axis segment interval offset type.
  236. /// </summary>
  237. [
  238. SRCategory("CategoryAttributeInterval"),
  239. DefaultValue(DateTimeIntervalType.Auto),
  240. SRDescription("DescriptionAttributeAxisScaleSegment_IntervalOffsetType"),
  241. ]
  242. public DateTimeIntervalType IntervalOffsetType
  243. {
  244. get
  245. {
  246. return this._intervalOffsetType;
  247. }
  248. }
  249. /// <summary>
  250. /// Object associated with axis scale segment.
  251. /// </summary>
  252. [
  253. SRCategory("CategoryAttributeMisc"),
  254. Browsable(false),
  255. DefaultValue(null),
  256. SRDescription("DescriptionAttributeAxisScaleSegment_Tag"),
  257. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
  258. SerializationVisibility(SerializationVisibility.Hidden),
  259. ]
  260. public object Tag
  261. {
  262. get
  263. {
  264. return this._tag;
  265. }
  266. set
  267. {
  268. this._tag = value;
  269. }
  270. }
  271. #endregion // Properties
  272. #region Break Line Painting Methods
  273. /// <summary>
  274. /// Paints the axis break line.
  275. /// </summary>
  276. /// <param name="graph">Chart graphics to use.</param>
  277. /// <param name="nextSegment">Axis scale segment next to current.</param>
  278. internal void PaintBreakLine(ChartGraphics graph, AxisScaleSegment nextSegment)
  279. {
  280. // Get break line position
  281. RectangleF breakPosition = this.GetBreakLinePosition(graph, nextSegment);
  282. // Get top line graphics path
  283. GraphicsPath breakLinePathTop = this.GetBreakLinePath(breakPosition, true);
  284. GraphicsPath breakLinePathBottom = null;
  285. // Clear break line space using chart color behind the area
  286. if(breakPosition.Width > 0f && breakPosition.Height > 0f)
  287. {
  288. // Get bottom line graphics path
  289. breakLinePathBottom = this.GetBreakLinePath(breakPosition, false);
  290. // Clear plotting area background
  291. using(GraphicsPath fillPath = new GraphicsPath())
  292. {
  293. // Create fill path out of top and bottom break lines
  294. fillPath.AddPath(breakLinePathTop, true);
  295. fillPath.Reverse();
  296. fillPath.AddPath(breakLinePathBottom, true);
  297. fillPath.CloseAllFigures();
  298. // Use chart back color to fill the area
  299. using(Brush fillBrush = this.GetChartFillBrush(graph))
  300. {
  301. graph.FillPath(fillBrush, fillPath);
  302. // Check if shadow exsits in chart area
  303. if( this.axis.ChartArea.ShadowOffset != 0 && !this.axis.ChartArea.ShadowColor.IsEmpty)
  304. {
  305. // Clear shadow
  306. RectangleF shadowPartRect = breakPosition;
  307. if( this.axis.AxisPosition == AxisPosition.Right || this.axis.AxisPosition == AxisPosition.Left )
  308. {
  309. shadowPartRect.Y += this.axis.ChartArea.ShadowOffset;
  310. shadowPartRect.Height -= this.axis.ChartArea.ShadowOffset;
  311. shadowPartRect.X = shadowPartRect.Right - 1;
  312. shadowPartRect.Width = this.axis.ChartArea.ShadowOffset + 2;
  313. }
  314. else
  315. {
  316. shadowPartRect.X += this.axis.ChartArea.ShadowOffset;
  317. shadowPartRect.Width -= this.axis.ChartArea.ShadowOffset;
  318. shadowPartRect.Y = shadowPartRect.Bottom - 1;
  319. shadowPartRect.Height = this.axis.ChartArea.ShadowOffset + 2;
  320. }
  321. graph.FillRectangle(fillBrush, shadowPartRect);
  322. // Draw new shadow
  323. using(GraphicsPath shadowPath = new GraphicsPath())
  324. {
  325. shadowPath.AddPath(breakLinePathTop, false);
  326. // Define maximum size
  327. float size = this.axis.ChartArea.ShadowOffset;
  328. if( this.axis.AxisPosition == AxisPosition.Right || this.axis.AxisPosition == AxisPosition.Left )
  329. {
  330. size = Math.Min(size, breakPosition.Height);
  331. }
  332. else
  333. {
  334. size = Math.Min(size, breakPosition.Width);
  335. }
  336. // Define step to increase transperancy
  337. int transparencyStep = (int)(this.axis.ChartArea.ShadowColor.A / size);
  338. // Set clip region to achieve spacing of the shadow
  339. // Start with the plotting rectangle position
  340. RectangleF clipRegion = graph.GetAbsoluteRectangle(this.axis.PlotAreaPosition.ToRectangleF());
  341. if( this.axis.AxisPosition == AxisPosition.Right || this.axis.AxisPosition == AxisPosition.Left )
  342. {
  343. clipRegion.X += this.axis.ChartArea.ShadowOffset;
  344. clipRegion.Width += this.axis.ChartArea.ShadowOffset;
  345. }
  346. else
  347. {
  348. clipRegion.Y += this.axis.ChartArea.ShadowOffset;
  349. clipRegion.Height += this.axis.ChartArea.ShadowOffset;
  350. }
  351. graph.SetClip(graph.GetRelativeRectangle(clipRegion));
  352. // Draw several lines to form shadow
  353. for(int index = 0; index < size; index ++)
  354. {
  355. using(Matrix newMatrix = new Matrix())
  356. {
  357. // Shift top break line by 1 pixel
  358. if( this.axis.AxisPosition == AxisPosition.Right || this.axis.AxisPosition == AxisPosition.Left )
  359. {
  360. newMatrix.Translate(0f, 1f);
  361. }
  362. else
  363. {
  364. newMatrix.Translate(1f, 0f);
  365. }
  366. shadowPath.Transform(newMatrix);
  367. }
  368. // Get line color
  369. Color color = Color.FromArgb(
  370. this.axis.ChartArea.ShadowColor.A - transparencyStep * index,
  371. this.axis.ChartArea.ShadowColor);
  372. using(Pen shadowPen = new Pen(color, 1))
  373. {
  374. // Draw shadow
  375. graph.DrawPath(shadowPen, shadowPath);
  376. }
  377. }
  378. graph.ResetClip();
  379. }
  380. }
  381. }
  382. }
  383. }
  384. // Draw Separator Line(s)
  385. if(this.axis.ScaleBreakStyle.BreakLineStyle != BreakLineStyle.None)
  386. {
  387. using(Pen pen = new Pen(this.axis.ScaleBreakStyle.LineColor, this.axis.ScaleBreakStyle.LineWidth))
  388. {
  389. // Set line style
  390. pen.DashStyle = graph.GetPenStyle(this.axis.ScaleBreakStyle.LineDashStyle);
  391. // Draw break lines
  392. graph.DrawPath(pen, breakLinePathTop);
  393. if(breakPosition.Width > 0f && breakPosition.Height > 0f)
  394. {
  395. graph.DrawPath(pen, breakLinePathBottom);
  396. }
  397. }
  398. }
  399. // Dispose break line paths
  400. breakLinePathTop.Dispose();
  401. breakLinePathTop = null;
  402. if(breakLinePathBottom != null)
  403. {
  404. breakLinePathBottom.Dispose();
  405. breakLinePathBottom = null;
  406. }
  407. }
  408. /// <summary>
  409. /// Get fill brush used to fill axis break lines spacing.
  410. /// </summary>
  411. /// <param name="graph">chart graphics.</param>
  412. /// <returns>Fill brush.</returns>
  413. private Brush GetChartFillBrush(ChartGraphics graph)
  414. {
  415. Chart chart = this.axis.ChartArea.Common.Chart;
  416. Brush brush = null;
  417. if( chart.BackGradientStyle == GradientStyle.None )
  418. {
  419. brush = new SolidBrush(chart.BackColor);
  420. }
  421. else
  422. {
  423. // If a gradient type is set create a brush with gradient
  424. brush = graph.GetGradientBrush(new RectangleF(0, 0, chart.chartPicture.Width - 1, chart.chartPicture.Height - 1), chart.BackColor, chart.BackSecondaryColor, chart.BackGradientStyle);
  425. }
  426. if( chart.BackHatchStyle != ChartHatchStyle.None )
  427. {
  428. brush = graph.GetHatchBrush( chart.BackHatchStyle, chart.BackColor, chart.BackSecondaryColor );
  429. }
  430. if( chart.BackImage.Length > 0 &&
  431. chart.BackImageWrapMode != ChartImageWrapMode.Unscaled &&
  432. chart.BackImageWrapMode != ChartImageWrapMode.Scaled)
  433. {
  434. brush = graph.GetTextureBrush(chart.BackImage, chart.BackImageTransparentColor, chart.BackImageWrapMode, chart.BackColor );
  435. }
  436. return brush;
  437. }
  438. /// <summary>
  439. /// Gets a path of the break line for specified position.
  440. /// </summary>
  441. /// <param name="breakLinePosition">Break line position.</param>
  442. /// <param name="top">Indicates if top or bottom break line path should be retrieved.</param>
  443. /// <returns>Graphics path with break line path.</returns>
  444. private GraphicsPath GetBreakLinePath(RectangleF breakLinePosition, bool top)
  445. {
  446. GraphicsPath path = new GraphicsPath();
  447. if(this.axis.ScaleBreakStyle.BreakLineStyle == BreakLineStyle.Wave)
  448. {
  449. PointF[] points = null;
  450. int pointNumber = 0;
  451. if( this.axis.AxisPosition == AxisPosition.Right || this.axis.AxisPosition == AxisPosition.Left )
  452. {
  453. float startX = breakLinePosition.X;
  454. float endX = breakLinePosition.Right;
  455. float y = (top) ? breakLinePosition.Y : breakLinePosition.Bottom;
  456. pointNumber = ((int)(endX - startX) / 40) * 2 ;
  457. if(pointNumber < 2)
  458. {
  459. pointNumber = 2;
  460. }
  461. float step = (endX - startX) / pointNumber;
  462. points = new PointF[pointNumber + 1];
  463. for(int pointIndex = 1; pointIndex < pointNumber + 1; pointIndex++)
  464. {
  465. points[pointIndex] = new PointF(
  466. startX + pointIndex * step,
  467. y + ((pointIndex%2 == 0) ? -2f : 2f) );
  468. }
  469. points[0] = new PointF(startX, y);
  470. points[points.Length - 1] = new PointF(endX, y);
  471. }
  472. else
  473. {
  474. float startY = breakLinePosition.Y;
  475. float endY = breakLinePosition.Bottom;
  476. float x = (top) ? breakLinePosition.X : breakLinePosition.Right;
  477. pointNumber = ((int)(endY - startY) / 40) * 2 ;
  478. if(pointNumber < 2)
  479. {
  480. pointNumber = 2;
  481. }
  482. float step = (endY - startY) / pointNumber;
  483. points = new PointF[pointNumber + 1];
  484. for(int pointIndex = 1; pointIndex < pointNumber + 1; pointIndex++)
  485. {
  486. points[pointIndex] = new PointF(
  487. x + ((pointIndex%2 == 0) ? -2f : 2f),
  488. startY + pointIndex * step);
  489. }
  490. points[0] = new PointF(x, startY);
  491. points[points.Length - 1] = new PointF(x, endY);
  492. }
  493. path.AddCurve(points, 0, pointNumber, 0.8f);
  494. }
  495. else if(this.axis.ScaleBreakStyle.BreakLineStyle == BreakLineStyle.Ragged)
  496. {
  497. PointF[] points = null;
  498. Random rand = new Random(435657);
  499. if( this.axis.AxisPosition == AxisPosition.Right || this.axis.AxisPosition == AxisPosition.Left )
  500. {
  501. float startX = breakLinePosition.X;
  502. float endX = breakLinePosition.Right;
  503. float y = (top) ? breakLinePosition.Y : breakLinePosition.Bottom;
  504. float step = 10f;
  505. int pointNumber = (int)((endX - startX) / step);
  506. if(pointNumber < 2)
  507. {
  508. pointNumber = 2;
  509. }
  510. points = new PointF[pointNumber];
  511. for(int pointIndex = 1; pointIndex < pointNumber - 1; pointIndex++)
  512. {
  513. points[pointIndex] = new PointF(
  514. startX + pointIndex * step,
  515. y + rand.Next(-3, 3) );
  516. }
  517. points[0] = new PointF(startX, y);
  518. points[points.Length - 1] = new PointF(endX, y);
  519. }
  520. else
  521. {
  522. float startY = breakLinePosition.Y;
  523. float endY = breakLinePosition.Bottom;
  524. float x = (top) ? breakLinePosition.X : breakLinePosition.Right;
  525. float step = 10f;
  526. int pointNumber = (int)((endY - startY) / step);
  527. if(pointNumber < 2)
  528. {
  529. pointNumber = 2;
  530. }
  531. points = new PointF[pointNumber];
  532. for(int pointIndex = 1; pointIndex < pointNumber - 1; pointIndex++)
  533. {
  534. points[pointIndex] = new PointF(
  535. x + rand.Next(-3, 3),
  536. startY + pointIndex * step );
  537. }
  538. points[0] = new PointF(x, startY);
  539. points[points.Length - 1] = new PointF(x, endY);
  540. }
  541. path.AddLines(points);
  542. }
  543. else
  544. {
  545. if( this.axis.AxisPosition == AxisPosition.Right || this.axis.AxisPosition == AxisPosition.Left )
  546. {
  547. if(top)
  548. {
  549. path.AddLine(breakLinePosition.X, breakLinePosition.Y, breakLinePosition.Right, breakLinePosition.Y);
  550. }
  551. else
  552. {
  553. path.AddLine(breakLinePosition.X, breakLinePosition.Bottom, breakLinePosition.Right, breakLinePosition.Bottom);
  554. }
  555. }
  556. else
  557. {
  558. if(top)
  559. {
  560. path.AddLine(breakLinePosition.X, breakLinePosition.Y, breakLinePosition.X, breakLinePosition.Bottom);
  561. }
  562. else
  563. {
  564. path.AddLine(breakLinePosition.Right, breakLinePosition.Y, breakLinePosition.Right, breakLinePosition.Bottom);
  565. }
  566. }
  567. }
  568. return path;
  569. }
  570. /// <summary>
  571. /// Gets position of the axis break line. Break line may be shown as a single
  572. /// line or two lines separated with a spacing.
  573. /// </summary>
  574. /// <param name="graph">Chart graphics.</param>
  575. /// <param name="nextSegment">Next segment reference.</param>
  576. /// <returns>Position of the axis break line in pixel coordinates.</returns>
  577. internal RectangleF GetBreakLinePosition(ChartGraphics graph, AxisScaleSegment nextSegment)
  578. {
  579. // Start with the plotting rectangle position
  580. RectangleF breakPosition = this.axis.PlotAreaPosition.ToRectangleF();
  581. // Find maximum scale value of the current segment and minimuj of the next
  582. double from = this.axis.GetLinearPosition(nextSegment.ScaleMinimum);
  583. double to = this.axis.GetLinearPosition(this.ScaleMaximum);
  584. if( this.axis.AxisPosition == AxisPosition.Right || this.axis.AxisPosition == AxisPosition.Left )
  585. {
  586. breakPosition.Y = (float)Math.Min(from, to);
  587. breakPosition.Height = (float)Math.Max(from, to);
  588. }
  589. else
  590. {
  591. breakPosition.X = (float)Math.Min(from, to);
  592. breakPosition.Width = (float)Math.Max(from, to);;
  593. }
  594. // Convert to pixels
  595. breakPosition = Rectangle.Round(graph.GetAbsoluteRectangle(breakPosition));
  596. // Add border width
  597. if( this.axis.AxisPosition == AxisPosition.Right || this.axis.AxisPosition == AxisPosition.Left )
  598. {
  599. breakPosition.Height = (float)Math.Abs(breakPosition.Y - breakPosition.Height);
  600. breakPosition.X -= this.axis.ChartArea.BorderWidth;
  601. breakPosition.Width += 2 * this.axis.ChartArea.BorderWidth;
  602. }
  603. else
  604. {
  605. breakPosition.Width = (float)Math.Abs(breakPosition.X - breakPosition.Width);
  606. breakPosition.Y -= this.axis.ChartArea.BorderWidth;
  607. breakPosition.Height += 2 * this.axis.ChartArea.BorderWidth;
  608. }
  609. return breakPosition;
  610. }
  611. #endregion // Break Line Painting Methods
  612. #region Helper Methods
  613. /// <summary>
  614. /// Gets segment scale position and size in relative coordinates.
  615. /// Method takes in consideration segment spacing and space required fro separatorType.
  616. /// </summary>
  617. /// <param name="plotAreaSize">Plotting area size in relative coordinates.</param>
  618. /// <param name="scalePosition">Returns scale position.</param>
  619. /// <param name="scaleSize">Returns scale size.</param>
  620. internal void GetScalePositionAndSize(double plotAreaSize, out double scalePosition, out double scaleSize)
  621. {
  622. scaleSize = (this.Size - this.Spacing) * (plotAreaSize / 100.0);
  623. scalePosition = this.Position * (plotAreaSize / 100.0);
  624. }
  625. /// <summary>
  626. /// Saves axis settings and set them from the current segment.
  627. /// </summary>
  628. internal void SetTempAxisScaleAndInterval()
  629. {
  630. // Save current axis settings
  631. if(_oldAxisSettings.Count == 0)
  632. {
  633. _oldAxisSettings.Push(this.axis.maximum);
  634. _oldAxisSettings.Push(this.axis.minimum);
  635. _oldAxisSettings.Push(this.axis.majorGrid.interval);
  636. _oldAxisSettings.Push(this.axis.majorGrid.intervalType);
  637. _oldAxisSettings.Push(this.axis.majorGrid.intervalOffset);
  638. _oldAxisSettings.Push(this.axis.majorGrid.intervalOffsetType);
  639. _oldAxisSettings.Push(this.axis.majorTickMark.interval);
  640. _oldAxisSettings.Push(this.axis.majorTickMark.intervalType);
  641. _oldAxisSettings.Push(this.axis.majorTickMark.intervalOffset);
  642. _oldAxisSettings.Push(this.axis.majorTickMark.intervalOffsetType);
  643. _oldAxisSettings.Push(this.axis.LabelStyle.interval);
  644. _oldAxisSettings.Push(this.axis.LabelStyle.intervalType);
  645. _oldAxisSettings.Push(this.axis.LabelStyle.intervalOffset);
  646. _oldAxisSettings.Push(this.axis.LabelStyle.intervalOffsetType);
  647. }
  648. // Copy settings from the segment into the axis
  649. this.axis.maximum = this.ScaleMaximum;
  650. this.axis.minimum = this.ScaleMinimum;
  651. this.axis.majorGrid.interval = this.Interval;
  652. this.axis.majorGrid.intervalType = this.IntervalType;
  653. this.axis.majorGrid.intervalOffset = this.IntervalOffset;
  654. this.axis.majorGrid.intervalOffsetType = this.IntervalOffsetType;
  655. this.axis.majorTickMark.interval = this.Interval;
  656. this.axis.majorTickMark.intervalType = this.IntervalType;
  657. this.axis.majorTickMark.intervalOffset = this.IntervalOffset;
  658. this.axis.majorTickMark.intervalOffsetType = this.IntervalOffsetType;
  659. this.axis.LabelStyle.interval = this.Interval;
  660. this.axis.LabelStyle.intervalType = this.IntervalType;
  661. this.axis.LabelStyle.intervalOffset = this.IntervalOffset;
  662. this.axis.LabelStyle.intervalOffsetType = this.IntervalOffsetType;
  663. }
  664. /// <summary>
  665. /// Restore previously saved axis settings.
  666. /// </summary>
  667. internal void RestoreAxisScaleAndInterval()
  668. {
  669. if(_oldAxisSettings.Count > 0)
  670. {
  671. this.axis.LabelStyle.intervalOffsetType = (DateTimeIntervalType)_oldAxisSettings.Pop();
  672. this.axis.LabelStyle.intervalOffset = (double)_oldAxisSettings.Pop();
  673. this.axis.LabelStyle.intervalType = (DateTimeIntervalType)_oldAxisSettings.Pop();
  674. this.axis.LabelStyle.interval = (double)_oldAxisSettings.Pop();
  675. this.axis.majorTickMark.intervalOffsetType = (DateTimeIntervalType)_oldAxisSettings.Pop();
  676. this.axis.majorTickMark.intervalOffset = (double)_oldAxisSettings.Pop();
  677. this.axis.majorTickMark.intervalType = (DateTimeIntervalType)_oldAxisSettings.Pop();
  678. this.axis.majorTickMark.interval = (double)_oldAxisSettings.Pop();
  679. this.axis.majorGrid.intervalOffsetType = (DateTimeIntervalType)_oldAxisSettings.Pop();
  680. this.axis.majorGrid.intervalOffset = (double)_oldAxisSettings.Pop();
  681. this.axis.majorGrid.intervalType = (DateTimeIntervalType)_oldAxisSettings.Pop();
  682. this.axis.majorGrid.interval = (double)_oldAxisSettings.Pop();
  683. this.axis.minimum = (double)_oldAxisSettings.Pop();
  684. this.axis.maximum = (double)_oldAxisSettings.Pop();
  685. }
  686. }
  687. #endregion // Helper Methods
  688. }
  689. /// <summary>
  690. /// <b>AxisScaleSegmentCollection</b> is a class that stores collection of axis segments.
  691. /// </summary>
  692. [
  693. SRDescription("DescriptionAttributeAxisScaleSegmentCollection_AxisScaleSegmentCollection"),
  694. ]
  695. internal class AxisScaleSegmentCollection : CollectionBase
  696. {
  697. #region Fields
  698. // Axis this segment collection belongs to.
  699. private Axis _axis = null;
  700. // Segment which is always used to convert scale values.
  701. // This value is set tmporarly when only one segment has
  702. // to handle all the values.
  703. private AxisScaleSegment _enforcedSegment = null;
  704. // Indicates that values allowed to be outside of the scale segment.
  705. // Otherwise they will be rounded to Min and Max values.
  706. internal bool AllowOutOfScaleValues = false;
  707. #endregion // Fields
  708. #region Construction and Initialization
  709. /// <summary>
  710. /// Default public constructor.
  711. /// </summary>
  712. /// <remarks>
  713. /// This constructor is for internal use and should not be part of documentation.
  714. /// </remarks>
  715. public AxisScaleSegmentCollection()
  716. {
  717. }
  718. /// <summary>
  719. /// Default public constructor.
  720. /// </summary>
  721. /// <remarks>
  722. /// This constructor is for internal use and should not be part of documentation.
  723. /// </remarks>
  724. /// <param name="axis">
  725. /// Chart axis this collection belongs to
  726. /// </param>
  727. internal AxisScaleSegmentCollection(Axis axis)
  728. {
  729. this._axis = axis;
  730. }
  731. #endregion // Construction and Initialization
  732. #region Indexer
  733. /// <summary>
  734. /// Axis scale segment collection indexer.
  735. /// </summary>
  736. /// <remarks>
  737. /// The <b>AxisScaleSegment</b> object index can be provided as a parameter. Returns the <see cref="AxisScaleSegment"/> object.
  738. /// </remarks>
  739. [
  740. SRDescription("DescriptionAttributeAxisScaleSegmentCollection_Item"),
  741. ]
  742. public AxisScaleSegment this[int index]
  743. {
  744. get
  745. {
  746. return (AxisScaleSegment)this.List[(int)index];
  747. }
  748. }
  749. #endregion // Indexer
  750. #region Collection Add and Insert methods
  751. /// <summary>
  752. /// Adds a segment to the end of the collection.
  753. /// </summary>
  754. /// <param name="segment">
  755. /// <see cref="AxisScaleSegment"/> object to add.
  756. /// </param>
  757. /// <returns>
  758. /// Index of the newly added object.
  759. /// </returns>
  760. public int Add(AxisScaleSegment segment)
  761. {
  762. return this.List.Add(segment);
  763. }
  764. #endregion // Collection Add and Insert methods
  765. #region Items Inserting and Removing Notification methods
  766. /// <summary>
  767. /// After new item inserted.
  768. /// </summary>
  769. /// <param name="index">Item index.</param>
  770. /// <param name="value">Item object.</param>
  771. /// <remarks>
  772. /// This is an internal method and should not be part of the documentation.
  773. /// </remarks>
  774. protected override void OnInsertComplete(int index, object value)
  775. {
  776. ((AxisScaleSegment)value).axis = this._axis;
  777. }
  778. /// <summary>
  779. /// After items is set.
  780. /// </summary>
  781. /// <param name="index">The zero-based index at which oldValue can be found.</param>
  782. /// <param name="oldValue">The value to replace with newValue.</param>
  783. /// <param name="newValue">The new value of the element at index.</param>
  784. /// <remarks>
  785. /// This is an internal method and should not be part of the documentation.
  786. /// </remarks>
  787. protected override void OnSetComplete(int index, object oldValue, object newValue)
  788. {
  789. ((AxisScaleSegment)newValue).axis = this._axis;
  790. }
  791. #endregion
  792. #region Helper Methods
  793. /// <summary>
  794. /// Ensures that specified axis scale segment is used for all coordinate transformations.
  795. /// Set tot NULL to reset.
  796. /// </summary>
  797. /// <param name="segment"></param>
  798. internal void EnforceSegment(AxisScaleSegment segment)
  799. {
  800. this._enforcedSegment = segment;
  801. }
  802. /// <summary>
  803. /// Find axis scale segment that should be used to translate axis value to relative coordinates.
  804. /// </summary>
  805. /// <param name="axisValue">Axis value to convert.</param>
  806. /// <returns>Scale segment to use for the convertion.</returns>
  807. public AxisScaleSegment FindScaleSegmentForAxisValue(double axisValue)
  808. {
  809. // Check if no segments defined
  810. if(this.List.Count == 0)
  811. {
  812. return null;
  813. }
  814. // Check if segment enforcment is enabled
  815. if(_enforcedSegment != null)
  816. {
  817. return _enforcedSegment;
  818. }
  819. // Iterate through all segments
  820. for(int index = 0; index < this.Count; index++)
  821. {
  822. if(axisValue < this[index].ScaleMinimum)
  823. {
  824. if(index == 0)
  825. {
  826. return this[index];
  827. }
  828. else
  829. {
  830. // Find the segment which is "closer" to the value
  831. if( Math.Abs(this[index].ScaleMinimum - axisValue) < Math.Abs(axisValue - this[index - 1].ScaleMaximum))
  832. {
  833. return this[index];
  834. }
  835. else
  836. {
  837. return this[index - 1];
  838. }
  839. }
  840. }
  841. if(axisValue <= this[index].ScaleMaximum)
  842. {
  843. return this[index];
  844. }
  845. else if(index == this.Count - 1)
  846. {
  847. return this[index];
  848. }
  849. }
  850. return null;
  851. }
  852. #endregion // Helper Methods
  853. }
  854. }