|
Chinese can display in title, but can't in the label |
Posted by piwi on Sep-17-2010 23:54 |
|
Chinese can display in title and legendbox, but can't in the x label
please help, thank you!
CODE:
double barData[] = {5.0, 4.8, 5.25, 6.47, 6.7, 7.6, 7.10, 5.15, 5.7, 4.17, 3.20, 3.25};
const char *labels[] = {TCHARtoUTF8(_T("一月")), TCHARtoUTF8(_T("二月")), TCHARtoUTF8(_T("三月")), TCHARtoUTF8(_T("四月")),
TCHARtoUTF8(_T("五月")), TCHARtoUTF8(_T("六月")), TCHARtoUTF8(_T("七月")), TCHARtoUTF8(_T("八月")),
TCHARtoUTF8(_T("九月")), TCHARtoUTF8(_T("十月")), TCHARtoUTF8(_T("十一月")), TCHARtoUTF8(_T("十二月"))};
XYChart *c = new XYChart(450, 220);
c->setBackground(0xECE9D8,0xECE9D8, 0);
c->setDefaultFonts("simsun.ttc");
TextBox *title = c->addTitle(TCHARtoUTF8(_T("总量")),"simsun.ttc", 10);
title->setMargin(0, 0, 8, 8);
c->setPlotArea(50, title->getHeight(), c->getWidth() - 65, c->getHeight() -
title->getHeight() - 10);
LegendBox *legendBox = c->addLegend(c->getWidth() / 2, c->getHeight() , false, "simsun.ttc", 8);
legendBox->setAlignment(Chart::BottomCenter);
legendBox->setBackground(Chart::Transparent, Chart::Transparent);
legendBox->setKeySize(15, 10);
legendBox->setLineStyleKey();
c->xAxis()->setColors(Chart::Transparent);
c->xAxis()->setLabels(StringArray(labels, sizeof(labels)/sizeof(labels[0])));
c->xAxis()->setLabelStyle("simsun.ttc");
SplineLayer *lineLayer = c->addSplineLayer(DoubleArray(fdata, sizeof(fdata)/sizeof(fdata[0])), 0x008040, TCHARtoUTF8(_T("因子")));
lineLayer->setLineWidth(2);
lineLayer->setUseYAxis2();
BarLayer *barLayer = c->addBarLayer(DoubleArray(barData, sizeof(barData)/sizeof(barData[0])), 0xfd6421, TCHARtoUTF8(_T("量")));
c->makeChart();
m_SloarGraph.setChart(c);
|
Re: Chinese can display in title, but can't in the label |
Posted by Peter Kwan on Sep-18-2010 01:43 |
|
Hi piwi,
According to the ChartDirector documentation on TCHARtoUTF8, you must use the text string before the TCHARtoUTF8 is destroyed.
In your code, the TCHARtoUTF8 is destroyed after the line "const char *labels[] = ....". When your code uses the labels at the line "c->xAxis()->setLabels(...);", the labels are already destroyed, so there is no label.
If you need to retain the conversion result, you would need to store it. For example:
//use strdup to allocate memory to store the text string
const char *labels[] = {strdup(TCHARtoUTF8(_T("一月"))), strdup(TCHARtoUTF8(_T("二月"))), .....};
After you have finished the charting code, remember to free the memory:
for (int i = 0; i < sizeof(labels)/sizeof(labels[0]); ++i)
free(labels[i]);
Hope this can help.
Regards
Peter Kwan |
Re: Chinese can display in title, but can't in the label |
Posted by guhuisec on Jan-23-2013 17:41 |
|
thx ! you are great!
Peter Kwan wrote:
Hi piwi,
According to the ChartDirector documentation on TCHARtoUTF8, you must use the text string before the TCHARtoUTF8 is destroyed.
In your code, the TCHARtoUTF8 is destroyed after the line "const char *labels[] = ....". When your code uses the labels at the line "c->xAxis()->setLabels(...);", the labels are already destroyed, so there is no label.
If you need to retain the conversion result, you would need to store it. For example:
//use strdup to allocate memory to store the text string
const char *labels[] = {strdup(TCHARtoUTF8(_T("一月"))), strdup(TCHARtoUTF8(_T("二月"))), .....};
After you have finished the charting code, remember to free the memory:
for (int i = 0; i < sizeof(labels)/sizeof(labels[0]); ++i)
free(labels[i]);
Hope this can help.
Regards
Peter Kwan
|
|