The following code demonstrate the usage of Curve copy constructor to copy a sub range of the Active Dataset, thus avoiding the range selection complication that might arise with SetLowerBound etc calls. Please note that I have merely used the existing fitpoly function, but a more extensive linear fit function with all the needed statistics results will be needed later to improve this. We are planning to provide much of these LLOC (low level OC) functions in future releases.
/**
linear fit to active data between specified range by row numbers, a red fit curve is added to the graph
Parameters:
nLower = lower index to start from or -1 to start from the lower bound.
nUpper = upper index to end at, or -1 to end at the upper bound.
nFitXRange = -1 to fill entire X range, 0 to be from x1 to x2, >0 percent extra outside x1 x2
*/
void fitlr(int nLower = -1, int nUpper = -1, int nFitXRange = 0)
{
string strCuvName = Project.ActiveCurveBase().GetName();
if(strCuvName.IsEmpty())
return;
GraphLayer glyr = Project.ActiveLayer();
if(glyr == NULL)
return;// we only will fit a graph
int nNumMissingInCopy, nSrcOffset;
// create a temp copy of Active curve to ensure data type is double and original range is not modified
Curve cc(Project.ActiveCurveBase(), nNumMissingInCopy, nSrcOffset,
CURVECOPY_SCAN_OVER_MISSING_FROM_LEFT | CURVECOPY_SCAN_OVER_MISSING_FROM_RIGHT | CURVECOPY_SKIP_MISSING_INSIDE,
nLower, nUpper);
double coeff[2];
fitpoly(cc, 1, coeff); // using built-in simple fitting function, sorry, no stats report
// generate fit curve
double x1, x2;
if(nFitXRange < 0) // span entire x axis
{
x1 = glyr.X.From;
x2 = glyr.X.To;
}
else // use data x range, with possible wider X range
{
x1 = Curve_x(&cc, 0);
x2 = Curve_x(&cc, cc.GetUpperIndex());
double dx = (x2-x1) * nFitXRange/100.0;
x1 -= dx;
x2 += dx;
}
Worksheet wksFit;
wksFit.Create(NULL, CREATE_HIDDEN);
wksFit.SetSize(-1,0);// empty all cols
wksFit.AddCol("FitX");
wksFit.AddCol("FitY");
wksFit.Columns(0).SetType(OKDATAOBJ_DESIGNATION_X);
wksFit.Columns(1).SetType(OKDATAOBJ_DESIGNATION_Y);
Dataset fitx(wksFit, 0);
Dataset fity(wksFit, 1);
int nFitPts = 20;// generate fit curve of 20 pts
fitx.SetSize(nFitPts);
fity.SetSize(nFitPts);
vector vData;
vData.Data(0,nFitPts-1); // Generate 0, 1,2,... nFitPts
fitx = x1 + vData * (x2-x1) / (nFitPts - 1);
fity = coeff[0] + coeff[1] * fitx;
Curve cfit(wksFit, 0, 1);
add_curve_to_graph_layer(cfit, glyr, 1, false, IDM_PLOT_LINE);
}
CP
Edited by - cpyang on 12/26/2003 4:42:18 PM