//============================================================================
//ZedGraph Class Library - A Flexible Line Graph/Bar Graph Library in C#
//Copyright ?2005  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.Collections;
using System.Text;
using System.Drawing;
using System.Runtime.Serialization;
using System.Security.Permissions;
namespace DrawGraph
{
	/// 
	/// The OrdinalScale class inherits from the  class, and implements
	/// the features specific to .
	/// 
	/// 
	/// OrdinalScale is an ordinal axis with tic labels generated at integral values.  An ordinal axis means that
	/// all data points are evenly spaced at integral values, and the actual coordinate values
	/// for points corresponding to that axis are ignored.  That is, if the X axis is an
	/// ordinal type, then all X values associated with the curves are ignored.
	/// 
	/// 
	///  John Champion  
	///  $Revision: 1.8 $ $Date: 2007/04/16 00:03:02 $ 
	[Serializable]
	class OrdinalScale : Scale, ISerializable //, ICloneable
	{
	#region constructors
		/// 
		/// Default constructor that defines the owner 
		/// (containing object) for this new object.
		/// 
		/// The owner, or containing object, of this instance
		public OrdinalScale( Axis owner )
			: base( owner )
		{
		}
		/// 
		/// The Copy Constructor
		/// 
		/// The  object from which to copy
		/// The  object that will own the
		/// new instance of 
		public OrdinalScale( Scale rhs, Axis owner )
			: base( rhs, owner )
		{
		}
		/// 
		/// Create a new clone of the current item, with a new owner assignment
		/// 
		/// The new  instance that will be
		/// the owner of the new Scale
		/// A new  clone.
		public override Scale Clone( Axis owner )
		{
			return new OrdinalScale( this, owner );
		}
	#endregion
	#region properties
		/// 
		/// Return the  for this , which is
		/// .
		/// 
		public override AxisType Type
		{
			get { return AxisType.Ordinal; }
		}
	#endregion
	#region methods
		/// 
		/// Select a reasonable ordinal axis scale given a range of data values.
		/// 
		/// 
		/// This method only applies to  type axes, and it
		/// is called by the general  method.  The scale range is chosen
		/// based on increments of 1, 2, or 5 (because they are even divisors of 10).
		/// Being an ordinal axis type, the  value will always be integral.  This
		/// method honors the , ,
		/// and  autorange settings.
		/// In the event that any of the autorange settings are false, the
		/// corresponding , , or 
		/// setting is explicitly honored, and the remaining autorange settings (if any) will
		/// be calculated to accomodate the non-autoranged values.  The basic defaults for
		/// scale selection are defined using ,
		/// , and 
		/// from the  default class.
		/// On Exit:
		///  is set to scale minimum (if  = true)
		///  is set to scale maximum (if  = true)
		///  is set to scale step size (if  = true)
		///  is set to scale minor step size (if  = true)
		///  is set to a magnitude multiplier according to the data
		///  is set to the display format for the values (this controls the
		/// number of decimal places, whether there are thousands separators, currency types, etc.)
		/// 
		/// A reference to the  object
		/// associated with this 
		/// 
		/// A graphic device object to be drawn into.  This is normally e.Graphics from the
		/// PaintEventArgs argument to the Paint() method.
		/// 
		/// 
		/// 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 PickScale( GraphPane pane, Graphics g, float scaleFactor )
		{
			// call the base class first
			base.PickScale( pane, g, scaleFactor );
			PickScale( pane, g, scaleFactor, this );
		}
		internal static void PickScale( GraphPane pane, Graphics g, float scaleFactor, Scale scale )
		{
			// Test for trivial condition of range = 0 and pick a suitable default
			if ( scale._max - scale._min < 1.0 )
			{
				if ( scale._maxAuto )
					scale._max = scale._min + 0.5;
				else
					scale._min = scale._max - 0.5;
			}
			else
			{
				// Calculate the new step size
				if ( scale._majorStepAuto )
				{
					// Calculate the step size based on targetSteps
					scale._majorStep = Scale.CalcStepSize( scale._max - scale._min,
						( scale._ownerAxis is XAxis || scale._ownerAxis is X2Axis ) ?
								Default.TargetXSteps : Default.TargetYSteps );
					if ( scale.IsPreventLabelOverlap )
					{
						// Calculate the maximum number of labels
						double maxLabels = (double) scale.CalcMaxLabels( g, pane, scaleFactor );
						// Calculate a step size based on the width of the labels
						double tmpStep = Math.Ceiling( ( scale._max - scale._min ) / maxLabels );
						// Use the greater of the two step sizes
						if ( tmpStep > scale._majorStep )
							scale._majorStep = tmpStep;
					}
				}
				scale._majorStep = (int)scale._majorStep;
				if ( scale._majorStep < 1.0 )
					scale._majorStep = 1.0;
				// Calculate the new minor step size
				if ( scale._minorStepAuto )
					scale._minorStep = Scale.CalcStepSize( scale._majorStep,
						( scale._ownerAxis is XAxis || scale._ownerAxis is X2Axis ) ?
								Default.TargetMinorXSteps : Default.TargetMinorYSteps );
				if ( scale._minAuto )
					scale._min -= 0.5;
				if ( scale._maxAuto )
					scale._max += 0.5;
			}
		}
	#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 OrdinalScale( 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" );
		}
		/// 
		/// 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 );
		}
	#endregion
	}
}