LabelAxis.cs

Go to the documentation of this file.
00001 /*
00002 NPlot - A charting library for .NET
00003 
00004 LabelAxis.cs
00005 Copyright (C) 2003
00006 Matt Howlett
00007 
00008 Redistribution and use of NPlot or parts there-of in source and
00009 binary forms, with or without modification, are permitted provided
00010 that the following conditions are met:
00011 
00012 1. Re-distributions in source form must retain at the head of each
00013    source file the above copyright notice, this list of conditions
00014    and the following disclaimer.
00015 
00016 2. Any product ("the product") that makes use NPlot or parts 
00017    there-of must either:
00018   
00019     (a) allow any user of the product to obtain a complete machine-
00020         readable copy of the corresponding source code for the 
00021         product and the version of NPlot used for a charge no more
00022         than your cost of physically performing source distribution,
00023         on a medium customarily used for software interchange, or:
00024 
00025     (b) reproduce the following text in the documentation, about 
00026         box or other materials intended to be read by human users
00027         of the product that is provided to every human user of the
00028         product: 
00029    
00030               "This product includes software developed as 
00031               part of the NPlot library project available 
00032               from: http://www.nplot.com/" 
00033 
00034         The words "This product" may optionally be replace with 
00035         the actual name of the product.
00036 
00037 ------------------------------------------------------------------------
00038 
00039 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00040 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00041 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00042 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00043 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00044 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00045 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00046 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00047 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00048 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00049 
00050 */
00051 
00052 using System;
00053 using System.Collections;
00054 using System.Drawing;
00055 
00056 namespace NPlot
00057 {
00058 
00063         public class LabelAxis : Axis
00064         {
00065 
00070                 public override object Clone()
00071                 {
00072                         LabelAxis a = new LabelAxis();
00073                         // ensure that this isn't being called on a derived type. If it is, then oh no!
00074                         if (this.GetType() != a.GetType())
00075                         {
00076                                 throw new NPlotException( "Error. Clone method is not defined in derived type." );
00077                         }
00078                         DoClone( this, a );
00079                         return a;
00080                 }
00081 
00087                 protected static void DoClone( LabelAxis b, LabelAxis a )
00088                 {
00089                         Axis.DoClone( b, a );
00090 
00091                         a.labels_ = (ArrayList)b.labels_.Clone();
00092                         a.numbers_ = (ArrayList)b.numbers_.Clone();
00093 
00094                         a.ticksBetweenText_ = b.ticksBetweenText_;
00095                         a.sortDataIfNecessary_ = b.sortDataIfNecessary_;
00096                 }
00097 
00098 
00102                 private void Init()
00103                 {
00104                         labels_ = new ArrayList();
00105                         numbers_ = new ArrayList();
00106                 }
00107 
00108 
00114                 public LabelAxis( Axis a )
00115                         : base( a )
00116                 {
00117                         Init();
00118                 }
00119 
00123                 public LabelAxis()
00124                         : base()
00125                 {
00126                         Init();
00127                 }
00128 
00134                 public LabelAxis( double worldMin, double worldMax )
00135                         : base( worldMin, worldMax )
00136                 {
00137                         Init();
00138                 }
00139 
00140 
00146                 public void AddLabel( string name, double val )
00147                 {
00148                         labels_.Add( name );
00149                         numbers_.Add( val );
00150                 }
00151                 
00152 
00162                 protected override void DrawTicks( 
00163                         Graphics g, 
00164                         Point physicalMin, 
00165                         Point physicalMax, 
00166                         out object labelOffset,
00167                         out object boundingBox )
00168                 {
00169 
00170                         Point tLabelOffset;
00171                         Rectangle tBoundingBox;
00172 
00173                         labelOffset = this.getDefaultLabelOffset( physicalMin, physicalMax );
00174                         boundingBox = null;
00175 
00176                         // draw the tick labels (but not the ticks).
00177                         PointF lastPos = WorldToPhysical( (double)numbers_[0], physicalMin, physicalMax, true );
00178                         for (int i=0; i<labels_.Count; ++i)
00179                         {
00180                                 
00181                                 if ((double)numbers_[i] > WorldMin && (double)numbers_[i] < WorldMax)
00182                                 {
00183                                         
00184                                         // check to make sure labels are far enough appart.
00185                                         PointF thisPos = WorldToPhysical( (double)numbers_[i], physicalMin, physicalMax, true );
00186                                         float dist = Utils.Distance( thisPos, lastPos );
00187 
00188                                         if ( i==0 || (dist > this.PhysicalSpacingMin) )
00189                                         {
00190                                                 lastPos = thisPos;
00191                                                 
00192                                                 this.DrawTick( g, (double)numbers_[i], 0, 
00193                                                         (string)labels_[i],
00194                                                         new Point(0,0), 
00195                                                         physicalMin, physicalMax,
00196                                                         out tLabelOffset, out tBoundingBox );
00197                                         
00198                                                 Axis.UpdateOffsetAndBounds( 
00199                                                         ref labelOffset, ref boundingBox, 
00200                                                         tLabelOffset, tBoundingBox );
00201                                         }
00202                                 }
00203                         }
00204 
00205                         // now draw the ticks (which might not be aligned with the tick text).
00206                         ArrayList largeTickPositions;
00207                         ArrayList smallTickPositions;
00208                         WorldTickPositions_FirstPass( physicalMin, physicalMax, out largeTickPositions, out smallTickPositions );
00209                         lastPos = WorldToPhysical( (double)largeTickPositions[0], physicalMin, physicalMax, true );
00210                         for (int i=0; i<largeTickPositions.Count; ++i)
00211                         {
00212                                 double tickPos = (double)largeTickPositions[i];
00213 
00214                                 // check to see that labels are far enough appart. 
00215                                 PointF thisPos = WorldToPhysical( tickPos, physicalMin, physicalMax, true );
00216                                 float dist = Utils.Distance( thisPos, lastPos );
00217                                 if ( (i==0) || (dist> this.PhysicalSpacingMin) )
00218                                 {
00219                                         lastPos = thisPos;
00220 
00221                                         this.DrawTick( g, tickPos, LargeTickSize, 
00222                                                 "",
00223                                                 new Point(0,0), 
00224                                                 physicalMin, physicalMax,
00225                                                 out tLabelOffset, out tBoundingBox );
00226                                         
00227                                         Axis.UpdateOffsetAndBounds( 
00228                                                 ref labelOffset, ref boundingBox, 
00229                                                 tLabelOffset, tBoundingBox );
00230                                 }
00231                         }
00232 
00233                 }
00234 
00235 
00245                 internal override void WorldTickPositions_FirstPass(
00246                         Point physicalMin, 
00247                         Point physicalMax,
00248                         out ArrayList largeTickPositions,
00249                         out ArrayList smallTickPositions
00250                         )
00251                 {
00252                         smallTickPositions = null;
00253                         largeTickPositions = new ArrayList();
00254 
00255                         // if ticks correspond to position of text
00256                         if (!ticksBetweenText_)
00257                         {
00258                                 for (int i=0; i<labels_.Count; ++i)
00259                                 {
00260                                         if ((double)numbers_[i] > WorldMin && (double)numbers_[i] < WorldMax)
00261                                         {
00262                                                 largeTickPositions.Add( numbers_[i] );
00263                                         }
00264                                 }
00265                         }
00266 
00267                         // if ticks correspond to gaps between text
00268                         else
00269                         {
00270                                 ArrayList numbers_copy;
00271                                 if (sortDataIfNecessary_)
00272                                 {
00273                                         numbers_copy = (ArrayList)numbers_.Clone(); // shallow copy.
00274                                         numbers_copy.Sort();
00275                                 }
00276                                 else
00277                                 {
00278                                         numbers_copy = numbers_;
00279                                 }
00280 
00281                                 for (int i=1; i<labels_.Count; ++i)
00282                                 {
00283                                         double worldPosition = ((double)numbers_copy[i] + (double)numbers_copy[i-1])/2.0;
00284                                         if (worldPosition > WorldMin && worldPosition < WorldMax)
00285                                         {
00286                                                 largeTickPositions.Add( worldPosition );
00287                                         }
00288                                 }
00289                         }
00290 
00291                 }
00292 
00293 
00298                 public bool TicksBetweenText
00299                 {
00300                         get
00301                         {
00302                                 return ticksBetweenText_;
00303                         }
00304                         set
00305                         {
00306                                 ticksBetweenText_ = value;
00307                         }
00308                 }
00309                 private bool ticksBetweenText_ = false;
00310 
00311 
00321                 public bool SortDataIfNecessary
00322                 {
00323                         get
00324                         {
00325                                 return sortDataIfNecessary_;
00326                         }
00327                         set
00328                         {
00329                                 sortDataIfNecessary_ = value;
00330                         }
00331                 }
00332                 private bool sortDataIfNecessary_ = true;
00333 
00334 
00339                 public int PhysicalSpacingMin
00340                 {
00341                         get
00342                         {
00343                                 return physicalSpacingMin_;
00344                         }
00345                         set
00346                         {
00347                                 physicalSpacingMin_ = value;
00348                         }
00349                 }
00350                 private int physicalSpacingMin_ = 0;
00351 
00352 
00353                 private ArrayList labels_;
00354                 private ArrayList numbers_;
00355         }
00356 }

Generated on Sat Nov 5 01:04:06 2005 for NPlot by  doxygen 1.4.5