home *** CD-ROM | disk | FTP | other *** search
- { Unit : PLOTDATA
-
- Utilization notes:
-
- 1. For informational purposes only, there is a 4-pixel
- border between the edge of a plot viewport and the
- actual area occupied by the plot.
-
- }
- { Specification for Plot class ****************************
-
- Variables:
- x1 : integer;
- Coordinate (global) of the left edge of the
- Plot object's viewport.
-
- x2 : integer;
- Coordinate (global) of the right edge of the
- Plot object's viewport.
-
- y1 : integer;
- Coordinate (global) of the top edge of the
- Plot object's viewport.
-
- y2 : integer;
- Coordinate (global) of the bottom edge of the
- Plot object's viewport.
-
- Mnx : real;
- Minimum x axis scale value.
-
- Mny : real;
- Minimum y axis scale value.
-
- Mxx : real;
- Maximum y axis scale value.
-
- Mxy : real;
- Maximum y axis scale value.
-
- XLabel : string;
- Label for x axis.
-
- YLabel : string;
- Label for y axis.
-
- OldPoint : Point;
- Object type that stores the local viewport
- coordinates of the last point plotted.
- Set to (0,0,FALSE) during initialization.
-
- OriginX : integer;
- Viewport x coord of origin.
-
- OriginY : integer;
- Viewport y coord of origin.
-
- MaxXAxis : integer;
- Viewport x coordinate of the maximum x-axis
- endpoint.
-
- MaxYAxis : integer;
- Viewport y coordinate of the maximum y-axis
- endpoint.
-
- DeltaXAxis : integer;
- Equal to MaxXAxis - OriginX.
-
- DeltaYAxis : integer;
- Equal to OriginY - MaxYAxis.
-
- DeltaX : real;
- Equal to Mxx - Mnx.
-
- DeltaY : real;
- Equal to Mxy - Mny.
-
- Procedures:
- .Init( miny, maxy, minx, maxx : real;
- lft, rt, tp, btm: integer;
- xlbl, ylbl : string );
- Store all viewport parameters, maxima and
- minima for both axes. Calculate all "deltas,"
- set viewport with border, draw axes with
- labels and maximum and minimum scale values.
-
- .AddPoint( x, y : real );
- Figure out where in the plot area the new point
- is located, and draw a line between the point
- stored in OldPoint and the new point.
-
- Descendant object types:
- None. }
-
- unit PlotData;
-
- interface
-
- uses Graph, Crt, Points;
-
- const
-
- LBuf = 4;
- RBuf = 4;
- TBuf = 4;
- BBuf = 4;
- left = 3;
- right = 1;
- top = 0;
- bottom = 2;
-
- type
-
- Plot = object
- x1, x2, y1, y2 : integer; { viewport left, right, top, bottom }
- Mnx, Mny, Mxx, Mxy : real; { min/max x min/max y }
- XW,XD,YW,YD : integer;
- XSFac, YSFac : real;
- XLabel : string;
- YLabel : string;
- PlotTitle : string;
- OldPoint : Point; { last point plotted }
- OriginX : integer; { viewport x coord of origin }
- OriginY : integer; { viewport y coord of origin }
- MaxXAxis : integer;
- MaxYAxis : integer;
- DeltaXAxis : integer;
- DeltaYAxis : integer;
- DeltaX : real;
- DeltaY : real;
- PlotEnabled : boolean;
- procedure Init( miny, maxy, ysf, minx, maxx, xsf : real;
- lft, rt, tp, btm, wx, dx, wy, dy : integer;
- ptitle, xlbl, ylbl : string;
- penbl : boolean );
- procedure AddPoint( x, y : real );
- procedure DrawHGridLine( y : real );
- procedure DrawVGridLine( x : real );
- procedure PlaceXAxisValue( r : real );
- procedure PlaceYAxisValue( r : real );
- procedure DrawTickMark( x, y : integer );
- end;
-
-
-
- implementation
-
- procedure Plot.Init( miny, maxy, ysf, minx, maxx, xsf : real;
- lft, rt, tp, btm, wx, dx, wy, dy : integer;
- ptitle, xlbl, ylbl : string;
- penbl : boolean );
- var
-
- LBorder, THeight : integer;
- VP : ViewPortType;
- TST : TextSettingsType;
- s : array[0..3] of string[11];
- begin
- XW := wx; XD := dx;
- YW := wy; YD := dy;
- XSFac := xsf;
- YSFac := ysf;
- Xlabel := xlbl;
- Ylabel := ylbl;
- PlotTitle := ptitle;
- x1 := lft;
- x2 := rt;
- y1 := tp;
- y2 := btm;
- Mnx := minx*xsf;
- Mxx := maxx*xsf;
- Mny := miny*ysf;
- Mxy := maxy*ysf;
- DeltaX := Mxx - Mnx;
- DeltaY := Mxy - Mny;
- Str( maxx:XW:XD, s[right]);
- Str( maxy:YW:YD, s[top]);
- Str( minx:XW:XD, s[left]);
- Str( miny:YW:YD, s[bottom]);
- THeight := TextHeight(s[0]);
- if TextWidth( s[top] ) < TextWidth( s[bottom]) then
- LBorder := TextWidth(s[bottom])
- else
- LBorder := TextWidth(s[top]);
-
- SetViewPort( lft, tp, rt, btm, true);
- if GraphResult = grError then
- OutTextXY( 0,0,'Input error to SetViewPort in Plot.Init' );
- GetViewSettings( VP );
- with VP do
- begin
- SetFillStyle( SolidFill, blue );
- Rectangle(0,0,x2-x1, y2-y1);
- FloodFill( 1,1, GetMaxColor );
-
- OriginY := y2-y1-2*(BBuf+THeight);
- OriginX := LBuf+LBorder+THeight;
- MaxYAxis := TBuf+(3*THeight div 2);
- MaxXAxis := x2-x1-RBuf-(TextWidth(s[right]) div 2);
- DeltaXAxis := MaxXAxis - OriginX;
- DeltaYAxis := OriginY - MaxYAxis;
-
- GetTextSettings( TST );
- with TST do
- begin
- Line( OriginX, MaxYAxis, OriginX, OriginY);
- SetTextJustify(RightText, BottomText);
- MoveTo(OriginX, OriginY);
- OutText(s[bottom]);
- MoveTo(OriginX,TBuf+(2*THeight));
- OutText(s[top]);
-
- Line( OriginX, OriginY,MaxXAxis,OriginY);
- SetTextJustify(CenterText, BottomText);
- MoveTo(OriginX, y2-y1-BBuf-THeight);
- OutText(s[left]);
- MoveTo( MaxXAxis, y2-y1-BBuf-THeight);
- OutText(s[right]);
-
- MoveTo(OriginX+DeltaXAxis div 2, y2-y1-BBuf);
- OutText( XLabel );
- SetTextStyle( Font, VertDir, CharSize );
- MoveTo( LBuf+(THeight div 2),
- OriginY-(DeltaYAxis div 2) );
- SetTextJustify( CenterText, CenterText );
- OutText(YLabel);
- SetTextStyle( Font, Direction, CharSize );
- MoveTo( (x2-x1) div 2, TBuf+(THeight div 2));
- OutText(PlotTitle);
- SetTextJustify( Horiz, Vert );
- end;
- end;
- OldPoint.Init(OriginX,OriginY);
- PlotEnabled := PEnbl;
- end;
-
- procedure Plot.AddPoint( x, y : real );
- var
- VP : ViewPortType;
- px, py : integer;
- begin
- GetViewSettings( VP );
- SetViewPort( x1, y1, x2, y2, true );
- px := OriginX+Round(((x-mnx)/DeltaX)*DeltaXAxis);
- py := OriginY-Round(((y-mny)/DeltaY)*DeltaYAxis);
- if (x >= mnx) and
- (x <= mxx) and
- (y >= mny) and
- (y <= mxy) then
- begin
- with OldPoint do
- begin
- if PlotEnabled then
- line( OldPoint.X, OldPoint.Y, px, py );
- OldPoint.Init( px, py );
- PlotEnabled := true;
- end;
- end;
- with VP do
- SetViewPort( x1, y1, x2, y2, true );
- end;
-
- procedure Plot.DrawHGridLine( y : real );
- var
- VP : ViewPortType;
- LST : LineSettingsType;
- py : integer;
- begin
- GetViewSettings( VP );
- SetViewPort( x1, y1, x2, y2, true );
- if (y > mny) and (y < mxy) then
- begin
- py := OriginY-Round(((y-mny)/DeltaY)*DeltaYAxis);
- GetLineSettings( LST );
- with LST do
- begin
- SetLineStyle( DottedLn, Pattern, NormWidth );
- Line( OriginX, py, MaxXAxis, py );
- SetLineStyle( LineStyle, Pattern, Thickness );
- end;
- end;
- with VP do
- SetViewPort( x1, y1, x2, y2, true );
- end;
-
- procedure Plot.DrawVGridLine( x : real );
- var
- VP : ViewPortType;
- LST : LineSettingsType;
- px : integer;
- begin
- GetViewSettings( VP );
- SetViewPort( x1, y1, x2, y2, true );
- if (x > mnx) and (x < mxx) then
- begin
- px := OriginX+Round(((x-mnx)/DeltaX)*DeltaXAxis);
- GetLineSettings( LST );
- with LST do
- begin
- SetLineStyle( DottedLn, Pattern, NormWidth );
- Line( px, OriginY, px, MaxYAxis );
- SetLineStyle( LineStyle, Pattern, Thickness );
- end;
- end;
- with VP do
- SetViewPort( x1, y1, x2, y2, true );
- end;
-
- procedure Plot.DrawTickMark( x, y : integer );
- var
- VP : ViewPortType;
- begin
- GetViewSettings( VP );
- SetViewPort( x1, y1, x2, y2, true );
- PutPixel( x, y, GetColor );
- PutPixel( x+1, y, GetColor );
- PutPixel( x, y+1, GetColor );
- PutPixel( x+1, y+1, GetColor );
- with VP do
- SetViewPort( x1, y1, x2, y2, true );
- end;
-
- procedure Plot.PlaceXAxisValue( r : real );
- var
- VP : ViewPortType;
- TST : TextSettingsType;
- px : integer;
- s : string;
- begin
- GetViewSettings( VP );
- SetViewPort( x1, y1, x2, y2, true );
- if (r > mnx) and (r < mxx) then
- begin
- GetTextSettings( TST );
- Str( (r/XSFac):XW:XD, s );
- px := OriginX+Round(((r-mnx)/DeltaX)*DeltaXAxis);
- SetTextJustify( CenterText, BottomText);
- MoveTo( px, y2-y1-BBuf-TextHeight(s));
- OutText(s);
- with TST do
- SetTextJustify( Horiz, Vert );
- DrawTickMark( px, OriginY-1 );
- end;
- with VP do
- SetViewPort( x1, y1, x2, y2, true );
- end;
-
- procedure Plot.PlaceYAxisValue( r : real );
- var
- VP : ViewPortType;
- TST : TextSettingsType;
- py : integer;
- s : string;
- begin
- GetViewSettings( VP );
- SetViewPort( x1, y1, x2, y2, true );
- if (r > mny) and (r < mxy) then
- begin
- GetTextSettings( TST );
- Str( (r/YSFac):YW:YD, s );
- py := OriginY-Round(((r-mny)/DeltaY)*DeltaYAxis);
- SetTextJustify( RightText, CenterText);
- MoveTo( OriginX, py );
- OutText(s);
- with TST do
- SetTextJustify( Horiz, Vert );
- DrawTickMark( OriginX+1, py-1 );
- end;
- with VP do
- SetViewPort( x1, y1, x2, y2, true );
- end;
-
- { Initialization code }
- begin
-
- { None }
-
- end.
-
- { Listing 4-1 should be saved and compiled as PLOTDATA.PAS }