|
Getting the size of labels |
Posted by Electron Shepherd on May-10-2015 21:09 |
|
I'm adding labels to the Y axis of an XY chart, and calling setLabelStyle() to format them. The data has already been added to the chart, so CD should know what the labels are.
The TextBox object returns gives 0 for the getHeight() and getWidth() functions. I've tried calling layout() and makeChart after calling setLabelStyle(), but they still return 0. How can I find out the width and height of the box that contains the Y labels? |
Re: Getting the size of labels |
Posted by Peter Kwan on May-13-2015 23:13 |
|
Hi Electron,
Sorry for the late reply.
The TextBox object returned by setLabelStyle is just a template for the labels. There are
many labels and everyone can have a different length. So the TextBox object cannot
return any particular legend as there are many different lengths.
If you are referring the the "box that contains all the y-axis labels", it is a property of the
y-axis and can be obtained using Axis.getThickness. In a typical case, the y-axis labels
are automatically determined by ChartDirector. To determine the labels, ChartDirector
needs to know the plot area height, as it can put more labels if the plot area is larger,
and less labels if it is smaller. So the steps should be:
(a) Set up the plot area size first,
(b) Enter all the data into the chart.
(c) With (a) and (b), the labels can be determined. To do this, you would need to call
Axis.layoutAxes(). Calling this method means you have already entered all the data and
the plot area size, so ChartDirector can layout the axis and determine the labels.
(d) You can know use Axis.getThickness to get the width of the box that contains all the
labels.
If you are trying to adjust the plot area size based on the length of the labels, the above
will not be the ideal method, you may consider to use the "packPlotArea" method to do
everyone in one step. You may refer to the sample code "Bars with Marks" for an
example.
Hope this can help.
Regards
Peter Kwan |
Re: Getting the size of labels |
Posted by Electron Shepherd on May-14-2015 04:39 |
|
Peter,
The reason I was going down the route of determining all the widths manually is that packPlotArea doesn't seem to handle "label overlaps" on the plot area.
To demonstrate, take markbar sample, and change the code for the title to be
TextBox *title = c->addTitle("Bars with Marks Demonstration", "ariali.ttf", 0);
title->setMargin(0, 0, 0, 0);
All this does is effectively remove the title. When you run this, the "300" at the top of the Y axis is truncated, which looks bad. In my situation, we are offering the user full flexibility for font style and size for all the text elements of the charts, so simply hard-coding a certain padding in pixels (as the samples do) won't work for us, since it will be wrong for most cases.
How can I make packPlotArea take into account the fact that the very top of the highest number is above the very top of the axis. Can I work out how high each label is on the axis? If so, I could use half this height as a padding.
|
Re: Getting the size of labels |
Posted by Peter Kwan on May-15-2015 01:24 |
|
Hi Electron,
It is correct that in packPlotArea, the plot area height will be adjusted according "axis
thickness", which does not include the parts of the labels that are outside the axis length.
There is a method to computer the label height. However, if I were you, I will just assume
the label height to be fontSize * 4 / 3 pixels. It is because by definition, 1 pt (the font unit)
is equal to 1/72 inches. On Windows, by default, the screen is assumed to be 96 pixels per
inch, and this factor is used by ChartDirector. That means 1 pt = 4/3 pixels. So the "design
surface" (called the EM square) of the text should be fontSize * 4 / 3 pixels. (If you are
familiar with HTML, it supports the "em" unit, which is referring to the size of the EM
square.) However, the standard allows the font designer to design a font that draws
outside the EM square, so it is theoretically possible for a font to deviate slightly from the
fontSize * 4 / 3 formula. If this formula is acceptable to you in practice, you can set the
margin to (fontSize * 2 + 2) / 3, which is half EM height rounded up to an integer.
If you want the exact height, one method is to create a text box using BaseChart.addText,
putting the box outside of the chart, using the font you want to measure, and just set the
text to "0" and the margin to 0. You can then read the height of the textbox using
Box.getHeight and it would be the line height of the font. (The line height of the text is the
same irrespective of the actual characters used. This is to ensure every line using the same
font will have the same height.) We usually use the variable name "measurer" for this
textbox as its purpose is to measure the font metrics.
Hope this can help.
Regards
Peter Kwan |
|