void CTChartBoxplot::CalcPercent(CDoubleVector &v, CDoubleVector &w, CIntVector &vecInd) { int i = 0, j = 0; BOOL bRet = FALSE; int k1[DISP_PERCENT], k2[DISP_PERCENT]; double fTc1[DISP_PERCENT], fTc2[DISP_PERCENT]; double fG1[DISP_PERCENT], fG2[DISP_PERCENT]; double fG1Star[DISP_PERCENT], fG2Star[DISP_PERCENT]; int nCol = m_bWeight ? 1 : 0; int nVlen = v.vlen(); if (!m_bSorted) { vecInd.destroy(); v.Sort(vecInd);//排序 } if (m_bCalcPercent) return;//已经计算 //百分数 for (i=0;i<DISP_PERCENT && nVlen < 2;i++) { if (nVlen == 0) { m_fPercentile[i][1] = 0; bRet = TRUE; } else if (nVlen == 1) { m_fPercentile[i][1] = v(0); bRet = TRUE; } } //判断是否返回 if (bRet) return; //存放权重的向量 CDoubleVector wTmp(nVlen,0); if (m_bWeight) { double fSum = v.mean_w(w) * nVlen;//总和 wTmp(0) = w(vecInd(0)-1); //求百分数 for (i=1;i<nVlen;i++) { wTmp(i) = wTmp(i-1) + w(vecInd(i)-1); } for (i=0;i<DISP_PERCENT;i++) { fTc1[i] = fSum * m_fPercentile[i][0]; fTc2[i] = (fSum+1) * m_fPercentile[i][0]; k1[i] = k2[i] = 0; } //初值 for (j=0;j<DISP_PERCENT;j++) { for (i=0;i<nVlen-1;i++) { if (wTmp(i) <= fTc1[j] && wTmp(i+1) > fTc1[j]) { k1[j] = i; break; } } } for (j=0;j<DISP_PERCENT;j++) { for (i=0;i<nVlen-1;i++) { if (wTmp(i) <= fTc2[j] && wTmp(i+1) > fTc2[j]) { k2[j] = i; break; } } } for (j=0;j<DISP_PERCENT;j++) { //fG1Star[j] = fTc1[j] - wTmp(k1[j]); fG2Star[j] = fTc2[j] - wTmp(k2[j]); if (fG2Star[j] >= 1) { m_fPercentile[j][1] = v(k1[j]+1); } else { if (wTmp(vecInd(k2[j]+1)-1) >= 1) { m_fPercentile[j][1] = (1-fG2Star[j])*v(k2[j]) + fG2Star[j]*v(k2[j]+1); } else { //fG1[j] = fG1Star[j]/dataMatrix(0)((vecInd(k1[j]+1)-1)); fG2[j] = fG2Star[j]/w((vecInd(k2[j]+1)-1)); m_fPercentile[j][1] = (1-fG2[j])*v(k2[j]) + fG2[j]*v(k2[j]+1); } } if (m_fPercentile[j][1] < v(0)) m_fPercentile[j][1] = v(0); if (m_fPercentile[j][1] > v(nVlen-1)) m_fPercentile[j][1] = v(nVlen-1); } } else { wTmp(0) = 1; //判断是否返回 if (bRet) return; //百分数int for ( i=0;i<DISP_PERCENT;i++) { double k = m_fPercentile[i][0]*nVlen; j = (long)ceil(k); if (nVlen%2 == 0) { if (j==int(k)) { m_fPercentile[i][1] = (v(j) + v(j-1))/2; } else { m_fPercentile[i][1] = v(j-1); } } else { long nIndex = (long)k; double fLeave = k- nIndex; if (m_fPercentile[i][0] == 0.25) { if (fLeave > 0.50) { m_fPercentile[i][1] = (v(nIndex+1) + v(nIndex))/2; } else { m_fPercentile[i][1] = v(nIndex); } } else if (m_fPercentile[i][0] == 0.50) { m_fPercentile[i][1] = v(nIndex); } else if (m_fPercentile[i][0] == 0.75) { if (fLeave < 0.50) { m_fPercentile[i][1] = (v(nIndex) + v(nIndex-1))/2; } else { m_fPercentile[i][1] = v(nIndex); } } else { if (j==int(k)) { m_fPercentile[i][1] = (v(j) + v(j-1))/2; } else { m_fPercentile[i][1] = v(j-1); } } } } } }
bool CHistogram::CalcHistogram() { int i=0, j=0, k=0; int nIndex=0, nPtCnt=0, nGroupIndex=0; int nColIndex=0, nRowCount=0, nDataType=0; double fTmp=0; CDWordArray arrCol; CMapManager dataMap; CDoubleMatrix dataMatrix; CIntVector vIndex; CDoubleVector v; CTString szTemp(""); CTString szFldNameX(""); CTString szFldNameY(""); if (m_tVarY.iCount > 0) { for (i=0; i<m_tVarY.iCount; i++) { nGroupIndex = m_tVarY.pValue[i]; szFldNameY = m_pDataInterface->GetFieldName(nGroupIndex); for (j=0; j<m_tVarX.iCount; j++) { nColIndex = m_tVarX.pValue[j]; arrCol.Add(nGroupIndex); arrCol.Add(nColIndex); szFldNameX = m_pDataInterface->GetFieldName(nColIndex); nRowCount = m_pDataInterface->GetColsData(arrCol,dataMatrix,dataMap); if (nRowCount < 1) { szTemp.Format("%s中的有效数据太少(分组变量=%s,数据变量=%s),无法绘图,请检查,Line=%d,File=%s",m_szChartName.GetData(),szFldNameY.GetData(),szFldNameX.GetData(),__LINE__,__FILE__); m_szErrMsg += szTemp; arrCol.RemoveAll(); dataMap.Clear(); dataMatrix.destroy(); continue; } //有效数据 v.create(nRowCount); CDoubleVector v1 = dataMatrix(0); CDoubleVector v2 = dataMatrix(1); v1.Sort(vIndex); fTmp = v1(0); nIndex = 0; for (k=0; k<nRowCount; k++) { if (fTmp == v1(k)) { v(nIndex) = v2(vIndex(k)-1); nIndex ++; continue; } szTemp = GetLabel(dataMap,0,nGroupIndex,fTmp); szTemp = "\n" + szTemp; //直方图 v.resize(nIndex); CalcHistogram(v,szFldNameX+szTemp); v.destroy(); v.create(nRowCount); fTmp = v1(k); nIndex =0; v(nIndex) = v2(vIndex(k)-1); } //最后 szTemp = GetLabel(dataMap,0,nGroupIndex,fTmp); szTemp = "\n" + szTemp; //直方图 v.resize(nIndex); CalcHistogram(v,szFldNameX+szTemp); v.destroy(); v.create(nRowCount); //释放内存 vIndex.destroy(); dataMap.Clear(); arrCol.RemoveAll(); dataMatrix.destroy(); } } } else { for (i=0; i<m_tVarX.iCount; i++) { nColIndex = m_tVarX.pValue[i]; arrCol.Add(nColIndex); szFldNameX = m_pDataInterface->GetFieldName(nColIndex); nRowCount = m_pDataInterface->GetColsData(arrCol,dataMatrix,0); if (nRowCount < 1) { szTemp.Format("%s中的有效数据太少(%s),无法绘图,请检查,Line=%d,File=%s",m_szChartName.GetData(),szFldNameX.GetData(),__LINE__,__FILE__); m_szErrMsg += szTemp; arrCol.RemoveAll(); dataMatrix.destroy(); continue; } //有效数据 v = dataMatrix(0); //直方图 CalcHistogram(v,szFldNameX); //释放内存 arrCol.RemoveAll(); dataMatrix.destroy(); } } return true; }