Now we need to make some data to put in our chart. CChartData::SetData talks some about the format of data in an XY chart. In review, all the data is stored in a one-dimensional array, and all the X elements are stored followed by all the Y elements. So, if we want to make an array, say of a parabola, we might do something like the following:
#define kDataSize 26 int data[2*kDataSize], i; int nDims, dims[2]; int dataID; for( i = 0; i < kDataSize; i++ ) { // The X data data[i] = i - kDataSize/2; // The Y Data data[i + kDataSize] = data[i]*data[i]; }We need to set up the dimensions of our arrays, as well, since when we set up the data, we need to know this:
nDims = 2; dims[0] = 2; // Two dimensions along the first axis dims[1] = kDataSize; // kDataSize dimensions along the secondFinally, we add this data to our chart. We might do this inside the OnInitialUpdate member of the View (in a real application it would be done elsewhere, probably)
dataID = m_Chart.AddData( data, nDims, dims );At the end, our OnInitialUpdate function might look something like the following:
#define kDataSize 26 void CTestProjectView::OnInitialUpdate() { CView::OnInitialUpdate(); int data[2*kDataSize], i; int nDims, dims[2]; int dataID; 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 ); }But we aren't finished. Although the chart will draw just fine like this, it will not have any axes, or titles, or anything on it. We will want to add those in the next step.