//============================================================================
//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
//=============================================================================
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.Serialization;
using System.Security.Permissions;
namespace DrawGraph
{
	/// 
	/// Encapsulates a curve type that is displayed as a line and/or a set of
	/// symbols at each point.
	/// 
	/// 
	///  John Champion 
	///  $Revision: 3.21 $ $Date: 2007/04/16 00:03:02 $ 
	[Serializable]
	public class LineItem : CurveItem, ICloneable, ISerializable
	{
	#region Fields
		/// 
		/// Private field that stores a reference to the 
		/// class defined for this .  Use the public
		/// property  to access this value.
		/// 
		protected Symbol	_symbol;
		/// 
		/// Private field that stores a reference to the 
		/// class defined for this .  Use the public
		/// property  to access this value.
		/// 
		protected Line		_line;
	#endregion
	#region Properties
		/// 
		/// Gets or sets the  class instance defined
		/// for this .
		/// 
		public Symbol Symbol
		{
			get { return _symbol; }
			set { _symbol = value; }
		}
		/// 
		/// Gets or sets the  class instance defined
		/// for this .
		/// 
		public Line Line
		{
			get { return _line; }
			set { _line = value; }
		}
		/// 
		/// Gets a flag indicating if the Z data range should be included in the axis scaling calculations.
		/// 
		/// The parent  of this .
		/// 
		/// true if the Z data are included, false otherwise
		override internal bool IsZIncluded( GraphPane pane )
		{
			return false;
		}
		/// 
		/// Gets a flag indicating if the X axis is the independent axis for this 
		/// 
		/// The parent  of this .
		/// 
		/// true if the X axis is independent, false otherwise
		override internal bool IsXIndependent( GraphPane pane )
		{
			return true;
		}
		
	#endregion
	
	#region Constructors
		/// 
		/// Create a new , specifying only the legend .
		/// 
		/// The _label that will appear in the legend.
		public LineItem( string label ) : base( label )
		{
			_symbol = new Symbol();
			_line = new Line();
		}
		
		/// 
		/// Create a new  using the specified properties.
		/// 
		/// The _label that will appear in the legend.
		/// An array of double precision values that define
		/// the independent (X axis) values for this curve
		/// An array of double precision values that define
		/// the dependent (Y axis) values for this curve
		/// A  value that will be applied to
		/// the  and  properties.
		/// 
		/// A  enum specifying the
		/// type of symbol to use for this .  Use 
		/// to hide the symbols.
		/// The width (in points) to be used for the .  This
		/// width is scaled based on .  Use a value of zero to
		/// hide the line (see ).
		public LineItem( string label, double[] x, double[] y, Color color, SymbolType symbolType, float lineWidth )
			: this( label, new PointPairList( x, y ), color, symbolType, lineWidth )
		{
		}
		/// 
		/// Create a new  using the specified properties.
		/// 
		/// The _label that will appear in the legend.
		/// An array of double precision values that define
		/// the independent (X axis) values for this curve
		/// An array of double precision values that define
		/// the dependent (Y axis) values for this curve
		/// A  value that will be applied to
		/// the  and  properties.
		/// 
		/// A  enum specifying the
		/// type of symbol to use for this .  Use 
		/// to hide the symbols.
		public LineItem( string label, double[] x, double[] y, Color color, SymbolType symbolType )
			: this( label, new PointPairList( x, y ), color, symbolType )
		{
		}
		/// 
		/// Create a new  using the specified properties.
		/// 
		/// The _label that will appear in the legend.
		/// A  of double precision value pairs that define
		/// the X and Y values for this curve
		/// A  value that will be applied to
		/// the  and  properties.
		/// 
		/// A  enum specifying the
		/// type of symbol to use for this .  Use 
		/// to hide the symbols.
		/// The width (in points) to be used for the .  This
		/// width is scaled based on .  Use a value of zero to
		/// hide the line (see ).
		public LineItem( string label, IPointList points, Color color, SymbolType symbolType, float lineWidth )
			: base( label, points )
		{
			_line = new Line( color );
			if ( lineWidth == 0 )
				_line.IsVisible = false;
			else
				_line.Width = lineWidth;
			_symbol = new Symbol( symbolType, color );
		}
		/// 
		/// Create a new  using the specified properties.
		/// 
		/// The _label that will appear in the legend.
		/// A  of double precision value pairs that define
		/// the X and Y values for this curve
		/// A  value that will be applied to
		/// the  and  properties.
		/// 
		/// A  enum specifying the
		/// type of symbol to use for this .  Use 
		/// to hide the symbols.
		public LineItem( string label, IPointList points, Color color, SymbolType symbolType )
			: this( label, points, color, symbolType, DrawGraph.LineBase.Default.Width )
		{
		}
		/// 
		/// The Copy Constructor
		/// 
		/// The  object from which to copy
		public LineItem( LineItem rhs ) : base( rhs )
		{
			_symbol = new Symbol( rhs.Symbol );
			_line = new Line( rhs.Line );
		}
		/// 
		/// 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 LineItem Clone()
		{
			return new LineItem( this );
		}
	#endregion
	#region Serialization
		/// 
		/// Current schema value that defines the version of the serialized file
		/// 
		public const int schema2 = 10;
		/// 
		/// Constructor for deserializing objects
		/// 
		/// A  instance that defines the serialized data
		/// 
		/// A  instance that contains the serialized data
		/// 
		protected LineItem( 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" );
			_symbol = (Symbol) info.GetValue( "symbol", typeof(Symbol) );
			_line = (Line) info.GetValue( "line", typeof(Line) );
		}
		/// 
		/// 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 override void GetObjectData( SerializationInfo info, StreamingContext context )
		{
			base.GetObjectData( info, context );
			info.AddValue( "schema2", schema2 );
			info.AddValue( "symbol", _symbol );
			info.AddValue( "line", _line );
		}
	#endregion
	#region Methods
		/// 
		/// Do all rendering associated with this  to the specified
		///  device.  This method is normally only
		/// called by the Draw method of the parent 
		/// collection object.
		/// 
		/// 
		/// 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.
		/// 
		/// The ordinal position of the current 
		/// curve.
		/// 
		/// 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.
		/// 
		override public void Draw( Graphics g, GraphPane pane, int pos, float scaleFactor  )
		{
			if ( _isVisible )
			{
				Line.Draw( g, pane, this, scaleFactor );
				
				Symbol.Draw( g, pane, this, scaleFactor, IsSelected );
			}
		}		
		/// 
		/// Draw a legend key entry for this  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.
        /// 
        /// The  struct that specifies the
        /// location for the legend key
		/// 
		/// 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.
		/// 
		override public void DrawLegendKey( Graphics g, GraphPane pane, RectangleF rect, float scaleFactor )
		{
			// Draw a sample curve to the left of the label text
			float yMid = rect.Top + rect.Height / 2.0F;
			//RectangleF rect2 = rect;
			//rect2.Y = yMid;
			//rect2.Height = rect.Height / 2.0f;
			_line.Fill.Draw( g, rect );
			_line.DrawSegment( g, pane, rect.Left, yMid, rect.Right, yMid, scaleFactor );
            // Draw a sample symbol to the left of the label text				
			_symbol.DrawSymbol( g, pane, rect.Left + rect.Width / 2.0F, yMid, scaleFactor,
										false, null );
		}
	
		/// 
		/// Loads some pseudo unique colors/symbols into this LineItem.  This
		/// is mainly useful for differentiating a set of new LineItems without
		/// having to pick your own colors/symbols.
		/// 
		/// 
		/// 
		/// The  that is used to pick the color
		///  and symbol for this method call.
		/// 
		override public void MakeUnique( ColorSymbolRotator rotator )
		{
			this.Color			= rotator.NextColor;
			this.Symbol.Type	= rotator.NextSymbol;
		}
		/// 
		/// Determine the coords for the rectangle associated with a specified point for 
		/// this 
		/// 
		/// The  to which this curve belongs
		/// The index of the point of interest
		/// A list of coordinates that represents the "rect" for
		/// this point (used in an html AREA tag)
		/// true if it's a valid point, false otherwise
		override public bool GetCoords( GraphPane pane, int i, out string coords )
		{
			coords = string.Empty;
			if ( i < 0 || i >= _points.Count )
				return false;
			PointPair pt = _points[i];
			if ( pt.IsInvalid )
				return false;
			double x, y, z;
			ValueHandler valueHandler = new ValueHandler( pane, false );
			valueHandler.GetValues( this, i, out x, out z, out y );
			Axis yAxis = GetYAxis( pane );
			Axis xAxis = GetXAxis( pane );
			PointF pixPt = new PointF( xAxis.Scale.Transform( _isOverrideOrdinal, i, x ),
							yAxis.Scale.Transform( _isOverrideOrdinal, i, y ) );
			
			if ( !pane.Chart.Rect.Contains( pixPt ) )
				return false;
			float halfSize = _symbol.Size * pane.CalcScaleFactor();
			coords = String.Format( "{0:f0},{1:f0},{2:f0},{3:f0}",
					pixPt.X - halfSize, pixPt.Y - halfSize,
					pixPt.X + halfSize, pixPt.Y + halfSize );
			return true;
		}
	#endregion
	}
}