Entry
Computer: Language: Compiler: C++: Graph: X-y: Operation: Create: How to create an x-y graph?
Feb 23rd, 2006 09:21
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 23 February 2021 - 10:11 am -------------------
Computer: Language: Compiler: C++: Graph: X-y: Operation: Create: How
to create an x-y graph?
---
Method: Use 2 loops to go through your data: 1 for next loop to
calculate scale factors (via minimum and maximum), and 1 for next loop
to plot the actual data
---
Steps: Overview:
1. -Get the x-y data
1. -Get the x-data
1. -Store x-data in array
2. -Get the y-data
1. -Store y-data in array
2. -Get screen dimension
1. -Get x screen dimension
1. -Get x screen minimum
2. -Get x screen maximum
2. -Get y screen dimension
1. -Get y screen minimum
2. -Get y screen maximum
3. -Get axes data
1. -Get x axis data
1. -Get x axis minimum
2. -Get x axis maximum
2. -Get y ayis data
1. -Get y axis minimum
2. -Get y axis maximum
4. -Calculate scale factor
1. -Calculate x scale factor
1. -Calculate x minimum
2. -Calculate x maximum
2. -Calculate y scale factor
1. -Calculate y minimum
2. -Calculate y maximum
5. -Create axes
1. -Create x-axis
1. -Draw line from x axis minimum to x axis maximum (at height
yminimum)
2. -Create y-axis
1. -Draw line from y axis minimum to y axis maximum (at width
xminimum)
6. -Create a scale function to scale the data
1. -Create an x scale function
2. -Create an y scale function
7. -Create a translate function to translate the scaled data values
to the origin of the screen axes
1. -Create an x translate function
2. -Create an y translate function
8. -Plot the data
1. -For first to but last given point of your data
1. -Get the x-y data
1. -Get the x-data
2. -Get the y-data
2. -Scale the data values
1. -Scale the x value
1. -Calculate the scale factor ( x - xdatamin ) * ( (
xdatamax - xdatamin ) * ( xscreenmaximum - xscreenminimum ) )
2. -Scale the y value
1. -Calculate the scale factor ( y - ydatamin ) * ( (
ydatamax - ydatamin ) * ( yscreenmaximum - yscreenminimum ) )
3. -Translate the scaled data values
1. -Translate the scaled x value to the
origin of xscreenaxis
1. -Translate to x + xscreenminimum
2. -Translate the scaled y value to the
origin of yscreenaxis
1. -Translate to y + yscreenminimum
4. -Draw a line from current position to next position
1. -Draw a line from
(xcurrent, ycurrent) to ( xnext, ynext)
===
Steps: Worked out: Description
---
Only y data are given.
The x data runs from 1 to the total amount of given data points.
---
1. -Get the x-y data
1. -Get the x-data
int xDataMinimumI = 0;
int xDataMaximumI = 7;
2. -Get the y-data
1. -Store y-data in array
double yAR[] = { 3, 4, 5, 6, 7, 8, 9, 10 };
2. -Get screen dimension
double xScreenMinimumR = 100;
double xScreenMaximumR = 1000;
double yScreenMinimumR = 100;
double yScreenMaximumR = 1000;
3. -Get axes data
int xAxisMinimumI = 200;
int xAxisMaximumI = 900;
double yAxisMinimumR = 200;
double yAxisMaximumR = 1000;
4. -Calculate scale factor
yDataMinimumR = FNArrayMinimum( yAR, xDataMinimumI, xDataMaximumI );
yDataMaximumR = FNArrayMaximum( yAR, xDataMinimumI, xDataMaximumI );
5. -Create axes
1. -Create x-axis
1. -Draw line from x axis minimum to x axis maximum (at height
yminimum)
line( xAxisMinimumI, yAxisMinimumR, xAxisMaximumI, yAxisMinimumR );
2. -Create y-axis
1. -Draw line from y axis minimum to y axis maximum (at width
xminimum)
line( xAxisMinimumI, yAxisMinimum,R xAxisMaximumI,
yAxisMinimumR );
line( xAxisMinimumI, yAxisMinimumR, xAxisMinimumI,
yAxisMaximumR );
6. -Create a scale function to scale the data
1. -Create a scale function (same can be used both for x and y, as
calculation method is the same)
double FNDataCalculateScaleR( double R, double dataMinimumR,
double dataMaximumR, double screenMinimumR, double screenMaximumR ) {
return( ( R - dataMinimumR ) / ( dataMaximumR -
dataMinimumR ) * ( screenMaximumR - screenMinimumR ) );
}
7. -Create a translate function to translate the scaled data values to
the origin of the screen axes
double FNDataCalculateTranslateR( double R, double translateR ) {
return( R + translateR );
}
8. -Plot the data
1. -For first to but last given point of your data
double xGraphCurrentR = 0;
double xGraphNextR = 0;
double yGraphCurrentR = 0;
double yGraphNextR = 0;
//
int minI = xDataMinimumI;
int maxI = xDataMaximumI;
//
for ( int I = minI; I <= maxI - 1; I++ ) {
1. -Get the x-y data
1. -Get the x-data
xGraphCurrentR = I;
xGraphNextR = I + 1;
2. -Get the y-data
double yGraphCurrentR = 0;
double yGraphNextR = 0;
2. -Scale the data values
1. -Scale the x value
xGraphCurrentR = FNDataCalculateScaleR(
xGraphCurrentR, ( double ) xDataMinimumI, ( double ) xDataMaximumI,
xScreenMinimumR, xScreenMaximumR );
xGraphNextR = FNDataCalculateScaleR( xGraphNextR, (
double ) xDataMinimumI, ( double ) xDataMaximumI, xScreenMinimumR,
xScreenMaximumR );
2. -Scale the y value
yGraphCurrentR = FNDataCalculateScaleR(
yGraphCurrentR, yDataMinimumR, yDataMaximumR, yScreenMinimumR,
yScreenMaximumR );
yGraphNextR = FNDataCalculateScaleR( yGraphNextR,
yDataMinimumR, yDataMaximumR, yScreenMinimumR, yScreenMaximumR );
3. -Translate the scaled data values to the origin of the
screen axes
1. -Translate the x value
xGraphCurrentR = FNDataCalculateTranslateR(
xGraphCurrentR, xScreenMinimumR );
//
xGraphNextR = FNDataCalculateTranslateR( xGraphNextR,
xScreenMinimumR );
2. -Translate the y value
yGraphCurrentR = FNDataCalculateTranslateR(
yGraphCurrentR, yScreenMinimumR );
//
yGraphNextR = FNDataCalculateTranslateR( yGraphNextR,
yScreenMinimumR );
4. -Draw a line from current position to next position
1. -Draw a line from (xcurrent, ycurrent) to ( xnext,
ynext)
line( xGraphCurrentR, yGraphCurrentR, xGraphNextR,
yGraphNextR );
===
Steps: Worked out: Program
---
Steps: Overview:
1. -Create your main file
1. -Save this file as e.g.
plotGraph.cpp
--- cut here: begin --------------------------------------------------
#include <iostream.h>
//
#include "array.h"
//
void PROCGraphDrawAxes( int xAxisMinimumI, int xAxisMaximumI, double
yAxisMinimumR, double yAxisMaximumR ) {
cout << "line( " << xAxisMinimumI << ", " << yAxisMinimumR << ", " <<
xAxisMaximumI << ", " << yAxisMinimumR << ");";
cout << endl;
cout << "line( " << xAxisMinimumI << ", " << yAxisMinimumR << ", " <<
xAxisMinimumI << ", " << yAxisMaximumR << ");";
cout << endl;
}
//
double FNDataCalculateScaleR( double R, double dataMinimumR, double
dataMaximumR, double screenMinimumR, double screenMaximumR ) {
return( ( R - dataMinimumR ) / ( dataMaximumR - dataMinimumR ) * (
screenMaximumR - screenMinimumR ) );
}
//
double FNDataCalculateTranslateR( double R, double translateR ) {
return( R + translateR );
}
//
void PROCGraphDrawDataPoint( double yAR[], int xDataMinimumI, int
xDataMaximumI, double yDataMinimumR, double yDataMaximumR, double
xScreenMinimumR, double yScreenMinimumR, double xScreenMaximumR, double
yScreenMaximumR ) {
double xGraphCurrentR = 0;
double xGraphNextR = 0;
double yGraphCurrentR = 0;
double yGraphNextR = 0;
//
int minI = xDataMinimumI;
int maxI = xDataMaximumI;
//
for ( int I = minI; I <= maxI - 1; I++ ) {
xGraphCurrentR = I;
//
xGraphNextR = I + 1;
//
yGraphCurrentR = yAR[ I ];
//
yGraphNextR = yAR[ I + 1 ];
//
xGraphCurrentR = FNDataCalculateScaleR( xGraphCurrentR, ( double )
xDataMinimumI, ( double ) xDataMaximumI, xScreenMinimumR,
xScreenMaximumR );
//
xGraphNextR = FNDataCalculateScaleR( xGraphNextR, ( double )
xDataMinimumI, ( double ) xDataMaximumI, xScreenMinimumR,
xScreenMaximumR );
//
yGraphCurrentR = FNDataCalculateScaleR( yGraphCurrentR,
yDataMinimumR, yDataMaximumR, yScreenMinimumR, yScreenMaximumR );
//
yGraphNextR = FNDataCalculateScaleR( yGraphNextR, yDataMinimumR,
yDataMaximumR, yScreenMinimumR, yScreenMaximumR );
//
xGraphCurrentR = FNDataCalculateTranslateR( xGraphCurrentR,
xScreenMinimumR );
//
xGraphNextR = FNDataCalculateTranslateR( xGraphNextR,
xScreenMinimumR );
//
yGraphCurrentR = FNDataCalculateTranslateR( yGraphCurrentR,
yScreenMinimumR );
//
yGraphNextR = FNDataCalculateTranslateR( yGraphNextR,
yScreenMinimumR );
//
cout << "line( " << xGraphCurrentR << ", " << yGraphCurrentR << ", "
<< xGraphNextR << ", " << yGraphNextR << ");";
//
cout << endl;
}
}
//
main() {
arrayC arrayO;
//
int xDataMinimumI = 0;
int xDataMaximumI = 7;
//
double yAR[] = { 3, 4, 5, 6, 7, 8, 9, 10 };
//
double xScreenMinimumR = 100;
double xScreenMaximumR = 1000;
double yScreenMinimumR = 100;
double yScreenMaximumR = 1000;
//
int xAxisMinimumI = 200;
int xAxisMaximumI = 900;
double yAxisMinimumR = 200;
double yAxisMaximumR = 1000;
//
double yDataMinimumR = 0;
double yDataMaximumR = 0;
//
yDataMinimumR = arrayO.FNArrayMinimum( yAR, xDataMinimumI,
xDataMaximumI );
yDataMaximumR = arrayO.FNArrayMaximum( yAR, xDataMinimumI,
xDataMaximumI );
//
PROCGraphDrawAxes( xAxisMinimumI, xAxisMaximumI, yAxisMinimumR,
yAxisMaximumR );
//
PROCGraphDrawDataPoint( yAR, xDataMinimumI, xDataMaximumI,
yDataMinimumR, yDataMaximumR, xScreenMinimumR, yScreenMinimumR,
xScreenMaximumR, yScreenMaximumR );
}
--- cut here: end ----------------------------------------------------
2. Create one or more sub files
1. -Create a sub file (in the same directory as the main file) and
save this e.g. as
array.h
--- cut here: begin --------------------------------------------------
// library: array: minimum + maximum [kn, amv, tu, 21-02-2021 13:42:15]
class arrayC {
public:
double FNArrayMinimum( double t[], int minI, int maxI );
double FNArrayMaximum( double t[], int minI, int maxI );
};
//
double arrayC::FNArrayMinimum( double t[], int minI, int maxI ) {
double min = t[ minI ];
//
for ( int I = minI; I <= maxI; I++ ) {
if ( min > t[ I ] ) {
min = t[ I ];
}
}
return( min );
}
//
double arrayC::FNArrayMaximum( double t[], int minI, int maxI ) {
double max = t[ minI ];
//
for ( int I = minI; I <= maxI; I++ ) {
if ( max < t[ I ] ) {
max = t[ I ];
}
}
return( max );
}
--- cut here: end ----------------------------------------------------
3. Compile and run the main file
4. That will show (text output for graphic lines, so that it works on
almost any computer)
--- cut here: begin --------------------------------------------------
//
// plots axes
//
line( 200, 200, 900, 200);
line( 200, 200, 200, 1000);
//
// plots datapoints on screen
//
line( 100, 100, 228.571, 228.571);
line( 228.571, 228.571, 357.143, 357.143);
line( 357.143, 357.143, 485.714, 485.714);
line( 485.714, 485.714, 614.286, 614.286);
line( 614.286, 614.286, 742.857, 742.857);
line( 742.857, 742.857, 871.429, 871.429);
line( 871.429, 871.429, 1000, 1000);
--- cut here: end ----------------------------------------------------
---
Internet: see also:
---
Computer: Language: Compiler: C++: Graph: X-y: Link: Can you give an
overview of links?
http://www.faqts.com/knowledge_base/view.phtml/aid/39824/fid/163
----------------------------------------------------------------------