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

Message ListMessage List     Post MessagePost Message

  setMonthNames for Chinese font
Posted by james on Feb-12-2021 10:21
Attachments:
Hi,
     I've set the Chinese font for a finance chart.
//-------------------------------------

const char *month_name[12] = { _strdup(TCHARtoUTF8(_T("一月"))),_strdup( TCHARtoUTF8(_T("二月"))), _strdup(TCHARtoUTF8(_T("三月"))), _strdup(TCHARtoUTF8(_T("四月"))),_strdup(TCHARtoUTF8(_T("五月"))), _strdup(TCHARtoUTF8(_T("六月"))), _strdup(TCHARtoUTF8(_T("七月"))), _strdup(TCHARtoUTF8(_T("八月"))),_strdup(TCHARtoUTF8(_T("九月"))),_strdup(TCHARtoUTF8(_T("十月"))),_strdup(TCHARtoUTF8(_T("十一月"))), _strdup(TCHARtoUTF8(_T("十二月"))) };
//myFirstC->setMonthNames(StringArray(month_name, (int)(sizeof(month_name) / sizeof(month_name[0]))));
myFirstC->setMonthNames(StringArray(month_name, 12));
myFirstC->setFontTable(0, "simhei.ttf");
//--------------------------------------

     The last line above, I try to set font name, but it don't work. (this font has been verified for Chinese charactors, i.e, in the title display)
     You may notice that the 'setMonthNames' only act at the first chart, but not every one.
     Actually I see the Chinese month name as a small square --- every one for a Chinese charactor.

     What's wrong? or I missed something?

     Thanks

     James
chardir-font.png

  Re: setMonthNames for Chinese font
Posted by Peter Kwan on Feb-12-2021 17:02
Hi James,

For your chart, I believe the legend is drawn by the "track cursor". It is probably based on the trackFinance method is the "Finance Chart Track Line" sample code:

https://www.advsofteng.com/doc/cdcpp.htm#trackfinance.htm

In that sample code, the line is drawn the legend is:

legendText << "<*block,valign=top,maxWidth=" << (plotArea->getWidth() - 5)
            << "*><*font=arialbd.ttf*>[" << c->xAxis()->getFormattedLabel(xValue, "mmm dd, yyyy")
            << "]<*/font*>" << ohlcLegend.str();

The above code uses "arialbd.ttf" (Arial Bold) as the font, which does not support Chinese characters. There are two methods to display the problem:

(a) Modify the code to use "font=normal" instead. The "normal" font is the first font in the font table, which your code has set to "simhei.ttf".

(b) Modify the code to directly use "font=simhei.ttf"

Hope this can help.

Regards
Peter Kwan

  Re: setMonthNames for Chinese font
Posted by James on Feb-12-2021 18:29
Hi, Peter,
    Thanks for your fast reply.
    I noticed the legendbox code in tracking function, but I don't understand the grammar of <*block*><*font*>.... I've tried <*font,name='simhei.ttf *> but has no effect.
    In SetTitle function, we can set the font for the chart.
    But LegendBox has been set to 'transparent', in this case, anyone have to use the <*font=xxx*> to realize the Chinese display in legendbox of every sub chart?

    I'll try your solution later.

    Thank you so much.

    And best withes for Chinese New Year.

    James

  Re: setMonthNames for Chinese font
Posted by Peter Kwan on Feb-13-2021 03:30
Hi James,

The LegendBox can be included as part of the chart, just like the candlesticks and axes are part of the chart. The legend items can also be drawn in the dynamic layer. The dynamic layer is used for things that can change when the mouse moves. For your chart, the legend items can change when the mouse moves, so they are drawn in the dynamic layer. To avoid duplication, we need to disable the LegendBox in the chart. This is by setting the LegendBox in the chart to transparent.

I have tried myself using the following code:

const char* month_name[12] = { _strdup(TCHARtoUTF8(_T("一月"))),_strdup(TCHARtoUTF8(_T("二月"))), _strdup(TCHARtoUTF8(_T("三月"))), _strdup(TCHARtoUTF8(_T("四月"))),_strdup(TCHARtoUTF8(_T("五月"))), _strdup(TCHARtoUTF8(_T("六月"))), _strdup(TCHARtoUTF8(_T("七月"))), _strdup(TCHARtoUTF8(_T("八月"))),_strdup(TCHARtoUTF8(_T("九月"))),_strdup(TCHARtoUTF8(_T("十月"))),_strdup(TCHARtoUTF8(_T("十一月"))), _strdup(TCHARtoUTF8(_T("十二月"))) };

c->setMonthNames(StringArray(month_name, 12));

// The legend begins with the date label, then the ohlcLegend (if any), and then the
// entries for the indicators.
ostringstream legendText;
legendText << "<*block,valign=top,maxWidth=" << (plotArea->getWidth() - 5) << "*><*font=simhei.ttf*>[" << c->xAxis()->getFormattedLabel(xValue, "mmm dd, yyyy") << "]<*/font*>" << ohlcLegend.str();

and the above works normally in my case.


Note that you must set the month names to the object that formats the month labels. In the above code, the month labels are created using the object "c":

c->xAxis()->getFormattedLabel(xValue, "mmm dd, yyyy")

so the month names must set to "c":

c->setMonthNames(StringArray(month_name, 12));

I notice in your code, you use "myFirstC" as the object name. Make sure it is the object the formats the month labels.

Regards
Peter Kwan

  Re: setMonthNames for Chinese font
Posted by James on Feb-13-2021 11:20
Attachments:
Hi,Peter,
  At now the children chart work fine.
  You need to set month_name for every children chart.
  Also the xaxis style: (in drawchart function)
  //---------------------
    // Create a FinanceChart object of width 640 pixels
FinanceChart *c = new FinanceChart(VIEW_WIDTH);

c->setXAxisStyle("simhei.ttf", 8,0x111111,0 );  //james++
  //---------------------
  Because the xasix use the default font aria.

  But thers is a strange issue: some of the xaxis name (Chinese) goes wrong.
  The legendbox of children chart works fine all the time.

  See the attached image.

  Regards

  James
chardir-font-xaxis.png
chardir-font-xaxis1.png

  Re: setMonthNames for Chinese font
Posted by Peter Kwan on Feb-14-2021 22:20
Hi James,

In some cases, the axis labels use two different fonts - a regular font and a bold font. The setXAxisStyle can be change one of the font, which is the normal font. So the bold labels are still using the old font.

I think for the purpose of supporting Chinese, it is better to use BaseChart.setDefaultFonts on the last subchart (the x-axis labels are on the last chart) to set the default fonts. It is like:

// Assume this is the last indicator
XYChart* lastChart = addIndicator(....);

// Set the default regular font to simhei.ttf, and the default bold font to artificially boldened simhei.ttf
lastChart->setDefaultFont("simhei.ttf", "simhei.ttf bold");

Hope this can help.

Regards
Peter Kwan

  Re: setMonthNames for Chinese font
Posted by James on Feb-15-2021 17:15
Hi, Peter
  Thanks a lot, the
  setDefaultFonts("simhei.ttf", "simhei.ttf bold");
  make the charts perfect.

  Regards

  James