|
Some labels not displaying on chart? |
Posted by Dave on Apr-03-2011 08:25 |
|
I have a chart that works correctly for all labels, unless the labels happen to reference a point that is at the Y origin. At least that seems to be the only difference between the labels that display correctly and those that do not display at all.
I've checked the label array I'm creating and the labels are definitely in the array. And yet... for those labels that reference an xy point (n,0), the label does not print on the chart. I've attached a pic of one of the charts with the label issue.
This is my code that adds the labels to the chart:
// Add labels to the chart as an extra field
basedatalayer->addExtraField(StringArray(labels, numpoints));
basedatalayer->setDataLabelFormat("{field0}"); // Set the data label format to display the extra field
TextBox *textbox = basedatalayer->setDataLabelStyle( "arialbd.ttf", 8 );
textbox->setFontColor( ColorWhite );
textbox->setBackground( Chart::Transparent, Chart::Transparent, 0 );
textbox->setAlignment( Chart::TopCenter );
// Get font metrics to get height of label so we can position it above the bar line
TTFText* textmetrics = c->getDrawArea()->text( testlabel.c_str().AsChar(), "arialbd.ttf", 8 );
int fontheight = textmetrics->getHeight();
textbox->setPos( 0, -( textmetrics->getHeight() ) ); // shift top label above the bar line
-----------------
I've tried this with and without the TextBox/setPos() code. Am I doing something incorrectly?
|
Re: Some labels not displaying on chart? |
Posted by Peter Kwan on Apr-04-2011 13:42 |
|
Hi Dave,
I am not too sure what is basedatalayer in your code. After studying your chart, I suspect it refers to the bar layer (that is, the labels are associated with the bars). Is this correct?
In ChartDirector, a bar data label is supposed to be inside the bar (though you can always shift it). By default, ChartDirector will not display the bar label if the bar is too short that it would not be possible for it to contain any label. You may disable this (force the bar to show all labels) by using:
basedatalayer->setMinLabelSize(0);
If your intention is to display the label on top of the bar and outside the bar, you may use aggregate label instead. In this way, you do not need to shift the labels, and the labels are always displayed.
basedatalayer->setAggregateLabelFormat("{field0}");
TextBox *textbox = basedatalayer->setAggregateLabelStyle( "arialbd.ttf", 8 );
textbox->setFontColor( ColorWhite );
textbox->setBackground( Chart::Transparent, Chart::Transparent, 0 );
textbox->setAlignment( Chart::TopCenter );
(Note: A bar can potentially has several segments, like in a stacked bar chart, overlay bar chart or percentage bar chart. The data labels are for the data values, which are represented as segments within the bar. To label segments, we have to put the labels inside the segments. That's why the data label is inside the bar segment. The aggregate label is for labelling the entire bar, and it is outside the bar. For consistency, if a bar chart is not stacked, ChartDirector will treat it the same as a stacked bar chart with 1 stack.)
Should you need further information, please feel free to contact me.
Regards
Peter Kwan |
Re: Some labels not displaying on chart? |
Posted by Dave on Apr-05-2011 08:32 |
|
Peter Kwan wrote:
I am not too sure what is basedatalayer in your code. After studying your chart, I suspect it refers to the bar layer (that is, the labels are associated with the bars). Is this correct?
Yes.
In ChartDirector, a bar data label is supposed to be inside the bar (though you can always shift it). By default, ChartDirector will not display the bar label if the bar is too short that it would not be possible for it to contain any label. You may disable this (force the bar to show all labels) by using:
basedatalayer->setMinLabelSize(0);
Thank you, Peter, that was exactly it. And I appreciate your explanation as to stacked charts and the reason for the display method of labels with the bars. Very helpful--I had no idea why the chart was not displaying the labels when the values went to zero. So thank you for so much for your thorough and helpful direction!
Dave |
|