|
Create a real histogram in chartdir |
Posted by Anis on Sep-14-2012 17:07 |
|
Hi,
I need to build a histogram from chartdirector chart so i use Bar charts but it does'nt
match my requirement so maybe i miss something.
This is the context:
i have data which represent number of values that are included in an floating range
Range => Number of values
0,000000 1,666904 5
1,666904 3,333808 11
3,333808 5,000712 269
5,000712 6,667616 6
6,667616 8,33452 58
8,33452 10,001424 14
you notice that the range width is the same.
from this sample i will have
double *pRange = {0,000000, 1,666904,3,333808,5,000712,6,667616,8,33452}
double *pNumberOfValuesBuffer = {5,11,269,6,58,14}
this is how i create the bar chart :
BarLayer * pBarLayer = m_pXYChart->addBarLayer();
pBarLayer->addDataSet(DoubleArray(pNumberOfValuesBuffer, iBufferSize), nFillColor,
strString);
pBarLayer->setXData(DoubleArray(pRange,iBufferSize));
But the result is not like what i expect because the bar width doesn't match the the
range width in the chart.
Is there any solution for my requirement
thank you in advance |
Re: Create a real histogram in chartdir |
Posted by Peter Kwan on Sep-15-2012 00:16 |
|
Hi Anis,
In your case, the bars are not at x = 0, 1.6669, 3.3338, ... Instead, the first bar is in between 0 and 1.6669. Also, in your case, there are 7 values on the x-axis from 0 to 10,001424, but only 6 bars.
For your case, I suggest you just treat the x-axis labels as names for human reading only. For bar charts, in 99% of the cases, x-coordinates are not needed except for human reading. There are two methods depending on how you would like to label the bars:
(a) Use 6 labels and 6 bars. The labels I suggest are "0 to 1.67", "1.67 to 3.33", "3.33 to 5", ..... The code is like:
BarLayer * pBarLayer = m_pXYChart->addBarLayer(DoubleArray(pNumberOfValuesBuffer, iBufferSize), nFillColor, strString);
m_pXYChart->xAxis()->setLabels(StringArray(myArrayOfTextStrings, iBufferSize));
(b) Use 7 labels and 6 bars. The labels are "0", "1.67", "3.33", .... , "10". The code is like:
BarLayer * pBarLayer = m_pXYChart->addBarLayer(DoubleArray(pNumberOfValuesBuffer, iBufferSize), nFillColor, strString);
m_pXYChart->xAxis()->setLinearScale(-0.5, iBufferSize - 0.5, StringArray(myArrayOfTextStrings, iBufferSize + 1));
m_pXYChart->xAxis()->setIndent(false);
In either case, there will be gaps between bars. If you would the bars to touch each others, you may use BarLayer.setBarGap, like:
pBarLayer->setBarGap(Chart::TouchBar);
Hope this can help.
Regards
Peter Kwan |
|