Hi Moses,
I believe your code is similar to the following sample code:
https://www.advsofteng.com/doc/cdcpp.htm#datatable2.htm
Like HTML and other word processors, by default, ChartDirector uses "baseline" alignment. It means the image is considered to be a fancy text character, and is aligned to the "baseline" just like another text characters. In your image, the image does not appear to be aligning to the baseline.
In any case, you can use CDML to align the text to the vertical middle. In the original sample code, the code is like:
sprintf_s(buffer, sizeof(buffer), "%s FY 2007", layer->getLegendIcon(0));
To align to the vertical middle, you may use:
sprintf_s(buffer, sizeof(buffer), "<*block,valign=middle*><*block*>%s<*/*> <*block*>FY 2007<*/*>", layer->getLegendIcon(0));
However, note that aligning to the middle may not be what you think. Like HTML, "middle" aligns to the middle of the "layout rectangle" above the baseline, not middle of the visible text. (Because characters have different heights, if vertical alignment is based on the visible text, every line will have different vertical alignment.) The "layout rectangle" is a rectangle specified in the font that programs should use to compute layout. The rectangle is large enough to fit the tallest character (characters with accents) and may still have some space left.
There is another alignment called "absmiddle", which aligns to the entire layout rectangle (as opposed to the part above the baseline).
If the "middle" is at a fractional pixel coordinate, the image may have to move up or down by up to 0.5 pixels to "grid fit" to the pixels.
To see where is the layout rectangle, a common way is to add a background color to the text for debugging:
sprintf_s(buffer, sizeof(buffer), "<*block,valign=middle*><*block*>%s<*/*> <*block,bgcolor=FFFF00*>FY 2007<*/*>", layer->getLegendIcon(0));
In fact, in your case, the image may already be middle aligned. It seems to fit within the layout rectangle (which is taller than the characters), and it may have been shifted up due to the fractional pixel.
For your case, you may consider to change the legend icon size to fit your font size, and just use the default baseline alignment. For a bar chart, the icon size is configurable using LegendBox.setKeySize.
c->getLegend()->setKeySize(7, 7);
If you need further help, is it possible to provide an example (perhaps by modifying the original Data Table (2) sample code) to allow me to reproduce the problem?
Best Regards
Peter Kwan |