|
[MFC] Bargraph Labeling. |
Posted by Kaiden on Sep-23-2013 11:38 |
|
Hey Peter,
Thanks for the previous helps on my Qs.
I encountered another problem in labeling on Bar Graph. I am drawing a histogram like the
code below at the moment.
double* pRange
int* pCount;
pRange = new double[nScale+1];
pCount = new int[nScale];
pRange[0] = min;
for(int i=1; i< (nScale+1) ; i++)
{
pRange[i] = pRange[i-1] + distance;
CString str;
str.Format(_T("%.2f"), pRange[i]);
int strLen = str.GetLength();
m_HistoLabels[i] = new char[strLen+1];
WideCharToMultiByte(CP_ACP, 0, str, -1, m_HistoLabels[i], strLen+1,
NULL, NULL);
}
pRange[nScale]= max;
for(int i=0; i< m_pDoc->iTotalRowCount; i++)
{
double check = 0;//_wtof(m_pDoc->vec2dStr[i+5]
[arrFindVec.GetAt(0)]);
check = _wtof(m_pDoc->vec2dStr[i+5][arrFindVec.GetAt(0)]);
for(int j=0; j< (nScale+1); j++)
{
if( pRange[j] < check)
{
if( check < pRange[j+1])
{
pCount[j]++;
break;
}
}
}
}
for(int i=0; i< nScale; i++)
{
m_nHistoCount[i][0] = (double)pCount[i];
}
m_pChart = new XYChart(800, 600, 0xf0f0f0, 0xaaaaaa);
m_pChart->setPlotArea(75, 70, m_pChart->getWidth() - 90, m_pChart-
>getHeight() - 150, Chart::Transparent, -1, Chart::Transparent, 0x7F7F7F,
Chart::Transparent);
m_pChart->addLegend(30, 20, false, "arialbd.ttf", 9)-
>setBackground(Chart::Transparent);
m_pChart->setClipping();
//////**************************************************************
***************/////////////
m_chartView.setFullRange("x", min, max-1);
double startLabelIndex = m_chartView.getValueAtViewPort("x",
m_chartView.getViewPortLeft());
double endLabelIndex = m_chartView.getValueAtViewPort("x",
m_chartView.getViewPortLeft() + m_chartView.getViewPortWidth());
m_chartView.setFullRange("x", startLabelIndex, endLabelIndex-1);
m_pChart->xAxis()->setLinearScale(startLabelIndex, endLabelIndex,
Chart::NoValue);
//////**************************************************************
***************/////////////
m_pChart->xAxis()->setLabelStyle("arialbd.ttf", 7, 0x008000)-
>setFontAngle(45);
// Set legend icon style to use line style icon, sized for 8pt font
m_pChart->getLegend()->setLineStyleKey();
m_pChart->getLegend()->setFontSize(8);
m_pChart->yAxis()->setMinTickInc(1);
// Set the axis stem to transparent
m_pChart->xAxis()->setColors(Chart::Transparent);
m_pChart->yAxis()->setColors(Chart::Transparent);
char* m_Title;
int strTitleLen = m_strTitle.GetLength();
m_Title = new char[strTitleLen+1];
WideCharToMultiByte(CP_ACP, 0,m_strTitle, -1, m_Title, strTitleLen+1, NULL,
NULL);
m_pChart->addTitle(m_Title, "ariali.ttf", 14);
BarLayer* layer = m_pChart->addBarLayer(Chart::Side, 3);
for(int j=0; j< m_ctrlListBox.GetSelCount(); j++)
{
for(int i=0; i< nScale; i++)
{
layer->addDataSet(DoubleArray(m_nHistoCount[i], arrFoundSize),
m_pColorTable->aryListBoxTableColor.GetAt(j));
layer->setBarGap(0.2, Chart::TouchBar);
}
}
m_chartView.syncLinearAxisWithViewPort("x", m_pChart->xAxis());
m_chartView.syncLinearAxisWithViewPort("y", m_pChart->yAxis());
m_chartView.setScrollDirection(Chart::DirectionHorizontalVertical);
m_chartView.setZoomDirection(Chart::DirectionHorizontalVertical);
if you delete the part between
//////************************************************/////////////
these two, bar graph works fine. but with the labels, labels showsup but no bar graph.
But labels aren't properly labeled.
Could you give me a hand on this? If you need anything please, let me know:)
Best Regards,
Kaiden.
|
Re: [MFC] Bargraph Labeling. |
Posted by Peter Kwan on Sep-24-2013 05:39 |
|
Hi Kaiden,
I think the code in the following thread has already been confirmed as working:
http://www.chartdir.com/forum/download_thread.php?bn=chartdir_support&thread=1373960759#N1374010998
If you want to label the x-axis, simply change the myLabels array to the labels you want to use.
In your new code, you have set the x-axis range to something else (from min to max-1), but your bars are not plotted at those positions. That's why your bars are not at the same place as the labels. In the code in the above link, the x-axis scale is set to a range consistent with the bar positions. That's why the labels match with the bars.
Hope this can help.
Regards
Peter Kwan |
Re: [MFC] Bargraph Labeling. |
Posted by Kaiden on Sep-25-2013 11:14 |
|
Hi Peter,
I made some mistakes and fixed the labeling problem.
But another thing showed up. If i hide
take a look at the attached captures and how it was created.
/////*****************
[pic 1] - proper label, histogram pushed to the left
-m_chartView.setFullRange("x", 0, nScale-1);
-//m_chartView.syncLinearAxisWithViewPort("x", m_pChart->xAxis());
[pic 2] -one label, but proper histogram
-//m_chartView.setFullRange("x", 0, nScale-1);
-m_chartView.syncLinearAxisWithViewPort("x", m_pChart->xAxis());
/////*****************
m_chartView.setFullRange("x", 0, nScale-1);
double startLabelIndex = m_chartView.getValueAtViewPort("x",
m_chartView.getViewPortLeft());
double endLabelIndex = m_chartView.getValueAtViewPort("x",
m_chartView.getViewPortLeft() + m_chartView.getViewPortWidth());
m_pChart->xAxis()->setLinearScale(startLabelIndex, endLabelIndex,
Chart::NoValue);
m_pChart->xAxis()->setLabelStyle("arialbd.ttf", 7, 0x008000)-
>setFontAngle(45);
// Add the labels with the range
for (int i = (int)ceil(startLabelIndex); i <= endLabelIndex; i++)
{
m_pChart->xAxis()->addLabel(i, m_HistoLabels[i]);
}
m_pChart->xAxis()->setLinearScale(startLabelIndex, endLabelIndex,
Chart::NoValue);
// Set legend icon style to use line style icon, sized for 8pt font
m_pChart->getLegend()->setLineStyleKey();
m_pChart->getLegend()->setFontSize(8);
m_pChart->yAxis()->setMinTickInc(1);
// Set the axis stem to transparent
m_pChart->xAxis()->setColors(Chart::Transparent);
m_pChart->yAxis()->setColors(Chart::Transparent);
char* m_Title;
int strTitleLen = m_strTitle.GetLength();
m_Title = new char[strTitleLen+1];
WideCharToMultiByte(CP_ACP, 0,m_strTitle, -1, m_Title, strTitleLen+1, NULL,
NULL);
m_pChart->addTitle(m_Title, "ariali.ttf", 14);
//**
// Display 1 out of 3 labels on the x-axis.
//m_pChart->xAxis()->setLabelStep(1); //
//**
//m_nMouseMove = 0 ; //reset again.
BarLayer* layer = m_pChart->addBarLayer(Chart::Side, 3);
for(int j=0; j< m_ctrlListBox.GetSelCount(); j++)
{
for(int i=0; i< nScale; i++) // depending on what the user have chosen.
{
layer->addDataSet(DoubleArray(m_nHistoCount[i], arrFoundSize),
m_pColorTable->aryListBoxTableColor.GetAt(j+1));
layer->setBarGap(0.2, Chart::TouchBar);
}
}
// Set the x-axis as a date/time axis with the scale according to the view port x range.
//m_chartView.syncLinearAxisWithViewPort("x", m_pChart->xAxis());
m_chartView.syncLinearAxisWithViewPort("y", m_pChart->yAxis());
|
Re: [MFC] Bargraph Labeling. |
Posted by Peter Kwan on Sep-26-2013 01:59 |
|
Hi Kaiden,
Actually, I cannot quite understand your code. For example, I do not understand what the following code doing:
for(int j=0; j< m_ctrlListBox.GetSelCount(); j++)
{
for(int i=0; i< nScale; i++) // depending on what the user have chosen.
{
layer->addDataSet(DoubleArray(m_nHistoCount[i], arrFoundSize),
m_pColorTable->aryListBoxTableColor.GetAt(j+1));
layer->setBarGap(0.2, Chart::TouchBar);
}
}
I think to add the data, you just need something like:
layer->addDataSet(DoubleArray(pCount, nScale), m_pColorTable->aryListBoxTableColor.GetAt(1));
Hope this can help.
Regards
Peter Kwan |
Re: [MFC] Bargraph Labeling. |
Posted by Kaiden on Sep-30-2013 20:59 |
|
Hi Peter,
Thank you.
layer->addDataSet(DoubleArray(pCount, nScale), m_pColorTable-
>aryListBoxTableColor.GetAt(1));
This works just fine as the one I used before. Much more simple
Still have some issues with labeling though.
I will ask in more related previous question.
Thank you always.
Best Regards,
Kaiden. |
|