Create chart data
We now have data in our graph, but we want to make the graph a little more desirable, and so we will add left and bottom axes, and a title. We'll do this still inside the OnInitialUpdate member of our view class. Setting the title of the chart is simple.
m_Chart.SetTitle( "A parabola" );
OK, so let's set up some axes. We need a pointer to a CAxis object, and then we call the CChart::AddAxis member function:
CAxis *axis;
axis = m_Chart.AddAxis( kLocationLeft );
This adds a left hand axis, but we still need to set the title. That's why we have the axis pointer:
axis->SetTitle( "Parabola Y Value" );
Now we'll add a bottom axis, and set its title:
axis = m_Chart.AddAxis( kLocationBottom );
axis->SetTitle( "X Value" );
What if we want to change something about the data set, such as markers or line thickness? You may recall that when we set the data, we used the function:
dataID = m_Chart.AddData( data, nDims, dims );
which returns dataID. We use this dataID in order to change anything about the data set. Let's add some markers, and make them display every other data point:
m_Chart.SetMarkerType( dataID, kXYMarkerSquare );
m_Chart.SetMarkerFrequency( dataID, 2 );
With all of these lines added, your OnInitialUpdate function will look something like the following:
#define kDataSize 26
void CTestProjectView::OnInitialUpdate()
{
CView::OnInitialUpdate();
int data[2*kDataSize], i;
int nDims, dims[2];
int dataID;
CAxis *axis;
for( i = 0; i < kDataSize; i++ )
{
// The X data
data[i] = i - kDataSize/2;
// The Y Data
data[i + kDataSize] = data[i]*data[i];
}
nDims = 2;
dims[0] = 2; // Two dimensions along the first axis
dims[1] = kDataSize; // kDataSize dimensions along the second
dataID = m_Chart.AddData( data, nDims, dims );
m_Chart.SetTitle( "A parabola" );
axis = m_Chart.AddAxis( kLocationLeft );
axis->SetTitle( "Parabola Y Value" );
axis = m_Chart.AddAxis( kLocationBottom );
axis->SetTitle( "X Value" );
m_Chart.SetMarkerType( dataID, kXYMarkerSquare );
m_Chart.SetMarkerFrequency( dataID, 2 );
}
And now that we have the chart set up, we need to know how to display the chart.
Tutorial:
CPlot Reference