//============================================================================
//ZedGraph Class Library - A Flexible Line Graph/Bar Graph Library in C#
//Copyright © 2004  John Champion
//
//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
//=============================================================================
#region Using directives
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Runtime.Serialization;
using System.Security.Permissions;
#endregion
namespace DrawGraph
{
	/// 
	/// This class handles the drawing of the curve  objects.
	/// The Error Bars are the vertical lines with a symbol at each end.
	/// 
	/// To draw "I-Beam" bars, the symbol type defaults to
	/// , which is just a horizontal line.
	/// If  is Y-oriented, then the symbol type should be
	/// set to  to get the same effect.
	/// 
	/// 
	///  John Champion 
	///  $Revision: 3.20 $ $Date: 2007/04/16 00:03:01 $ 
	[Serializable]
	public class ErrorBar : ICloneable, ISerializable
	{
	#region Fields
		/// 
		/// Private field that stores the visibility of this
		/// .  Use the public
		/// property  to access this value.  If this value is
		/// false, the symbols will not be shown.
		/// 
		private bool		_isVisible;
		/// 
		/// Private field that stores the error bar color.  Use the public
		/// property  to access this value.
		/// 
		private Color		_color;
		/// 
		/// Private field that stores the pen width for this error bar.  Use the public
		/// property  to access this value.
		/// 
		private float		_penWidth;
		/// 
		/// private field that contains the symbol element that will be drawn
		/// at the top and bottom of the error bar.  Use the public property
		///  to access this value.
		/// 
		private Symbol		_symbol;
	#endregion
	#region Defaults
		/// 
		/// A simple struct that defines the
		/// default property values for the  class.
		/// 
		public struct Default
		{
			// Default Symbol properties
			/// 
			/// The default size for curve symbols
			/// ( property),
			/// in units of points.
			/// 
			public static float Size = 7;
			/// 
			/// The default pen width to be used for drawing error bars
			/// ( property).  Units are points.
			/// 
			public static float PenWidth = 1.0F;
			/// 
			/// The default display mode for symbols ( property).
			/// true to display symbols, false to hide them.
			/// 
			public static bool IsVisible = true;
			/// 
			/// The default color for drawing error bars ( property).
			/// 
			public static Color Color = Color.Red;
			/// 
			/// The default symbol for drawing at the top and bottom of the
			/// error bar (see ).
			/// 
			public static SymbolType Type = SymbolType.HDash;
		}
	#endregion
	#region Properties
		/// 
		/// Gets or sets a property that shows or hides the .
		/// 
		/// true to show the error bar, false to hide it
		/// 
		public bool IsVisible
		{
			get { return _isVisible; }
			set { _isVisible = value; }
		}
		
		/// 
		/// Gets or sets the  data for this
		/// .
		/// 
		/// This property only controls the color of
		/// the vertical line.  The symbol color is controlled separately in
		/// the  property.
		/// 
		public Color Color
		{
			get { return _color; }
			set { _color = value; }
		}
		/// 
		/// The pen width to be used for drawing error bars
		/// Units are points.
		/// 
		/// This property only controls the pen width for the
		/// vertical line.  The pen width for the symbol outline is
		/// controlled separately by the  property.
		/// 
		public float PenWidth
		{
			get { return _penWidth; }
			set { _penWidth = value; }
		}
		/// 
		/// Contains the symbol element that will be drawn
		/// at the top and bottom of the error bar.
		/// 
		public Symbol Symbol
		{
			get { return _symbol; }
			set { _symbol = value; }
		}
	#endregion
	
	#region Constructors
		/// 
		/// Default constructor that sets all  properties to
		/// default values as defined in the  class.
		/// 
		public ErrorBar() : this( Default.Color )
		{
		}
		/// 
		/// Default constructor that sets the
		///  as specified, and the remaining
		///  properties to default
		/// values as defined in the  class.
		/// 
		/// A  value indicating
		/// the color of the symbol
		/// 
		public ErrorBar( Color color )
		{
			_symbol = new Symbol( Default.Type, color );
			_symbol.Size = Default.Size;
			_color = color;
			_penWidth = Default.PenWidth;
			_isVisible = Default.IsVisible;
		}
		/// 
		/// The Copy Constructor
		/// 
		/// The  object from which to copy
		public ErrorBar( ErrorBar rhs )
		{
			_color = rhs.Color;
			_isVisible = rhs.IsVisible;
			_penWidth = rhs.PenWidth;
			_symbol = rhs.Symbol.Clone();
		}
		/// 
		/// Implement the  interface in a typesafe manner by just
		/// calling the typed version of 
		/// 
		/// A deep copy of this object
		object ICloneable.Clone()
		{
			return this.Clone();
		}
		/// 
		/// Typesafe, deep-copy clone method.
		/// 
		/// A new, independent copy of this class
		public ErrorBar Clone()
		{
			return new ErrorBar( this );
		}
	#endregion
	#region Serialization
		/// 
		/// Current schema value that defines the version of the serialized file
		/// 
		public const int schema = 10;
		/// 
		/// Constructor for deserializing objects
		/// 
		/// A  instance that defines the serialized data
		/// 
		/// A  instance that contains the serialized data
		/// 
		protected ErrorBar( SerializationInfo info, StreamingContext 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( "schema" );
			_isVisible = info.GetBoolean( "isVisible" );
			_color = (Color) info.GetValue( "color", typeof(Color) );
			_penWidth = info.GetSingle( "penWidth" );
			_symbol = (Symbol) info.GetValue( "symbol", typeof(Symbol) );
		}
		/// 
		/// Populates a  instance with the data needed to serialize the target object
		/// 
		/// A  instance that defines the serialized data
		/// A  instance that contains the serialized data
		[SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter=true)]
		public virtual void GetObjectData( SerializationInfo info, StreamingContext context )
		{
			info.AddValue( "schema", schema );
			info.AddValue( "isVisible", _isVisible );
			info.AddValue( "color", _color );
			info.AddValue( "penWidth", _penWidth );
			info.AddValue( "symbol", _symbol );
		}
	#endregion
	#region Rendering Methods
		/// 
		/// Draw the  to the specified 
		/// device at the specified location.
		/// 
		/// 
		/// A graphic device object to be drawn into.  This is normally e.Graphics from the
		/// PaintEventArgs argument to the Paint() method.
		/// 
		/// 
		/// A reference to the  object that is the parent or
		/// owner of this object.
		/// 
		/// boolean value that indicates if the "base" axis for this
		///  is the X axis.  True for an  base,
		/// false for a  or  base.
		/// The independent axis position of the center of the error bar in
		/// pixel units
		/// The dependent axis position of the top of the error bar in
		/// pixel units
		/// The dependent axis position of the bottom of the error bar in
		/// pixel units
		/// 
		/// The scaling factor for the features of the graph based on the .  This
		/// scaling factor is calculated by the  method.  The scale factor
		/// represents a linear multiple to be applied to font sizes, symbol sizes, etc.
		/// A pen with attributes of  and
		///  for this 
		/// The data value to be used for a value-based
		/// color gradient.  This is only applicable for ,
		///  or .
		/// Indicates that the  should be drawn
		/// with attributes from the  class.
		/// 
		public void Draw( Graphics g, GraphPane pane, bool isXBase,
								float pixBase, float pixValue,
								float pixLowValue, float scaleFactor, Pen pen, bool isSelected,
								PointPair dataValue )
		{
			if ( isXBase )
			{
				g.DrawLine( pen, pixBase, pixValue, pixBase, pixLowValue );
				_symbol.DrawSymbol( g, pane, pixBase, pixValue,
							scaleFactor, isSelected, dataValue );
				_symbol.DrawSymbol( g, pane, pixBase, pixLowValue,
							scaleFactor, isSelected, dataValue );
			}
			else
			{
				g.DrawLine( pen, pixValue, pixBase, pixLowValue, pixBase );
				_symbol.DrawSymbol( g, pane, pixValue, pixBase,
							scaleFactor, isSelected, dataValue );
				_symbol.DrawSymbol( g, pane, pixLowValue, pixBase,
							scaleFactor, isSelected, dataValue );
			}
		}
		/// 
		/// Draw all the 's to the specified 
		/// device as a an error bar at each defined point.
		/// 
		/// 
		/// A graphic device object to be drawn into.  This is normally e.Graphics from the
		/// PaintEventArgs argument to the Paint() method.
		/// 
		/// 
		/// A reference to the  object that is the parent or
		/// owner of this object.
		/// 
		/// A  object representing the
		/// 's to be drawn.
		/// The  class instance that defines the base (independent)
		/// axis for the 
		/// The  class instance that defines the value (dependent)
		/// axis for the 
		/// 
		/// The scaling factor to be used for rendering objects.  This is calculated and
		/// passed down by the parent  object using the
		///  method, and is used to proportionally adjust
		/// font sizes, etc. according to the actual size of the graph.
		/// 
		public void Draw( Graphics g, GraphPane pane, ErrorBarItem curve,
							Axis baseAxis, Axis valueAxis, float scaleFactor )
		{
			ValueHandler valueHandler = new ValueHandler( pane, false );
			float	pixBase, pixValue, pixLowValue;
			double	scaleBase, scaleValue, scaleLowValue;
		
			if ( curve.Points != null && this.IsVisible )
			{
				using ( Pen pen = !curve.IsSelected ? new Pen( _color, _penWidth ) :
						new Pen( Selection.Border.Color, Selection.Border.Width ) )
				{
					// Loop over each defined point							
					for ( int i = 0; i < curve.Points.Count; i++ )
					{
						valueHandler.GetValues( curve, i, out scaleBase,
									out scaleLowValue, out scaleValue );
						// Any value set to double max is invalid and should be skipped
						// This is used for calculated values that are out of range, divide
						//   by zero, etc.
						// Also, any value <= zero on a log scale is invalid
						if ( !curve.Points[i].IsInvalid3D &&
								( scaleBase > 0 || !baseAxis._scale.IsLog ) &&
								( ( scaleValue > 0 && scaleLowValue > 0 ) || !valueAxis._scale.IsLog ) )
						{
							pixBase = baseAxis.Scale.Transform( curve.IsOverrideOrdinal, i, scaleBase );
							pixValue = valueAxis.Scale.Transform( curve.IsOverrideOrdinal, i, scaleValue );
							pixLowValue = valueAxis.Scale.Transform( curve.IsOverrideOrdinal, i, scaleLowValue );
							//if ( this.fill.IsGradientValueType )
							//	brush = fill.MakeBrush( _rect, _points[i] );
							this.Draw( g, pane, baseAxis is XAxis || baseAxis is X2Axis, pixBase, pixValue,
											pixLowValue, scaleFactor, pen, curve.IsSelected,
											curve.Points[i] );
						}
					}
				}
			}
		}
	#endregion
	
	}
}