ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Changing the font used by AddZone in LinearMeter in C++
Posted by Alain on Jun-13-2020 15:41
Attachments:
Hello,

I use a LinearMeter :
    LinearMeter *m = new LinearMeter(430, 80, WHITE, 0, 0);

I add a title with a specific font:
    sprintf(str, "%s = %.2f", IMC, value);
    m->addTitle(Chart::Bottom, str, "arial.ttf", 8)->setBackground(Chart::Transparent, 0, 0);

And I add zones like:
    m->addZone(10  , 18.5, BMIBLUE,    "Underweight" );
    m->addZone(18.5, 25  , BMIGREEN,  "Normal (healthy weight)");

Is it possible to change the font of the texts added by AddZone ? I have seen that it is a TextBox but I do not see how to modify it.
And is it possible to make the text fit to the witdh of the zone ? Making a broader image size is a possibility but not the most elegant! For example "Normal (healthy weight)" is a long string, to long for the width of the zone.

Any help would be apperciated!

Regards

Alain
imc.png

  Re: Changing the font used by AddZone in LinearMeter in C++
Posted by Peter Kwan on Jun-13-2020 20:42
Hi Alain,

To change the font, it is like:

TextBox *t = m->addZone(10  , 18.5, BMIBLUE,    "Underweight" );
t->setFontSize(12);   // To change the font size
t->setFontStyle("arialbd.ttf");   // To change the font family

For "making the text fit to the width of the zone", suppose the zone is 20 pixels wide (enough for 3 characters), but your label is "ABCD DFGH 1234 5678", how would like to fit the label to the 20 pixels zone? In another example, suppose the zone is 500 pixels wide and the label is "AB"? Would you like to increase the font size so that the "AB" is 500 pixels wide? The font size will probably be so large so that its height exceeds the height of the zone.

ChartDirector can truncate the label to a given width (that is, throw away some characters at the end and replace them with 3 dots "..." , provided the width is large enough for 3 dots). ChartDirector can also wrap text to multiple lines to fit a given width, and truncate the text is there are too many lines. Truncation ensures the font size is consistent and at least some text is readable.

ChartDirector can also tell you the width of some text given a font style and font size. The method is to create an invisible TextBox (using BaseChart.addText) and then use Box.getWidth to get its width. The TextBox can be invisible if it is located outside the chart (eg. with coordinates (-9999, -100). Since the font size is only has a limited range (eg. from 1 to 20), you can try one by one in a loop to determine the optimal font size to use.

Regards
Peter Kwan

Alain wrote:

Hello,

I use a LinearMeter :
    LinearMeter *m = new LinearMeter(430, 80, WHITE, 0, 0);

I add a title with a specific font:
    sprintf(str, "%s = %.2f", IMC, value);
    m->addTitle(Chart::Bottom, str, "arial.ttf", 8)->setBackground(Chart::Transparent, 0, 0);

And I add zones like:
    m->addZone(10  , 18.5, BMIBLUE,    "Underweight" );
    m->addZone(18.5, 25  , BMIGREEN,  "Normal (healthy weight)");

Is it possible to change the font of the texts added by AddZone ? I have seen that it is a TextBox but I do not see how to modify it.
And is it possible to make the text fit to the witdh of the zone ? Making a broader image size is a possibility but not the most elegant! For example "Normal (healthy weight)" is a long string, to long for the width of the zone.

Any help would be apperciated!

Regards

Alain

  Re: Changing the font used by AddZone in LinearMeter in C++
Posted by Alain on Jun-14-2020 01:00
Attachments:
Hi Peter,

Thanks a lot! You helped me solve the problem.

With a scale from 10 to 45 and a meter of 400 units width, I just have to compute the size of reach zone and restrict the textbox to it.

    m->setMeter(10, 10, 410, 40, Chart::Bottom);
    // Set meter scale from 14 - 45
    m->setScale(10, 45);

    // Add a title at the bottom of the meter with a 1 pixel raised 3D border
    sprintf(str, "%s = %.2f", IMC, value);
    m->addTitle(Chart::Bottom, str, "arial.ttf", 8)->setBackground(Chart::Transparent, 0, 0);

    TextBox *t1 = m->addZone(10  , 18.5, BMIBLUE,    Maigreur);
    t1->setFontSize(8);   // To change the font size
    t1->setFontStyle("arial.ttf");   // To change the font family
    t1->setWidth( (18.5-10.0)/(45.0-10.0)*400.0 );

Regards,

Alain
imc0.png