Histogram Chart
The OCHistogram class creates a bar chart that counts the distribution of values in a given set of data.
The Histogram chart performs the data analysis in two stages. During the first stage, data is scanned to determine the overall range of the data values. This range is divided into a number of subdivisions or slots. The number of data slots may be programmed using the SetDataDivisions() function. During the second stage, counters for these slots are incremented once for each data value that falls within a particular slot range.
Using the Histogram Chart
Data is supplied to the chart item by item. First the whole data set or the range of data is sent to the AddToScan() function. This sets up the scale. Then the data is sent one value at a time to the AddData() function which sorts the values into their correct slots. Once this data is scaled and sorted, the chart is drawn in the normal way by calling the DrawComponentList() function via the Paint() function in the OCUTItem class.
Figure 134 and the code example following it show how the histogram chart is used to generate a histogram of the colors in an image stored in a resource bitmap.
Figure 134 – Image analyzed by Histogram chart
BITMAP bitmap;
// create an OCHistogram object somewhere in the app
// load the bitmap to be analyzed
CBitmap bm;
CDC mdc; //=GetDesktopWindow()->GetDC();
CBitmap *pOldbm=(CBitmap *)mdc.SelectObject(&bm);
CRect r;
int nOldMode=pDC->SetStretchBltMode(COLORONCOLOR);
pDC->StretchBlt(0, 0, r.Width(), r.Height(), &mdc, 0, 0, bitmap.bmWidth,
bitmap.bmHeight, SRCCOPY);
// set up the Histogram chart
// this sets up the data range.
// this method is used because we know the range to expect
// it is also considerably faster than the full scan
// this does the real data
for(int y=0;y<bitmap.bmHeight;y++)
for(int x=0;x<bitmap.bmWidth;x++)
COLORREF color=mdc.GetPixel(x,y);
m_Histogram.AddData((double)( color & 0xff));
// display the bar chart