//============================================================================ //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 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 //General Public License for more details. // //You should have received a copy of the GNU 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 ZedGraph { /// /// The DateAsOrdinalScale class inherits from the class, and implements /// the features specific to . /// /// DateAsOrdinalScale is an ordinal axis that will have labels formatted with dates from the /// actual data values of the first in the . /// Although the tics are labeled with real data values, the actual points will be /// evenly-spaced in spite of the data values. For example, if the X values of the first curve /// are 1, 5, and 100, then the tic labels will show 1, 5, and 100, but they will be equal /// distance from each other. /// /// /// John Champion /// $Revision: 1.13 $ $Date: 2007-11-28 02:38:22 $ [Serializable] class DateAsOrdinalScale : 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 DateAsOrdinalScale( Axis owner ) : base( owner ) { } /// /// The Copy Constructor /// /// The object from which to copy /// The object that will own the /// new instance of public DateAsOrdinalScale( 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 DateAsOrdinalScale( this, owner ); } #endregion #region properties /// /// Return the for this , which is /// . /// public override AxisType Type { get { return AxisType.DateAsOrdinal; } } /// /// Gets or sets the minimum value for this scale. /// /// /// The set property is specifically adapted for scales, /// in that it automatically limits the value to the range of valid dates for the /// struct. /// public override double Min { get { return _min; } set { _min = XDate.MakeValidDate( value ); _minAuto = false; } } /// /// Gets or sets the maximum value for this scale. /// /// /// The set property is specifically adapted for scales, /// in that it automatically limits the value to the range of valid dates for the /// struct. /// public override double Max { get { return _max; } set { _max = XDate.MakeValidDate( value ); _maxAuto = false; } } #endregion #region methods /// /// Select a reasonable ordinal axis scale given a range of data values, with the expectation that /// dates will be displayed. /// /// /// This method only applies to type axes, and it /// is called by the general method. For this type, /// the first curve is the "master", which contains the dates to be applied. /// 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 ); /* // First, get the date ranges from the first curve in the list double xMin; // = Double.MaxValue; double xMax; // = Double.MinValue; double yMin; // = Double.MaxValue; double yMax; // = Double.MinValue; double range = 1; foreach ( CurveItem curve in pane.CurveList ) { if ( ( _ownerAxis is Y2Axis && curve.IsY2Axis ) || ( _ownerAxis is YAxis && !curve.IsY2Axis ) || ( _ownerAxis is X2Axis && curve.IsX2Axis ) || ( _ownerAxis is XAxis && !curve.IsX2Axis ) ) { curve.GetRange( out xMin, out xMax, out yMin, out yMax, false, pane.IsBoundedRanges, pane ); if ( _ownerAxis is XAxis || _ownerAxis is X2Axis ) range = xMax - xMin; else range = yMax - yMin; } } */ // Set the DateFormat by calling CalcDateStepSize // DateScale.CalcDateStepSize( range, Default.TargetXSteps, this ); SetDateFormat( pane ); // Now, set the axis range based on a ordinal scale base.PickScale( pane, g, scaleFactor ); OrdinalScale.PickScale( pane, g, scaleFactor, this ); } internal void SetDateFormat( GraphPane pane ) { if ( _formatAuto ) { double range = 10; if ( pane.CurveList.Count > 0 && pane.CurveList[0].Points.Count > 1 ) { double val1, val2; PointPair pt1 = pane.CurveList[0].Points[0]; PointPair pt2 = pane.CurveList[0].Points[pane.CurveList[0].Points.Count - 1]; int p1 = 1; int p2 = pane.CurveList[0].Points.Count; if ( pane.IsBoundedRanges ) { p1 = (int) Math.Floor( _ownerAxis.Scale.Min ); p2 = (int) Math.Ceiling( _ownerAxis.Scale.Max ); p1 = Math.Min( Math.Max( p1, 1 ), pane.CurveList[0].Points.Count ); p2 = Math.Min( Math.Max( p2, 1 ), pane.CurveList[0].Points.Count ); if ( p2 > p1 ) { pt1 = pane.CurveList[0].Points[p1-1]; pt2 = pane.CurveList[0].Points[p2-1]; } } if ( _ownerAxis is XAxis || _ownerAxis is X2Axis ) { val1 = pt1.X; val2 = pt2.X; } else { val1 = pt1.Y; val2 = pt2.Y; } if ( val1 != PointPair.Missing && val2 != PointPair.Missing && !Double.IsNaN( val1 ) && !Double.IsNaN( val2 ) && !Double.IsInfinity( val1 ) && !Double.IsInfinity( val2 ) && Math.Abs( val2 - val1 ) > 1e-10 ) range = Math.Abs( val2 - val1 ); } if ( range > Default.RangeYearYear ) _format = Default.FormatYearYear; else if ( range > Default.RangeYearMonth ) _format = Default.FormatYearMonth; else if ( range > Default.RangeMonthMonth ) _format = Default.FormatMonthMonth; else if ( range > Default.RangeDayDay ) _format = Default.FormatDayDay; else if ( range > Default.RangeDayHour ) _format = Default.FormatDayHour; else if ( range > Default.RangeHourHour ) _format = Default.FormatHourHour; else if ( range > Default.RangeHourMinute ) _format = Default.FormatHourMinute; else if ( range > Default.RangeMinuteMinute ) _format = Default.FormatMinuteMinute; else if ( range > Default.RangeMinuteSecond ) _format = Default.FormatMinuteSecond; else if ( range > Default.RangeSecondSecond ) _format = Default.FormatSecondSecond; else // MilliSecond _format = Default.FormatMillisecond; } } /// /// Make a value label for an . /// /// /// A reference to the object that is the parent or /// owner of this object. /// /// /// The zero-based, ordinal index of the label to be generated. For example, a value of 2 would /// cause the third value label on the axis to be generated. /// /// /// The numeric value associated with the label. This value is ignored for log /// () /// and text () type axes. /// /// The resulting value label as a override internal string MakeLabel( GraphPane pane, int index, double dVal ) { if ( _format == null ) _format = Scale.Default.Format; double val; int tmpIndex = (int) dVal - 1; if ( pane.CurveList.Count > 0 && pane.CurveList[0].Points.Count > tmpIndex ) { if ( _ownerAxis is XAxis || _ownerAxis is X2Axis ) val = pane.CurveList[0].Points[tmpIndex].X; else val = pane.CurveList[0].Points[tmpIndex].Y; return XDate.ToString( val, _format ); } else return string.Empty; } #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 DateAsOrdinalScale( 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 } }