GasGaugeRegion.cs 19.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
//============================================================================
//GasGaugeRegion Class
//Copyright © 2006 Jay Mistry
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//=============================================================================
using System.Runtime.Serialization;
using System.Drawing.Drawing2D;
using System;
using System.Text;
using System.Drawing;
using System.Security.Permissions;

namespace ZedGraph
{
	/// <summary>
	/// A class representing a region on the GasGuage chart
	/// <see cref="GasGaugeRegion"/>s.
	/// </summary>
	/// <author> Jay Mistry </author>
	/// <version> $Revision: 1.2 $ $Date: 2007/07/30 05:26:23 $ </version>
	[Serializable]
	public class GasGaugeRegion : CurveItem, ICloneable, ISerializable
	{
		#region Fields

		/// <summary>
		/// Defines the minimum value of this <see cref="GasGaugeRegion"/>
		/// </summary>
		private double _minValue;

		/// <summary>
		/// Defines the maximum value of this <see cref="GasGaugeRegion"/>
		/// </summary>
		private double _maxValue;

		/// <summary>
		/// Defines the Color of this <see cref="GasGaugeRegion"/>
		/// </summary>
		private Color _color;

		/// <summary>
		/// Internally calculated; Start angle of this pie that defines this <see cref="GasGaugeRegion"/>
		/// </summary>
		private float _startAngle;

		/// <summary>
		/// Internally calculated; Sweep angle of this pie that defines this <see cref="GasGaugeRegion"/>
		/// </summary>
		private float _sweepAngle;

		/// <summary>
		/// Private	field	that stores the	<see cref="ZedGraph.Fill"/> data for this
		/// <see cref="GasGaugeRegion"/>.	 Use the public property <see	cref="Fill"/> to
		/// access this value.
		/// </summary>
		private Fill _fill;

		/// <summary>
		/// A <see cref="ZedGraph.TextObj"/> which will customize the label display of this
		/// <see cref="GasGaugeRegion"/>
		/// </summary>
		private TextObj _labelDetail;

		/// <summary>
		/// Private	field	that stores the	<see cref="Border"/> class that defines	the
		/// properties of the	border around	this <see cref="GasGaugeRegion"/>. Use the public
		/// property	<see cref="Border"/> to access this value.
		/// </summary>
		private Border _border;

		/// <summary>
		/// The bounding rectangle for this <see cref="GasGaugeRegion"/>.
		/// </summary>
		private RectangleF _boundingRectangle;

		/// <summary>
		/// Private field to hold the GraphicsPath of this <see cref="GasGaugeRegion"/> to be
		/// used for 'hit testing'.
		/// </summary>
		private GraphicsPath _slicePath;

		#endregion

		#region Serialization
		/// <summary>
		/// Current schema value that defines the version of the serialized file
		/// </summary>
		public const int schema2 = 10;

		/// <summary>
		/// Constructor for deserializing objects
		/// </summary>
		/// <param name="info">A <see cref="SerializationInfo"/> instance that defines the serialized data
		/// </param>
		/// <param name="context">A <see cref="StreamingContext"/> instance that contains the serialized data
		/// </param>
		protected GasGaugeRegion( SerializationInfo info, StreamingContext context )
			: base( info, context )
		{
			// The schema value is just a file version parameter. You can use it to make future versions
			// backwards compatible as new member variables are added to classes
			int sch = info.GetInt32( "schema2" );

			_labelDetail = (TextObj)info.GetValue( "labelDetail", typeof( TextObj ) );
			_fill = (Fill)info.GetValue( "fill", typeof( Fill ) );
			_border = (Border)info.GetValue( "border", typeof( Border ) );
			_color = (Color)info.GetValue( "color", typeof( Color ) );
			_minValue = info.GetDouble( "minValue" );
			_maxValue = info.GetDouble( "maxValue" );
			_startAngle = (float)info.GetDouble( "startAngle" );
			_sweepAngle = (float)info.GetDouble( "sweepAngle" );
			_boundingRectangle = (RectangleF)info.GetValue( "boundingRectangle", typeof( RectangleF ) );
			_slicePath = (GraphicsPath)info.GetValue( "slicePath", typeof( GraphicsPath ) );
		}

		/// <summary>
		/// Populates a <see cref="SerializationInfo"/> instance with the data needed to serialize the target object
		/// </summary>
		/// <param name="info">A <see cref="SerializationInfo"/> instance that defines the serialized data</param>
		/// <param name="context">A <see cref="StreamingContext"/> instance that contains the serialized data</param>
		[SecurityPermissionAttribute( SecurityAction.Demand, SerializationFormatter = true )]
		public override void GetObjectData( SerializationInfo info, StreamingContext context )
		{
			base.GetObjectData( info, context );
			info.AddValue( "schema2", schema2 );
			info.AddValue( "labelDetail", _labelDetail );
			info.AddValue( "fill", _fill );
			info.AddValue( "color", _color );
			info.AddValue( "border", _border );
			info.AddValue( "minVal", _minValue );
			info.AddValue( "maxVal", _maxValue );
			info.AddValue( "startAngle", _startAngle );
			info.AddValue( "sweepAngle", _sweepAngle );
			info.AddValue( "boundingRectangle", _boundingRectangle );
			info.AddValue( "slicePath", _slicePath );
		}
		#endregion

		#region Constructors

		/// <summary>
		/// Create a new <see cref="GasGaugeRegion"/>
		/// </summary>
		/// <param name="label">The value associated with this <see cref="GasGaugeRegion"/> instance.</param>
		/// <param name="color">The display color for this <see cref="GasGaugeRegion"/> instance.</param>
		/// <param name="minVal">The minimum value of this <see cref="GasGaugeNeedle"/>.</param>
		/// <param name="maxVal">The maximum value of this <see cref="GasGaugeNeedle"/>.</param>
		public GasGaugeRegion( string label, double minVal, double maxVal, Color color )
			: base( label )
		{
			MinValue = minVal;
			MaxValue = maxVal;
			RegionColor = color;
			StartAngle = 0f;
			SweepAngle = 0f;
			_border = new Border( Default.BorderColor, Default.BorderWidth );
			_labelDetail = new TextObj();
			_labelDetail.FontSpec.Size = Default.FontSize;
			_slicePath = null;
		}

		/// <summary>
		/// The Copy Constructor
		/// </summary>
		/// <param name="ggr">The <see cref="GasGaugeRegion"/> object from which to copy</param>
		public GasGaugeRegion( GasGaugeRegion ggr )
			: base( ggr )
		{
			_minValue = ggr._minValue;
			_maxValue = ggr._maxValue;
			_color = ggr._color;
			_startAngle = ggr._startAngle;
			_sweepAngle = ggr._sweepAngle;
			_border = ggr._border.Clone();
			_labelDetail = ggr._labelDetail.Clone();
		}

		/// <summary>
		/// Implement the <see cref="ICloneable" /> interface in a typesafe manner by just
		/// calling the typed version of <see cref="Clone" />
		/// </summary>
		/// <returns>A deep copy of this object</returns>
		object ICloneable.Clone()
		{
			return this.Clone();
		}

		/// <summary>
		/// Typesafe, deep-copy clone method.
		/// </summary>
		/// <returns>A new, independent copy of this class</returns>
		public GasGaugeRegion Clone()
		{
			return new GasGaugeRegion( this );
		}
		#endregion

		#region Properties

		/// <summary>
		/// Gets or sets the SlicePath of this <see cref="GasGaugeRegion"/>
		/// </summary>
		public GraphicsPath SlicePath
		{
			get { return _slicePath; }
		}

		/// <summary>
		/// Gets or sets the LabelDetail of this <see cref="GasGaugeRegion"/>
		/// </summary>
		public TextObj LabelDetail
		{
			get { return _labelDetail; }
			set { _labelDetail = value; }
		}

		/// <summary>
		/// Gets or sets the Border of this <see cref="GasGaugeRegion"/>
		/// </summary>
		public Border Border
		{
			get { return ( _border ); }
			set { _border = value; }
		}

		/// <summary>
		/// Gets or sets the RegionColor of this <see cref="GasGaugeRegion"/>
		/// </summary>
		public Color RegionColor
		{
			get { return _color; }
			set
			{
				_color = value;
				Fill = new Fill( _color );
			}
		}

		/// <summary>
		/// Gets or sets the Fill of this <see cref="GasGaugeRegion"/>
		/// </summary>
		public Fill Fill
		{
			get { return _fill; }
			set { _fill = value; }
		}

		/// <summary>
		/// Gets or sets the SweepAngle of this <see cref="GasGaugeRegion"/>
		/// </summary>
		private float SweepAngle
		{
			get { return _sweepAngle; }
			set { _sweepAngle = value; }
		}

		/// <summary>
		/// Gets or sets the StartAngle of this <see cref="GasGaugeRegion"/>
		/// </summary>
		private float StartAngle
		{
			get { return ( _startAngle ); }
			set { _startAngle = value; }
		}

		/// <summary>
		/// Gets or sets the MinValue of this <see cref="GasGaugeRegion"/>
		/// </summary>
		public double MinValue
		{
			get { return ( _minValue ); }
			set { _minValue = value > 0 ? value : 0; }
		}

		/// <summary>
		/// Gets or sets the MaxValue of this <see cref="GasGaugeRegion"/>
		/// </summary>
		public double MaxValue
		{
			get { return ( _maxValue ); }
			set { _maxValue = value > 0 ? value : 0; }
		}

		/// <summary>
		/// Gets a flag indicating if the Z data range should be included in the axis scaling calculations.
		/// </summary>
		/// <param name="pane">The parent <see cref="GraphPane" /> of this <see cref="CurveItem" />.
		/// </param>
		/// <value>true if the Z data are included, false otherwise</value>
		override internal bool IsZIncluded( GraphPane pane )
		{
			return false;
		}

		/// <summary>
		/// Gets a flag indicating if the X axis is the independent axis for this <see cref="CurveItem" />
		/// </summary>
		/// <param name="pane">The parent <see cref="GraphPane" /> of this <see cref="CurveItem" />.
		/// </param>
		/// <value>true if the X axis is independent, false otherwise</value>
		override internal bool IsXIndependent( GraphPane pane )
		{
			return true;
		}

	#endregion

	#region Defaults

		/// <summary>
		/// Specify the default property values for the <see cref="GasGaugeRegion"/> class.
		/// </summary>
		public struct Default
		{
			/// <summary>
			/// The default border pen width for the <see cref="GasGaugeRegion"/>
			/// </summary>
			public static float BorderWidth = 1.0F;

			/// <summary>
			/// The default fill type for the <see cref="GasGaugeRegion"/>
			/// </summary>
			public static FillType FillType = FillType.Brush;

			/// <summary>
			/// The default value for the visibility of the <see cref="GasGaugeRegion"/> border.
			/// </summary>
			public static bool IsBorderVisible = true;

			/// <summary>
			/// The default value for the color of the <see cref="GasGaugeRegion"/> border
			/// </summary>
			public static Color BorderColor = Color.Gray;

			/// <summary>
			/// The default value for the color of the <see cref="GasGaugeRegion"/> fill
			/// </summary>
			public static Color FillColor = Color.Empty;

			/// <summary>
			/// The default value for the fill brush of the <see cref="GasGaugeRegion"/>
			/// </summary>
			public static Brush FillBrush = null;

			/// <summary>
			/// The default value for the visibility of the <see cref="GasGaugeRegion"/> fill.
			/// </summary>
			public static bool isVisible = true;

//			public static PieLabelType LabelType = PieLabelType.Name;

			/// <summary>
			/// The default value for the font size of the <see cref="GasGaugeRegion"/> labels.
			/// </summary>
			public static float FontSize = 10;
		}

	#endregion Defaults

	#region Methods

		/// <summary>
		/// Do all rendering associated with this <see cref="GasGaugeRegion"/> item to the specified
		/// <see cref="Graphics"/> device. This method is normally only
		/// called by the Draw method of the parent <see cref="ZedGraph.CurveList"/>
		/// collection object.
		/// </summary>
		/// <param name="g">
		/// A graphic device object to be drawn into. This is normally e.Graphics from the
		/// PaintEventArgs argument to the Paint() method.
		/// </param>
		/// <param name="pane">
		/// A reference to the <see cref="ZedGraph.GraphPane"/> object that is the parent or
		/// owner of this object.
		/// </param>
		/// <param name="pos">Not used for rendering GasGaugeNeedle</param>
		/// <param name="scaleFactor">
		/// The scaling factor to be used for rendering objects. This is calculated and
		/// passed down by the parent <see cref="ZedGraph.GraphPane"/> object using the
		/// <see cref="PaneBase.CalcScaleFactor"/> method, and is used to proportionally adjust
		/// font sizes, etc. according to the actual size of the graph.
		/// </param>	
		public override void Draw( Graphics g, GraphPane pane, int pos, float scaleFactor )
		{
			if ( pane.Chart._rect.Width <= 0 && pane.Chart._rect.Height <= 0 )
			{
				_slicePath = null;
			}
			else
			{
				CalcRectangle( g, pane, scaleFactor, pane.Chart._rect );

				_slicePath = new GraphicsPath();

				if ( !_isVisible )
					return;

				RectangleF tRect = _boundingRectangle;

				if ( tRect.Width >= 1 && tRect.Height >= 1 )
				{
					SmoothingMode sMode = g.SmoothingMode;
					g.SmoothingMode = SmoothingMode.AntiAlias;

					_slicePath.AddPie( tRect.X, tRect.Y, tRect.Width, tRect.Height,
															 -0.0f, -180.0f );

					g.FillPie( Fill.MakeBrush( _boundingRectangle ), tRect.X, tRect.Y, tRect.Width, tRect.Height, -StartAngle, -SweepAngle );

					if ( this.Border.IsVisible )
					{
						Pen borderPen = _border.GetPen( pane, scaleFactor );
						g.DrawPie( borderPen, tRect.X, tRect.Y, tRect.Width, tRect.Height,
							 -0.0f, -180.0f );
						borderPen.Dispose();
					}

					g.SmoothingMode = sMode;
				}
			}
		}

		/// <summary>
		/// Render the label for this <see cref="GasGaugeRegion"/>.
		/// </summary>
		/// <param name="g">
		/// A graphic device object to be drawn into. This is normally e.Graphics from the
		/// PaintEventArgs argument to the Paint() method.
		/// </param>
		/// <param name="pane">
		/// A graphic device object to be drawn into. This is normally e.Graphics from the
		/// PaintEventArgs argument to the Paint() method.
		/// </param>
		/// <param name="rect">Bounding rectangle for this <see cref="GasGaugeRegion"/>.</param>
		/// <param name="scaleFactor">
		/// The scaling factor to be used for rendering objects. This is calculated and
		/// passed down by the parent <see cref="ZedGraph.GraphPane"/> object using the
		/// <see cref="PaneBase.CalcScaleFactor"/> method, and is used to proportionally adjust
		/// font sizes, etc. according to the actual size of the graph.
		/// </param>		
		public override void DrawLegendKey( Graphics g, GraphPane pane, RectangleF rect, float scaleFactor )
		{
			if ( !_isVisible )
				return;

			// Fill the slice
			if ( _fill.IsVisible )
			{
				// just avoid height/width being less than 0.1 so GDI+ doesn't cry
				using ( Brush brush = _fill.MakeBrush( rect ) )
				{
					g.FillRectangle( brush, rect );
					//brush.Dispose();
				}
			}

			// Border the bar
			if ( !_border.Color.IsEmpty )
				_border.Draw( g, pane, scaleFactor, rect );
		}

		/// <summary>
		/// Determine the coords for the rectangle associated with a specified point for 
		/// this <see cref="CurveItem" />
		/// </summary>
		/// <param name="pane">The <see cref="GraphPane" /> to which this curve belongs</param>
		/// <param name="i">The index of the point of interest</param>
		/// <param name="coords">A list of coordinates that represents the "rect" for
		/// this point (used in an html AREA tag)</param>
		/// <returns>true if it's a valid point, false otherwise</returns>
		public override bool GetCoords( GraphPane pane, int i, out string coords )
		{
			coords = String.Empty;
			return false;
		}

		/// <summary>
		/// Calculate the values needed to properly display this <see cref="GasGaugeRegion"/>.
		/// </summary>
		/// <param name="pane">
		/// A graphic device object to be drawn into. This is normally e.Graphics from the
		/// PaintEventArgs argument to the Paint() method.
		/// </param>
		public static void CalculateGasGuageParameters( GraphPane pane )
		{
			//loop thru slices and get total value and maxDisplacement
			double minVal = double.MaxValue;
			double maxVal = double.MinValue;
			foreach ( CurveItem curve in pane.CurveList )
				if ( curve is GasGaugeRegion )
				{
					GasGaugeRegion ggr = (GasGaugeRegion)curve;
					if ( maxVal < ggr.MaxValue )
						maxVal = ggr.MaxValue;

					if ( minVal > ggr.MinValue )
						minVal = ggr.MinValue;
				}

			//Calculate start and sweep angles for each of the GasGaugeRegion based on teh min and max value
			foreach ( CurveItem curve in pane.CurveList )
			{
				if ( curve is GasGaugeRegion )
				{
					GasGaugeRegion ggr = (GasGaugeRegion)curve;
					float start = ( (float)ggr.MinValue - (float)minVal ) / ( (float)maxVal - (float)minVal ) * 180.0f;
					float sweep = ( (float)ggr.MaxValue - (float)minVal ) / ( (float)maxVal - (float)minVal ) * 180.0f;
					sweep = sweep - start;

					Fill f = new Fill( Color.White, ggr.RegionColor, -( sweep / 2f ) );
					ggr.Fill = f;

					ggr.StartAngle = start;
					ggr.SweepAngle = sweep;
				}
			}
		}

		/// <summary>
		/// Calculate the <see cref="RectangleF"/> that will be used to define the bounding rectangle of
		/// the GasGaugeNeedle.
		/// </summary>
		/// <remarks>This rectangle always lies inside of the <see cref="Chart.Rect"/>, and it is
		/// normally a square so that the pie itself is not oval-shaped.</remarks>
		/// <param name="g">
		/// A graphic device object to be drawn into. This is normally e.Graphics from the
		/// PaintEventArgs argument to the Paint() method.
		/// </param>
		/// <param name="pane">
		/// A reference to the <see cref="ZedGraph.GraphPane"/> object that is the parent or
		/// owner of this object.
		/// </param>
		/// <param name="scaleFactor">
		/// The scaling factor to be used for rendering objects. This is calculated and
		/// passed down by the parent <see cref="ZedGraph.GraphPane"/> object using the
		/// <see cref="PaneBase.CalcScaleFactor"/> method, and is used to proportionally adjust
		/// font sizes, etc. according to the actual size of the graph.
		/// </param>				
		/// <param name="chartRect">The <see cref="RectangleF"/> (normally the <see cref="Chart.Rect"/>)
		/// that bounds this pie.</param>
		/// <returns></returns>
		public static RectangleF CalcRectangle( Graphics g, GraphPane pane, float scaleFactor, RectangleF chartRect )
		{
			RectangleF nonExpRect = chartRect;

			if ( ( 2 * nonExpRect.Height ) > nonExpRect.Width )
			{
				//Scale based on width
				float percentS = ( ( nonExpRect.Height * 2 ) - nonExpRect.Width ) / ( nonExpRect.Height * 2 );
				nonExpRect.Height = ( ( nonExpRect.Height * 2 ) - ( ( nonExpRect.Height * 2 ) * percentS ) );
			}
			else
			{
				nonExpRect.Height = nonExpRect.Height * 2;
			}

			nonExpRect.Width = nonExpRect.Height;

			float xDelta = ( chartRect.Width / 2 ) - ( nonExpRect.Width / 2 );

			//Align Horizontally
			nonExpRect.X += xDelta;
			//nonExpRect.Y += -(float)0.025F * nonExpRect.Height;
			//nonExpRect.Y += ((chartRect.Height) - (nonExpRect.Height / 2)) - 10.0f;

			nonExpRect.Inflate( -(float)0.05F * nonExpRect.Height, -(float)0.05 * nonExpRect.Width );

			GasGaugeRegion.CalculateGasGuageParameters( pane );

			foreach ( CurveItem curve in pane.CurveList )
			{
				if ( curve is GasGaugeRegion )
				{
					GasGaugeRegion gg = (GasGaugeRegion)curve;
					gg._boundingRectangle = nonExpRect;
				}
			}

			return nonExpRect;

		}

		#endregion
	}
}