Objective Chart : PART II Programmer’s Guide : Chapter 25 Custom Drawing on a Chart
Chapter 25 Custom Drawing on a Chart
Adding Custom Text or Graphics
You can add custom text or graphics to a chart created with Objective Grid. For example, the control limits can be drawn on a control chart using the axis scaling of the display. Another example would be drawing a smooth curve from a formula for comparison with observed data without generating hundreds of SRGraphData objects.
Such custom graphics can be added to the OnDraw() function after DrawComponentList(). After the chart has been drawn, the display rectangle and axis scaling parameters are available. If the graph and axis type are fixed, the drawing code can be quite simple.
The code below draws a line indicating the y-axis value of 105. A classic axis with linear scaling is assumed.
 
void CCCodeView::OnDraw(CDC* pDC)
{
CCCodeDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
 
pDoc->m_Graph.DrawComponentList(pDC, this);
 
// add a line to the display
SRGraphDisplay *pD = (SRGraphDisplay *)
pDoc-> m_Graph.GetComponent(0,IDS_SRG_DISPLAYTYPE);
if(pD!=NULL)
{
CPen Pen,*OldPen;
Pen.CreatePen(PS_SOLID,3,CXCLR_BLACK);
OldPen=(CPen *)pDC->SelectObject(&Pen);
CRect DisplayRect = pD->GetDisplayRect();
double ScalingFactor = (double)DisplayRect.Height()/
pD-> GetMaxScaleY();
double dValue=105.;
// assume linear scale
dValue-=pD->GetScaleY().Min();
int y=(int)((double)DisplayRect.bottom
- (ScalingFactor*dValue)+0.5);
pDC->MoveTo(DisplayRect.left,y);
pDC->LineTo(DisplayRect.right,y);
pDC->SelectObject(OldPen);
Pen.DeleteObject();
}
}
The SRGraph functions DrawingUsing() and NotDrawing() are used along with similarly named functions in the component classes to enable the use of the chart and component drawing and positioning functions outside of DrawComponentList() processing.
The following code segment illustrates how these functions can be used to draw a wiget.
SRGraphDisplay *pD =
(SRGraphDisplay *)GetGraph()->GetComponent(0,IDS_SRG_DISPLAYTYPE);
If(pD != NULL)
{
CDC *pDC = GetDC();
GetGraph()->DrawingUsing(pDC, this);
pD->DrawingUsing(pDC, this);
CPoint datapoint= [ ... ]; // omitted for brevity
pD->DrawWiget(datapoint,pStyle);
pD->NotDrawing();
GetGraph()->NotDrawing();
ReleaseDC(pDC);
}