00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 using System;
00053 using System.Drawing;
00054 using System.Data;
00055
00056 namespace NPlot
00057 {
00058
00062 public class LabelPointPlot : PointPlot, ISequencePlot
00063 {
00064
00069 private class TextDataAdapter
00070 {
00071
00072 private object data_;
00073 private object dataSource_;
00074 private string dataMember_;
00075
00076 public TextDataAdapter( object dataSource, string dataMember, object data )
00077 {
00078 this.data_ = data;
00079 this.dataSource_ = dataSource;
00080 this.dataMember_ = dataMember;
00081 }
00082
00083 public string this[int i]
00084 {
00085 get
00086 {
00087
00088
00089
00090 if (data_ is string[])
00091 {
00092 return ((string[])data_)[i];
00093 }
00094
00095 if (data_ is string)
00096 {
00097 if (dataSource_ == null)
00098 {
00099 throw new NPlotException( "Error: DataSource null" );
00100 }
00101
00102 System.Data.DataRowCollection rows;
00103
00104 if ( dataSource_ is System.Data.DataSet )
00105 {
00106 if (dataMember_ != null)
00107 {
00108
00109 rows = ((DataTable)((DataSet)dataSource_).Tables[dataMember_]).Rows;
00110 }
00111 else
00112 {
00113
00114 rows = ((DataTable)((DataSet)dataSource_).Tables[0]).Rows;
00115 }
00116 }
00117
00118 else if (dataSource_ is System.Data.DataTable )
00119 {
00120 rows = ((DataTable)dataSource_).Rows;
00121 }
00122
00123 else
00124 {
00125 throw new NPlotException ( "not implemented yet" );
00126 }
00127
00128 return (string)((System.Data.DataRow)(rows[i]))[(string)data_];
00129 }
00130
00131 if (data_ is System.Collections.ArrayList)
00132 {
00133 object dataPoint = ((System.Collections.ArrayList)data_)[i];
00134 if (dataPoint is string)
00135 return (string)dataPoint;
00136 throw new NPlotException( "TextDataAdapter: data not in recognised format" );
00137 }
00138
00139 if (data_ == null)
00140 {
00141 return "text";
00142 }
00143
00144 throw new NPlotException( "Text data not of recognised type" );
00145 }
00146 }
00147
00148
00149 public int Count
00150 {
00151 get
00152 {
00153
00154
00155 if (data_ == null)
00156 {
00157 return 0;
00158 }
00159 if (data_ is string[])
00160 {
00161 return ((string[])data_).Length;
00162 }
00163 if (data_ is System.Collections.ArrayList)
00164 {
00165 return ((System.Collections.ArrayList)data_).Count;
00166 }
00167 throw new NPlotException( "Text data not in correct format" );
00168 }
00169 }
00170
00171 }
00172
00173
00177 public enum LabelPositions
00178 {
00182 Above,
00186 Below,
00190 Left,
00194 Right
00195 }
00196
00197
00201 public LabelPointPlot()
00202 {
00203 }
00204
00205
00210 public LabelPointPlot( Marker marker )
00211 : base( marker )
00212 {
00213 }
00214
00215
00219 public LabelPositions LabelTextPosition
00220 {
00221 get
00222 {
00223 return labelTextPosition_;
00224 }
00225 set
00226 {
00227 labelTextPosition_ = value;
00228 }
00229 }
00230 private LabelPositions labelTextPosition_ = LabelPositions.Above;
00231
00232
00236 public object TextData
00237 {
00238 get
00239 {
00240 return textData_;
00241 }
00242 set
00243 {
00244 textData_ = value;
00245 }
00246 }
00247 object textData_;
00248
00249
00253 public Font Font
00254 {
00255 get
00256 {
00257 return font_;
00258 }
00259 set
00260 {
00261 font_ = value;
00262 }
00263 }
00264 private Font font_ = new Font( "Arial", 8.0f );
00265
00266
00273 public override void Draw( Graphics g, PhysicalAxis xAxis, PhysicalAxis yAxis )
00274 {
00275 SequenceAdapter data =
00276 new SequenceAdapter( this.DataSource, this.DataMember, this.OrdinateData, this.AbscissaData );
00277
00278 TextDataAdapter textData =
00279 new TextDataAdapter( this.DataSource, this.DataMember, this.TextData );
00280
00281
00282
00283 for (int i=0; i<data.Count; ++i)
00284 {
00285 try
00286 {
00287 PointD pt = data[i];
00288 if ( !Double.IsNaN(pt.X) && !Double.IsNaN(pt.Y) )
00289 {
00290 PointF xPos = xAxis.WorldToPhysical( pt.X, false);
00291 PointF yPos = yAxis.WorldToPhysical( pt.Y, false);
00292 Marker.Draw( g, (int)xPos.X, (int)yPos.Y );
00293 if ( textData[i] != "" )
00294 {
00295 SizeF size = g.MeasureString( textData[i], this.Font );
00296 switch (labelTextPosition_)
00297 {
00298 case LabelPositions.Above:
00299 g.DrawString( textData[i], font_, Brushes.Black, new PointF(xPos.X-size.Width/2,yPos.Y-size.Height-Marker.Size*2/3));
00300 break;
00301 case LabelPositions.Below:
00302 g.DrawString( textData[i], font_, Brushes.Black, new PointF(xPos.X-size.Width/2,yPos.Y+Marker.Size*2/3));
00303 break;
00304 case LabelPositions.Left:
00305 g.DrawString( textData[i], font_, Brushes.Black, new PointF(xPos.X-size.Width-Marker.Size*2/3,yPos.Y-size.Height/2));
00306 break;
00307 case LabelPositions.Right:
00308 g.DrawString( textData[i], font_, Brushes.Black, new PointF(xPos.X+Marker.Size*2/3,yPos.Y-size.Height/2));
00309 break;
00310 }
00311 }
00312 }
00313 }
00314 catch
00315 {
00316 throw new NPlotException("Error in TextPlot.Draw");
00317 }
00318 }
00319
00320 }
00321
00322
00323 }
00324 }